implement Error for stanza crate sasl error types
This commit is contained in:
		
							parent
							
								
									4fe4ab9d83
								
							
						
					
					
						commit
						90a5af5c75
					
				|  | @ -1,9 +1,10 @@ | ||||||
| use std::ops::Deref; | use std::{fmt::Display, ops::Deref}; | ||||||
| 
 | 
 | ||||||
| use peanuts::{ | use peanuts::{ | ||||||
|     element::{FromElement, IntoElement}, |     element::{FromElement, IntoElement}, | ||||||
|     DeserializeError, Element, |     DeserializeError, Element, | ||||||
| }; | }; | ||||||
|  | use thiserror::Error; | ||||||
| 
 | 
 | ||||||
| pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-sasl"; | 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 { | pub struct Failure { | ||||||
|     r#type: Option<FailureType>, |     r#type: Option<FailureType>, | ||||||
|     text: Option<Text>, |     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 { | impl FromElement for Failure { | ||||||
|     fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> { |     fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> { | ||||||
|         element.check_name("failure")?; |         element.check_name("failure")?; | ||||||
|  | @ -186,18 +223,29 @@ impl FromElement for Failure { | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Debug)] | #[derive(Error, Debug, Clone)] | ||||||
| pub enum FailureType { | pub enum FailureType { | ||||||
|  |     #[error("aborted")] | ||||||
|     Aborted, |     Aborted, | ||||||
|  |     #[error("account disabled")] | ||||||
|     AccountDisabled, |     AccountDisabled, | ||||||
|  |     #[error("credentials expired")] | ||||||
|     CredentialsExpired, |     CredentialsExpired, | ||||||
|  |     #[error("encryption required")] | ||||||
|     EncryptionRequired, |     EncryptionRequired, | ||||||
|  |     #[error("incorrect encoding")] | ||||||
|     IncorrectEncoding, |     IncorrectEncoding, | ||||||
|  |     #[error("invalid authzid")] | ||||||
|     InvalidAuthzid, |     InvalidAuthzid, | ||||||
|  |     #[error("invalid mechanism")] | ||||||
|     InvalidMechanism, |     InvalidMechanism, | ||||||
|  |     #[error("malformed request")] | ||||||
|     MalformedRequest, |     MalformedRequest, | ||||||
|  |     #[error("mechanism too weak")] | ||||||
|     MechanismTooWeak, |     MechanismTooWeak, | ||||||
|  |     #[error("not authorized")] | ||||||
|     NotAuthorized, |     NotAuthorized, | ||||||
|  |     #[error("temporary auth failure")] | ||||||
|     TemporaryAuthFailure, |     TemporaryAuthFailure, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -220,8 +268,9 @@ impl FromElement for FailureType { | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Debug)] | #[derive(Debug, Clone)] | ||||||
| pub struct Text { | pub struct Text { | ||||||
|  |     #[allow(dead_code)] | ||||||
|     lang: Option<String>, |     lang: Option<String>, | ||||||
|     text: Option<String>, |     text: Option<String>, | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue