From 9ff65ae77a6026660847707625740e3cff98acb3 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 20 Jun 2023 12:51:37 +0200 Subject: [PATCH] jid: Rename error into Error JidParseError is an ok name when imported elsewhere, but inside of this crate it makes much more sense to name it Error. --- jid/src/error.rs | 28 +++++++++---------- jid/src/inner.rs | 2 +- jid/src/lib.rs | 72 ++++++++++++++++++++++++------------------------ 3 files changed, 51 insertions(+), 51 deletions(-) diff --git a/jid/src/error.rs b/jid/src/error.rs index 333c701..74fbf44 100644 --- a/jid/src/error.rs +++ b/jid/src/error.rs @@ -13,7 +13,7 @@ use std::fmt; /// An error that signifies that a `Jid` cannot be parsed from a string. #[derive(Debug, PartialEq, Eq)] -pub enum JidParseError { +pub enum Error { /// Happens when there is no domain, that is either the string is empty, /// starts with a /, or contains the @/ sequence. NoDomain, @@ -49,22 +49,22 @@ pub enum JidParseError { ResourceInBareJid, } -impl StdError for JidParseError {} +impl StdError for Error {} -impl fmt::Display for JidParseError { +impl fmt::Display for Error { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.write_str(match self { - JidParseError::NoDomain => "no domain found in this JID", - JidParseError::NoResource => "no resource found in this full JID", - JidParseError::EmptyNode => "nodepart empty despite the presence of a @", - JidParseError::EmptyResource => "resource empty despite the presence of a /", - JidParseError::NodeTooLong => "localpart longer than 1023 bytes", - JidParseError::DomainTooLong => "domain longer than 1023 bytes", - JidParseError::ResourceTooLong => "resource longer than 1023 bytes", - JidParseError::NodePrep => "localpart doesn’t pass nodeprep validation", - JidParseError::NamePrep => "domain doesn’t pass nameprep validation", - JidParseError::ResourcePrep => "resource doesn’t pass resourceprep validation", - JidParseError::ResourceInBareJid => "resource found while parsing a bare JID", + Error::NoDomain => "no domain found in this JID", + Error::NoResource => "no resource found in this full JID", + Error::EmptyNode => "nodepart empty despite the presence of a @", + Error::EmptyResource => "resource empty despite the presence of a /", + Error::NodeTooLong => "localpart longer than 1023 bytes", + Error::DomainTooLong => "domain longer than 1023 bytes", + Error::ResourceTooLong => "resource longer than 1023 bytes", + Error::NodePrep => "localpart doesn’t pass nodeprep validation", + Error::NamePrep => "domain doesn’t pass nameprep validation", + Error::ResourcePrep => "resource doesn’t pass resourceprep validation", + Error::ResourceInBareJid => "resource found while parsing a bare JID", }) } } diff --git a/jid/src/inner.rs b/jid/src/inner.rs index 16b77e9..5d5271d 100644 --- a/jid/src/inner.rs +++ b/jid/src/inner.rs @@ -10,7 +10,7 @@ //! //! For usage, check the documentation on the `Jid` struct. -use crate::JidParseError as Error; +use crate::Error; use core::num::NonZeroU16; use memchr::memchr; use std::str::FromStr; diff --git a/jid/src/lib.rs b/jid/src/lib.rs index c4428c2..d2125c2 100644 --- a/jid/src/lib.rs +++ b/jid/src/lib.rs @@ -24,7 +24,7 @@ use stringprep::resourceprep; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; mod error; -pub use crate::error::JidParseError; +pub use crate::error::Error; mod inner; use inner::InnerJid; @@ -42,9 +42,9 @@ pub enum Jid { } impl FromStr for Jid { - type Err = JidParseError; + type Err = Error; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { Jid::new(s) } } @@ -94,9 +94,9 @@ impl Jid { /// /// ``` /// use jid::Jid; - /// # use jid::JidParseError; + /// # use jid::Error; /// - /// # fn main() -> Result<(), JidParseError> { + /// # fn main() -> Result<(), Error> { /// let jid = Jid::new("node@domain/resource")?; /// /// assert_eq!(jid.node(), Some("node")); @@ -105,7 +105,7 @@ impl Jid { /// # Ok(()) /// # } /// ``` - pub fn new(s: &str) -> Result { + pub fn new(s: &str) -> Result { let inner = InnerJid::new(s)?; if inner.slash.is_some() { Ok(Jid::Full(FullJid { inner })) @@ -153,12 +153,12 @@ impl Jid { } impl TryFrom for FullJid { - type Error = JidParseError; + type Error = Error; fn try_from(jid: Jid) -> Result { match jid { Jid::Full(full) => Ok(full), - Jid::Bare(_) => Err(JidParseError::NoResource), + Jid::Bare(_) => Err(Error::NoResource), } } } @@ -297,9 +297,9 @@ impl Serialize for BareJid { } impl FromStr for FullJid { - type Err = JidParseError; + type Err = Error; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { FullJid::new(s) } } @@ -335,9 +335,9 @@ impl FullJid { /// /// ``` /// use jid::FullJid; - /// # use jid::JidParseError; + /// # use jid::Error; /// - /// # fn main() -> Result<(), JidParseError> { + /// # fn main() -> Result<(), Error> { /// let jid = FullJid::new("node@domain/resource")?; /// /// assert_eq!(jid.node(), Some("node")); @@ -346,12 +346,12 @@ impl FullJid { /// # Ok(()) /// # } /// ``` - pub fn new(s: &str) -> Result { + pub fn new(s: &str) -> Result { let inner = InnerJid::new(s)?; if inner.slash.is_some() { Ok(FullJid { inner }) } else { - Err(JidParseError::NoResource) + Err(Error::NoResource) } } @@ -393,9 +393,9 @@ impl FullJid { } impl FromStr for BareJid { - type Err = JidParseError; + type Err = Error; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { BareJid::new(s) } } @@ -409,9 +409,9 @@ impl BareJid { /// /// ``` /// use jid::BareJid; - /// # use jid::JidParseError; + /// # use jid::Error; /// - /// # fn main() -> Result<(), JidParseError> { + /// # fn main() -> Result<(), Error> { /// let jid = BareJid::new("node@domain")?; /// /// assert_eq!(jid.node(), Some("node")); @@ -419,12 +419,12 @@ impl BareJid { /// # Ok(()) /// # } /// ``` - pub fn new(s: &str) -> Result { + pub fn new(s: &str) -> Result { let inner = InnerJid::new(s)?; if inner.slash.is_none() { Ok(BareJid { inner }) } else { - Err(JidParseError::ResourceInBareJid) + Err(Error::ResourceInBareJid) } } @@ -452,8 +452,8 @@ impl BareJid { /// assert_eq!(full.domain(), "domain"); /// assert_eq!(full.resource(), "resource"); /// ``` - pub fn with_resource(&self, resource: &str) -> Result { - let resource = resourceprep(resource).map_err(|_| JidParseError::ResourcePrep)?; + pub fn with_resource(&self, resource: &str) -> Result { + let resource = resourceprep(resource).map_err(|_| Error::ResourcePrep)?; let slash = NonZeroU16::new(self.inner.normalized.len() as u16); let normalized = format!("{}/{resource}", self.inner.normalized); let inner = InnerJid { @@ -549,8 +549,8 @@ mod tests { Ok(FullJid::new("b.c/d").unwrap()) ); - assert_eq!(FullJid::from_str("a@b.c"), Err(JidParseError::NoResource)); - assert_eq!(FullJid::from_str("b.c"), Err(JidParseError::NoResource)); + assert_eq!(FullJid::from_str("a@b.c"), Err(Error::NoResource)); + assert_eq!(FullJid::from_str("b.c"), Err(Error::NoResource)); } #[test] @@ -606,7 +606,7 @@ mod tests { assert_eq!(FullJid::try_from(Jid::Full(full.clone())), Ok(full.clone())); assert_eq!( FullJid::try_from(Jid::Bare(bare.clone())), - Err(JidParseError::NoResource), + Err(Error::NoResource), ); assert_eq!(Jid::Bare(full.clone().to_bare()), bare.clone()); assert_eq!(Jid::Bare(bare.clone()), bare); @@ -631,18 +631,18 @@ mod tests { #[test] fn invalid_jids() { - assert_eq!(BareJid::from_str(""), Err(JidParseError::NoDomain)); - assert_eq!(BareJid::from_str("/c"), Err(JidParseError::NoDomain)); - assert_eq!(BareJid::from_str("a@/c"), Err(JidParseError::NoDomain)); - assert_eq!(BareJid::from_str("@b"), Err(JidParseError::EmptyNode)); - assert_eq!(BareJid::from_str("b/"), Err(JidParseError::EmptyResource)); + assert_eq!(BareJid::from_str(""), Err(Error::NoDomain)); + assert_eq!(BareJid::from_str("/c"), Err(Error::NoDomain)); + assert_eq!(BareJid::from_str("a@/c"), Err(Error::NoDomain)); + assert_eq!(BareJid::from_str("@b"), Err(Error::EmptyNode)); + assert_eq!(BareJid::from_str("b/"), Err(Error::EmptyResource)); - assert_eq!(FullJid::from_str(""), Err(JidParseError::NoDomain)); - assert_eq!(FullJid::from_str("/c"), Err(JidParseError::NoDomain)); - assert_eq!(FullJid::from_str("a@/c"), Err(JidParseError::NoDomain)); - assert_eq!(FullJid::from_str("@b"), Err(JidParseError::EmptyNode)); - assert_eq!(FullJid::from_str("b/"), Err(JidParseError::EmptyResource)); - assert_eq!(FullJid::from_str("a@b"), Err(JidParseError::NoResource)); + assert_eq!(FullJid::from_str(""), Err(Error::NoDomain)); + assert_eq!(FullJid::from_str("/c"), Err(Error::NoDomain)); + assert_eq!(FullJid::from_str("a@/c"), Err(Error::NoDomain)); + assert_eq!(FullJid::from_str("@b"), Err(Error::EmptyNode)); + assert_eq!(FullJid::from_str("b/"), Err(Error::EmptyResource)); + assert_eq!(FullJid::from_str("a@b"), Err(Error::NoResource)); } #[test]