webauthn_rp

WebAuthn Level 3 RP library.
git clone https://git.philomathiclife.com/repos/webauthn_rp
Log | Files | Refs | README

error.rs (3798B)


      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 `Vec` is not a valid ASCII domain.
     11 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     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, Eq, PartialEq)]
     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 [`RpId::try_from`] when the `String` is not a valid [`AsciiDomain`] nor [`Url`].
     52 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     53 pub struct RpIdErr;
     54 impl Display for RpIdErr {
     55     #[inline]
     56     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
     57         f.write_str("RpId is invalid")
     58     }
     59 }
     60 impl Error for RpIdErr {}
     61 /// Error returned by [`Scheme::try_from`] when the passed [`str`] is empty.
     62 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     63 pub struct SchemeParseErr;
     64 impl Display for SchemeParseErr {
     65     #[inline]
     66     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
     67         f.write_str("scheme was empty")
     68     }
     69 }
     70 impl Error for SchemeParseErr {}
     71 /// Error returned by [`Port::from_str`] when the passed [`str`] is not a valid unsigned 16-bit integer in
     72 /// decimal form without leading 0s.
     73 #[derive(Debug, Eq, PartialEq)]
     74 pub enum PortParseErr {
     75     /// Variant returned iff [`u16::from_str`] does.
     76     ParseInt(ParseIntError),
     77     /// Variant returned iff a `str` is a valid 16-bit unsigned integer with leading 0s.
     78     NotCanonical,
     79 }
     80 impl Display for PortParseErr {
     81     #[inline]
     82     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
     83         match *self {
     84             Self::ParseInt(ref err) => err.fmt(f),
     85             Self::NotCanonical => {
     86                 f.write_str("string was a valid TCP/UDP port number, but it had leading 0s")
     87             }
     88         }
     89     }
     90 }
     91 impl Error for PortParseErr {}
     92 /// Error returned by [`DomainOrigin::try_from`].
     93 #[derive(Debug, Eq, PartialEq)]
     94 pub enum DomainOriginParseErr {
     95     /// Variant returned when there is an error parsing the scheme.
     96     Scheme(SchemeParseErr),
     97     /// Variant returned when there is an error parsing the port.
     98     Port(PortParseErr),
     99 }
    100 impl Display for DomainOriginParseErr {
    101     #[inline]
    102     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    103         match *self {
    104             Self::Scheme(err) => err.fmt(f),
    105             Self::Port(ref err) => err.fmt(f),
    106         }
    107     }
    108 }
    109 impl Error for DomainOriginParseErr {}