diff options
Diffstat (limited to '')
| -rw-r--r-- | stanza/src/sasl.rs | 57 | 
1 files changed, 53 insertions, 4 deletions
| diff --git a/stanza/src/sasl.rs b/stanza/src/sasl.rs index 0a3f06f..598a91b 100644 --- a/stanza/src/sasl.rs +++ b/stanza/src/sasl.rs @@ -1,9 +1,10 @@ -use std::ops::Deref; +use std::{fmt::Display, ops::Deref};  use peanuts::{      element::{FromElement, IntoElement},      DeserializeError, Element,  }; +use thiserror::Error;  pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-sasl"; @@ -168,12 +169,48 @@ impl IntoElement for Response {      }  } -#[derive(Debug)] +#[derive(Error, Debug, Clone)]  pub struct Failure {      r#type: Option<FailureType>,      text: Option<Text>,  } +impl Display for Failure { +    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +        let mut had_type = false; +        let mut had_text = false; +        if let Some(r#type) = &self.r#type { +            had_type = true; +            match r#type { +                FailureType::Aborted => f.write_str("aborted"), +                FailureType::AccountDisabled => f.write_str("account disabled"), +                FailureType::CredentialsExpired => f.write_str("credentials expired"), +                FailureType::EncryptionRequired => f.write_str("encryption required"), +                FailureType::IncorrectEncoding => f.write_str("incorrect encoding"), +                FailureType::InvalidAuthzid => f.write_str("invalid authzid"), +                FailureType::InvalidMechanism => f.write_str("invalid mechanism"), +                FailureType::MalformedRequest => f.write_str("malformed request"), +                FailureType::MechanismTooWeak => f.write_str("mechanism too weak"), +                FailureType::NotAuthorized => f.write_str("not authorized"), +                FailureType::TemporaryAuthFailure => f.write_str("temporary auth failure"), +            }?; +        } +        if let Some(text) = &self.text { +            if let Some(text) = &text.text { +                if had_type { +                    f.write_str(": ")?; +                } +                f.write_str(text)?; +                had_text = true; +            } +        } +        if !had_type && !had_text { +            f.write_str("failure")?; +        } +        Ok(()) +    } +} +  impl FromElement for Failure {      fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> {          element.check_name("failure")?; @@ -186,18 +223,29 @@ impl FromElement for Failure {      }  } -#[derive(Debug)] +#[derive(Error, Debug, Clone)]  pub enum FailureType { +    #[error("aborted")]      Aborted, +    #[error("account disabled")]      AccountDisabled, +    #[error("credentials expired")]      CredentialsExpired, +    #[error("encryption required")]      EncryptionRequired, +    #[error("incorrect encoding")]      IncorrectEncoding, +    #[error("invalid authzid")]      InvalidAuthzid, +    #[error("invalid mechanism")]      InvalidMechanism, +    #[error("malformed request")]      MalformedRequest, +    #[error("mechanism too weak")]      MechanismTooWeak, +    #[error("not authorized")]      NotAuthorized, +    #[error("temporary auth failure")]      TemporaryAuthFailure,  } @@ -220,8 +268,9 @@ impl FromElement for FailureType {      }  } -#[derive(Debug)] +#[derive(Debug, Clone)]  pub struct Text { +    #[allow(dead_code)]      lang: Option<String>,      text: Option<String>,  } | 
