error.rs (3386B)
1 #[cfg(doc)] 2 use super::{AsciiDomain, DomainOrigin, Port, RpId, Scheme, Url}; 3 #[cfg(doc)] 4 use core::str::FromStr; 5 use core::{ 6 error::Error, 7 fmt::{self, Display, Formatter}, 8 num::ParseIntError, 9 }; 10 /// Error returned by [`AsciiDomain::try_from`] when the `String` is not a valid ASCII domain. 11 #[derive(Clone, Copy, Debug)] 12 pub enum AsciiDomainErr { 13 /// Variant returned when the domain is empty. 14 Empty, 15 /// Variant returned when the domain is the root domain (i.e., `'.'`). 16 RootDomain, 17 /// Variant returned when the domain is too long. 18 Len, 19 /// Variant returned when an empty label exists. 20 EmptyLabel, 21 /// Variant returned when a label is too long. 22 LabelLen, 23 /// Variant returned when a label contains a `u8` that is not valid ASCII. 24 NotAscii, 25 } 26 impl Display for AsciiDomainErr { 27 #[inline] 28 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 29 f.write_str(match *self { 30 Self::Empty => "domain is empty", 31 Self::RootDomain => "domain is the root domain", 32 Self::Len => "domain is too long", 33 Self::EmptyLabel => "domain has an empty label", 34 Self::LabelLen => "domain has a label that is too long", 35 Self::NotAscii => "domain has a label that contains a non-ASCII byte", 36 }) 37 } 38 } 39 impl Error for AsciiDomainErr {} 40 /// Error returned by [`Url::from_str`] when the `str` passed to the 41 /// [URL serializer](https://url.spec.whatwg.org/#concept-url-serializer) leads to a failure. 42 #[derive(Clone, Copy, Debug)] 43 pub struct UrlErr; 44 impl Display for UrlErr { 45 #[inline] 46 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 47 f.write_str("URL serializer failed") 48 } 49 } 50 impl Error for UrlErr {} 51 /// Error returned by [`Scheme::try_from`] when the passed [`str`] is empty. 52 #[derive(Clone, Copy, Debug)] 53 pub struct SchemeParseErr; 54 impl Display for SchemeParseErr { 55 #[inline] 56 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 57 f.write_str("scheme was empty") 58 } 59 } 60 impl Error for SchemeParseErr {} 61 /// Error returned by [`Port::from_str`] when the passed [`str`] is not a valid unsigned 16-bit integer in 62 /// decimal form without leading 0s. 63 #[derive(Debug)] 64 pub enum PortParseErr { 65 /// Variant returned iff [`u16::from_str`] does. 66 ParseInt(ParseIntError), 67 /// Variant returned iff a `str` is a valid 16-bit unsigned integer with leading 0s. 68 NotCanonical, 69 } 70 impl Display for PortParseErr { 71 #[inline] 72 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 73 match *self { 74 Self::ParseInt(ref err) => err.fmt(f), 75 Self::NotCanonical => { 76 f.write_str("string was a valid TCP/UDP port number, but it had leading 0s") 77 } 78 } 79 } 80 } 81 impl Error for PortParseErr {} 82 /// Error returned by [`DomainOrigin::try_from`]. 83 #[derive(Debug)] 84 pub enum DomainOriginParseErr { 85 /// Variant returned when there is an error parsing the scheme. 86 Scheme(SchemeParseErr), 87 /// Variant returned when there is an error parsing the port. 88 Port(PortParseErr), 89 } 90 impl Display for DomainOriginParseErr { 91 #[inline] 92 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 93 match *self { 94 Self::Scheme(err) => err.fmt(f), 95 Self::Port(ref err) => err.fmt(f), 96 } 97 } 98 } 99 impl Error for DomainOriginParseErr {}