implement Error for stanza crate error types
This commit is contained in:
parent
b859cd7f78
commit
53ea2951ae
|
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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")?;
|
||||
|
|
|
@ -138,7 +138,7 @@ impl IntoElement for Error {
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Text {
|
||||
text: Option<String>,
|
||||
pub text: Option<String>,
|
||||
lang: Option<String>,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue