diff options
| author | 2025-02-25 20:30:44 +0000 | |
|---|---|---|
| committer | 2025-02-25 20:30:44 +0000 | |
| commit | 53ea2951ae49d1617a6a7de760d391fe0ea2b7a9 (patch) | |
| tree | 17e8484a9bc6b05e320d77dd1c3f668af41c3006 /stanza/src/client | |
| parent | b859cd7f78495da90b947febabefdff82c02deb9 (diff) | |
| download | luz-53ea2951ae49d1617a6a7de760d391fe0ea2b7a9.tar.gz luz-53ea2951ae49d1617a6a7de760d391fe0ea2b7a9.tar.bz2 luz-53ea2951ae49d1617a6a7de760d391fe0ea2b7a9.zip | |
implement Error for stanza crate error types
Diffstat (limited to '')
| -rw-r--r-- | stanza/src/client/error.rs | 44 | 
1 files changed, 31 insertions, 13 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(), -        } -    } -} | 
