diff options
Diffstat (limited to '')
| -rw-r--r-- | stanza/src/client/error.rs | 44 | ||||
| -rw-r--r-- | stanza/src/stanza_error.rs | 2 | ||||
| -rw-r--r-- | stanza/src/stream.rs | 17 | ||||
| -rw-r--r-- | stanza/src/stream_error.rs | 2 | 
4 files changed, 49 insertions, 16 deletions
diff --git a/stanza/src/client/error.rs b/stanza/src/client/error.rs index 689953a..aa142bf 100644 --- a/stanza/src/client/error.rs +++ b/stanza/src/client/error.rs @@ -1,14 +1,16 @@ +use std::fmt::Display;  use std::str::FromStr;  use peanuts::element::{FromElement, IntoElement};  use peanuts::{DeserializeError, Element}; +use thiserror::Error;  use crate::stanza_error::Error as StanzaError;  use crate::stanza_error::Text;  use super::XMLNS; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Error)]  pub struct Error {      by: Option<String>,      r#type: ErrorType, @@ -17,6 +19,22 @@ pub struct Error {      text: Option<Text>,  } +impl Display for Error { +    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +        write!(f, "{}, {}", self.r#type, self.error)?; + +        if let Some(text) = &self.text { +            if let Some(text) = &text.text { +                write!(f, ": {}", text)?; +            } +        } +        if let Some(by) = &self.by { +            write!(f, " (error returned by `{}`)", by)?; +        } +        Ok(()) +    } +} +  impl FromElement for Error {      fn from_element(mut element: peanuts::Element) -> peanuts::element::DeserializeResult<Self> {          element.check_name("error")?; @@ -55,6 +73,18 @@ pub enum ErrorType {      Wait,  } +impl Display for ErrorType { +    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +        match self { +            ErrorType::Auth => f.write_str("auth"), +            ErrorType::Cancel => f.write_str("cancel"), +            ErrorType::Continue => f.write_str("continue"), +            ErrorType::Modify => f.write_str("modify"), +            ErrorType::Wait => f.write_str("wait"), +        } +    } +} +  impl FromStr for ErrorType {      type Err = DeserializeError; @@ -69,15 +99,3 @@ impl FromStr for ErrorType {          }      }  } - -impl ToString for ErrorType { -    fn to_string(&self) -> String { -        match self { -            ErrorType::Auth => "auth".to_string(), -            ErrorType::Cancel => "cancel".to_string(), -            ErrorType::Continue => "continue".to_string(), -            ErrorType::Modify => "modify".to_string(), -            ErrorType::Wait => "wait".to_string(), -        } -    } -} diff --git a/stanza/src/stanza_error.rs b/stanza/src/stanza_error.rs index 6aec98c..a4b58bc 100644 --- a/stanza/src/stanza_error.rs +++ b/stanza/src/stanza_error.rs @@ -125,7 +125,7 @@ impl IntoElement for Error {  #[derive(Clone, Debug)]  pub struct Text {      lang: Option<String>, -    text: Option<String>, +    pub text: Option<String>,  }  impl FromElement for Text { diff --git a/stanza/src/stream.rs b/stanza/src/stream.rs index 069dcd1..60b13bc 100644 --- a/stanza/src/stream.rs +++ b/stanza/src/stream.rs @@ -1,6 +1,9 @@ +use std::fmt::Display; +  use jid::JID;  use peanuts::element::{ElementBuilder, FromElement, IntoElement};  use peanuts::Element; +use thiserror::Error;  use crate::bind; @@ -176,12 +179,24 @@ impl FromElement for Feature {      }  } -#[derive(Debug)] +#[derive(Error, Debug)]  pub struct Error {      error: StreamError,      text: Option<Text>,  } +impl Display for Error { +    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +        write!(f, "{}", self.error)?; +        if let Some(text) = &self.text { +            if let Some(text) = &text.text { +                write!(f, ": {}", text)?; +            } +        } +        Ok(()) +    } +} +  impl FromElement for Error {      fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> {          element.check_name("error")?; diff --git a/stanza/src/stream_error.rs b/stanza/src/stream_error.rs index c1537ff..03d48eb 100644 --- a/stanza/src/stream_error.rs +++ b/stanza/src/stream_error.rs @@ -138,7 +138,7 @@ impl IntoElement for Error {  #[derive(Clone, Debug)]  pub struct Text { -    text: Option<String>, +    pub text: Option<String>,      lang: Option<String>,  }  | 
