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.
This commit is contained in:
Emmanuel Gil Peyrot 2023-06-20 12:51:37 +02:00
parent 2a3d393ad5
commit 1904f0af6c
3 changed files with 51 additions and 51 deletions

View file

@ -13,7 +13,7 @@ use std::fmt;
/// An error that signifies that a `Jid` cannot be parsed from a string. /// An error that signifies that a `Jid` cannot be parsed from a string.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum JidParseError { pub enum Error {
/// Happens when there is no domain, that is either the string is empty, /// Happens when there is no domain, that is either the string is empty,
/// starts with a /, or contains the @/ sequence. /// starts with a /, or contains the @/ sequence.
NoDomain, NoDomain,
@ -49,22 +49,22 @@ pub enum JidParseError {
ResourceInBareJid, 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 { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(match self { fmt.write_str(match self {
JidParseError::NoDomain => "no domain found in this JID", Error::NoDomain => "no domain found in this JID",
JidParseError::NoResource => "no resource found in this full JID", Error::NoResource => "no resource found in this full JID",
JidParseError::EmptyNode => "nodepart empty despite the presence of a @", Error::EmptyNode => "nodepart empty despite the presence of a @",
JidParseError::EmptyResource => "resource empty despite the presence of a /", Error::EmptyResource => "resource empty despite the presence of a /",
JidParseError::NodeTooLong => "localpart longer than 1023 bytes", Error::NodeTooLong => "localpart longer than 1023 bytes",
JidParseError::DomainTooLong => "domain longer than 1023 bytes", Error::DomainTooLong => "domain longer than 1023 bytes",
JidParseError::ResourceTooLong => "resource longer than 1023 bytes", Error::ResourceTooLong => "resource longer than 1023 bytes",
JidParseError::NodePrep => "localpart doesnt pass nodeprep validation", Error::NodePrep => "localpart doesnt pass nodeprep validation",
JidParseError::NamePrep => "domain doesnt pass nameprep validation", Error::NamePrep => "domain doesnt pass nameprep validation",
JidParseError::ResourcePrep => "resource doesnt pass resourceprep validation", Error::ResourcePrep => "resource doesnt pass resourceprep validation",
JidParseError::ResourceInBareJid => "resource found while parsing a bare JID", Error::ResourceInBareJid => "resource found while parsing a bare JID",
}) })
} }
} }

View file

@ -10,7 +10,7 @@
//! //!
//! For usage, check the documentation on the `Jid` struct. //! For usage, check the documentation on the `Jid` struct.
use crate::JidParseError as Error; use crate::Error;
use core::num::NonZeroU16; use core::num::NonZeroU16;
use memchr::memchr; use memchr::memchr;
use std::str::FromStr; use std::str::FromStr;

View file

@ -24,7 +24,7 @@ use stringprep::resourceprep;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
mod error; mod error;
pub use crate::error::JidParseError; pub use crate::error::Error;
mod inner; mod inner;
use inner::InnerJid; use inner::InnerJid;
@ -42,9 +42,9 @@ pub enum Jid {
} }
impl FromStr for Jid { impl FromStr for Jid {
type Err = JidParseError; type Err = Error;
fn from_str(s: &str) -> Result<Jid, JidParseError> { fn from_str(s: &str) -> Result<Jid, Error> {
Jid::new(s) Jid::new(s)
} }
} }
@ -94,9 +94,9 @@ impl Jid {
/// ///
/// ``` /// ```
/// use jid::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")?; /// let jid = Jid::new("node@domain/resource")?;
/// ///
/// assert_eq!(jid.node(), Some("node")); /// assert_eq!(jid.node(), Some("node"));
@ -105,7 +105,7 @@ impl Jid {
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
pub fn new(s: &str) -> Result<Jid, JidParseError> { pub fn new(s: &str) -> Result<Jid, Error> {
let inner = InnerJid::new(s)?; let inner = InnerJid::new(s)?;
if inner.slash.is_some() { if inner.slash.is_some() {
Ok(Jid::Full(FullJid { inner })) Ok(Jid::Full(FullJid { inner }))
@ -153,12 +153,12 @@ impl Jid {
} }
impl TryFrom<Jid> for FullJid { impl TryFrom<Jid> for FullJid {
type Error = JidParseError; type Error = Error;
fn try_from(jid: Jid) -> Result<Self, Self::Error> { fn try_from(jid: Jid) -> Result<Self, Self::Error> {
match jid { match jid {
Jid::Full(full) => Ok(full), 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 { impl FromStr for FullJid {
type Err = JidParseError; type Err = Error;
fn from_str(s: &str) -> Result<FullJid, JidParseError> { fn from_str(s: &str) -> Result<FullJid, Error> {
FullJid::new(s) FullJid::new(s)
} }
} }
@ -335,9 +335,9 @@ impl FullJid {
/// ///
/// ``` /// ```
/// use jid::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")?; /// let jid = FullJid::new("node@domain/resource")?;
/// ///
/// assert_eq!(jid.node(), Some("node")); /// assert_eq!(jid.node(), Some("node"));
@ -346,12 +346,12 @@ impl FullJid {
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
pub fn new(s: &str) -> Result<FullJid, JidParseError> { pub fn new(s: &str) -> Result<FullJid, Error> {
let inner = InnerJid::new(s)?; let inner = InnerJid::new(s)?;
if inner.slash.is_some() { if inner.slash.is_some() {
Ok(FullJid { inner }) Ok(FullJid { inner })
} else { } else {
Err(JidParseError::NoResource) Err(Error::NoResource)
} }
} }
@ -393,9 +393,9 @@ impl FullJid {
} }
impl FromStr for BareJid { impl FromStr for BareJid {
type Err = JidParseError; type Err = Error;
fn from_str(s: &str) -> Result<BareJid, JidParseError> { fn from_str(s: &str) -> Result<BareJid, Error> {
BareJid::new(s) BareJid::new(s)
} }
} }
@ -409,9 +409,9 @@ impl BareJid {
/// ///
/// ``` /// ```
/// use jid::BareJid; /// use jid::BareJid;
/// # use jid::JidParseError; /// # use jid::Error;
/// ///
/// # fn main() -> Result<(), JidParseError> { /// # fn main() -> Result<(), Error> {
/// let jid = BareJid::new("node@domain")?; /// let jid = BareJid::new("node@domain")?;
/// ///
/// assert_eq!(jid.node(), Some("node")); /// assert_eq!(jid.node(), Some("node"));
@ -419,12 +419,12 @@ impl BareJid {
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
pub fn new(s: &str) -> Result<BareJid, JidParseError> { pub fn new(s: &str) -> Result<BareJid, Error> {
let inner = InnerJid::new(s)?; let inner = InnerJid::new(s)?;
if inner.slash.is_none() { if inner.slash.is_none() {
Ok(BareJid { inner }) Ok(BareJid { inner })
} else { } else {
Err(JidParseError::ResourceInBareJid) Err(Error::ResourceInBareJid)
} }
} }
@ -452,8 +452,8 @@ impl BareJid {
/// assert_eq!(full.domain(), "domain"); /// assert_eq!(full.domain(), "domain");
/// assert_eq!(full.resource(), "resource"); /// assert_eq!(full.resource(), "resource");
/// ``` /// ```
pub fn with_resource(&self, resource: &str) -> Result<FullJid, JidParseError> { pub fn with_resource(&self, resource: &str) -> Result<FullJid, Error> {
let resource = resourceprep(resource).map_err(|_| JidParseError::ResourcePrep)?; let resource = resourceprep(resource).map_err(|_| Error::ResourcePrep)?;
let slash = NonZeroU16::new(self.inner.normalized.len() as u16); let slash = NonZeroU16::new(self.inner.normalized.len() as u16);
let normalized = format!("{}/{resource}", self.inner.normalized); let normalized = format!("{}/{resource}", self.inner.normalized);
let inner = InnerJid { let inner = InnerJid {
@ -549,8 +549,8 @@ mod tests {
Ok(FullJid::new("b.c/d").unwrap()) Ok(FullJid::new("b.c/d").unwrap())
); );
assert_eq!(FullJid::from_str("a@b.c"), Err(JidParseError::NoResource)); assert_eq!(FullJid::from_str("a@b.c"), Err(Error::NoResource));
assert_eq!(FullJid::from_str("b.c"), Err(JidParseError::NoResource)); assert_eq!(FullJid::from_str("b.c"), Err(Error::NoResource));
} }
#[test] #[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::Full(full.clone())), Ok(full.clone()));
assert_eq!( assert_eq!(
FullJid::try_from(Jid::Bare(bare.clone())), 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(full.clone().to_bare()), bare.clone());
assert_eq!(Jid::Bare(bare.clone()), bare); assert_eq!(Jid::Bare(bare.clone()), bare);
@ -631,18 +631,18 @@ mod tests {
#[test] #[test]
fn invalid_jids() { fn invalid_jids() {
assert_eq!(BareJid::from_str(""), Err(JidParseError::NoDomain)); assert_eq!(BareJid::from_str(""), Err(Error::NoDomain));
assert_eq!(BareJid::from_str("/c"), Err(JidParseError::NoDomain)); assert_eq!(BareJid::from_str("/c"), Err(Error::NoDomain));
assert_eq!(BareJid::from_str("a@/c"), Err(JidParseError::NoDomain)); assert_eq!(BareJid::from_str("a@/c"), Err(Error::NoDomain));
assert_eq!(BareJid::from_str("@b"), Err(JidParseError::EmptyNode)); assert_eq!(BareJid::from_str("@b"), Err(Error::EmptyNode));
assert_eq!(BareJid::from_str("b/"), Err(JidParseError::EmptyResource)); assert_eq!(BareJid::from_str("b/"), Err(Error::EmptyResource));
assert_eq!(FullJid::from_str(""), Err(JidParseError::NoDomain)); assert_eq!(FullJid::from_str(""), Err(Error::NoDomain));
assert_eq!(FullJid::from_str("/c"), Err(JidParseError::NoDomain)); assert_eq!(FullJid::from_str("/c"), Err(Error::NoDomain));
assert_eq!(FullJid::from_str("a@/c"), Err(JidParseError::NoDomain)); assert_eq!(FullJid::from_str("a@/c"), Err(Error::NoDomain));
assert_eq!(FullJid::from_str("@b"), Err(JidParseError::EmptyNode)); assert_eq!(FullJid::from_str("@b"), Err(Error::EmptyNode));
assert_eq!(FullJid::from_str("b/"), Err(JidParseError::EmptyResource)); assert_eq!(FullJid::from_str("b/"), Err(Error::EmptyResource));
assert_eq!(FullJid::from_str("a@b"), Err(JidParseError::NoResource)); assert_eq!(FullJid::from_str("a@b"), Err(Error::NoResource));
} }
#[test] #[test]