implement Error for stanza crate sasl error types

This commit is contained in:
cel 🌸 2025-02-25 19:45:20 +00:00
parent 4fe4ab9d83
commit 90a5af5c75
1 changed files with 53 additions and 4 deletions

View File

@ -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>,
}