implement Error for stanza crate error types

This commit is contained in:
cel 🌸 2025-02-25 19:11:25 +00:00
parent 3c412ea6b0
commit 4fe4ab9d83
3 changed files with 55 additions and 5 deletions

View File

@ -6,3 +6,4 @@ edition = "2021"
[dependencies]
peanuts = { version = "0.1.0", path = "../../peanuts" }
jid = { version = "0.1.0", path = "../jid" }
thiserror = "2.0.11"

View File

@ -4,32 +4,55 @@ use peanuts::{
element::{FromElement, IntoElement},
Element, XML_NS,
};
use thiserror::Error;
pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-stanzas";
#[derive(Clone, Debug)]
#[derive(Error, Clone, Debug)]
pub enum Error {
#[error("bad request")]
BadRequest,
#[error("conflict")]
Conflict,
#[error("feature not implemented")]
FeatureNotImplemented,
#[error("forbidden")]
Forbidden,
#[error("gone: {0:?}")]
Gone(Option<String>),
#[error("internal server error")]
InternalServerError,
#[error("item not found")]
ItemNotFound,
JidMalformed,
#[error("JID malformed")]
JIDMalformed,
#[error("not acceptable")]
NotAcceptable,
#[error("not allowed")]
NotAllowed,
#[error("not authorized")]
NotAuthorized,
#[error("policy violation")]
PolicyViolation,
#[error("recipient unavailable")]
RecipientUnavailable,
#[error("redirect: {0:?}")]
Redirect(Option<String>),
#[error("registration required")]
RegistrationRequired,
#[error("remote server not found")]
RemoteServerNotFound,
#[error("remote server timeout")]
RemoteServerTimeout,
#[error("resource constraint")]
ResourceConstraint,
#[error("service unavailable")]
ServiceUnavailable,
#[error("subscription required")]
SubscriptionRequired,
#[error("undefined condition")]
UndefinedCondition,
#[error("unexpected request")]
UnexpectedRequest,
}
@ -44,7 +67,7 @@ impl FromElement for Error {
(Some(XMLNS), "gone") => return Ok(Error::Gone(element.pop_value_opt()?)),
(Some(XMLNS), "internal-server-error") => error = Error::InternalServerError,
(Some(XMLNS), "item-not-found") => error = Error::ItemNotFound,
(Some(XMLNS), "jid-malformed") => error = Error::JidMalformed,
(Some(XMLNS), "jid-malformed") => error = Error::JIDMalformed,
(Some(XMLNS), "not-acceptable") => error = Error::NotAcceptable,
(Some(XMLNS), "not-allowed") => error = Error::NotAllowed,
(Some(XMLNS), "not-authorized") => error = Error::NotAuthorized,
@ -78,7 +101,7 @@ impl IntoElement for Error {
Error::Gone(r) => Element::builder("gone", Some(XMLNS)).push_text_opt(r.clone()),
Error::InternalServerError => Element::builder("internal-server-error", Some(XMLNS)),
Error::ItemNotFound => Element::builder("item-not-found", Some(XMLNS)),
Error::JidMalformed => Element::builder("jid-malformed", Some(XMLNS)),
Error::JIDMalformed => Element::builder("jid-malformed", Some(XMLNS)),
Error::NotAcceptable => Element::builder("not-acceptable", Some(XMLNS)),
Error::NotAllowed => Element::builder("not-allowed", Some(XMLNS)),
Error::NotAuthorized => Element::builder("not-authorized", Some(XMLNS)),

View File

@ -2,35 +2,61 @@ use peanuts::{
element::{FromElement, IntoElement},
DeserializeError, Element, XML_NS,
};
use thiserror::Error;
pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-streams";
#[derive(Clone, Debug)]
#[derive(Error, Clone, Debug)]
pub enum Error {
#[error("bad format")]
BadFormat,
#[error("bad namespace prefix")]
BadNamespacePrefix,
#[error("conflict")]
Conflict,
#[error("connection timeout")]
ConnectionTimeout,
#[error("host gone")]
HostGone,
#[error("host unknown")]
HostUnknown,
#[error("improper addressing")]
ImproperAddressing,
#[error("internal server error")]
InternalServerError,
#[error("invalid from")]
InvalidFrom,
#[error("invalid id")]
InvalidId,
#[error("invalid namespace")]
InvalidNamespace,
#[error("invalid xml")]
InvalidXml,
#[error("not authorized")]
NotAuthorized,
#[error("not well formed")]
NotWellFormed,
#[error("policy violation")]
PolicyViolation,
#[error("remote connection failed")]
RemoteConnectionFailed,
#[error("reset")]
Reset,
#[error("resource constraint")]
ResourceConstraint,
#[error("restricted xml")]
RestrictedXml,
#[error("see other host: {0:?}")]
SeeOtherHost(Option<String>),
#[error("system shutdown")]
SystemShutdown,
#[error("undefined condition")]
UndefinedCondition,
#[error("unsupported encoding")]
UnsupportedEncoding,
#[error("unsupported stanza type")]
UnsupportedStanzaType,
#[error("unsupported version")]
UnsupportedVersion,
}