diff options
| author | 2025-02-25 19:45:20 +0000 | |
|---|---|---|
| committer | 2025-02-25 19:45:20 +0000 | |
| commit | 90a5af5c751e5d4f4dcbe01f9df695708229235d (patch) | |
| tree | 1fb1d315dfad1828be761a03f53844a5175c1d7b /stanza/src | |
| parent | 4fe4ab9d838ff967395ebaf79145163d28e7db74 (diff) | |
| download | luz-90a5af5c751e5d4f4dcbe01f9df695708229235d.tar.gz luz-90a5af5c751e5d4f4dcbe01f9df695708229235d.tar.bz2 luz-90a5af5c751e5d4f4dcbe01f9df695708229235d.zip | |
implement Error for stanza crate sasl error types
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>,  } | 
