jid: Move JidParseError into its own module

This commit is contained in:
Emmanuel Gil Peyrot 2023-06-19 23:54:08 +02:00
parent d867d8d7a1
commit 187e156b0b
2 changed files with 75 additions and 61 deletions

73
jid/src/error.rs Normal file
View file

@ -0,0 +1,73 @@
// Copyright (c) 2017, 2018 lumi <lumi@pew.im>
// Copyright (c) 2017, 2018, 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
// Copyright (c) 2017, 2018, 2019 Maxime “pep” Buquet <pep@bouah.net>
// Copyright (c) 2017, 2018 Astro <astro@spaceboyz.net>
// Copyright (c) 2017 Bastien Orivel <eijebong@bananium.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::error::Error as StdError;
use std::fmt;
/// An error that signifies that a `Jid` cannot be parsed from a string.
#[derive(Debug)]
pub enum JidParseError {
/// Happens when there is no domain, that is either the string is empty,
/// starts with a /, or contains the @/ sequence.
NoDomain,
/// Happens when there is no resource, that is string contains no /.
NoResource,
/// Happens when the node is empty, that is the string starts with a @.
EmptyNode,
/// Happens when the resource is empty, that is the string ends with a /.
EmptyResource,
/// Happens when the JID is invalid according to stringprep. TODO: make errors
/// meaningful.
Stringprep(stringprep::Error),
}
impl From<stringprep::Error> for JidParseError {
fn from(e: stringprep::Error) -> JidParseError {
JidParseError::Stringprep(e)
}
}
impl PartialEq for JidParseError {
fn eq(&self, other: &JidParseError) -> bool {
use JidParseError as E;
match (self, other) {
(E::NoDomain, E::NoDomain) => true,
(E::NoResource, E::NoResource) => true,
(E::EmptyNode, E::EmptyNode) => true,
(E::EmptyResource, E::EmptyResource) => true,
(E::Stringprep(_), E::Stringprep(_)) => true, // TODO: fix that upstream.
_ => false,
}
}
}
impl Eq for JidParseError {}
impl StdError for JidParseError {}
impl fmt::Display for JidParseError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(
fmt,
"{}",
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::Stringprep(_err) => "TODO",
}
)
}
}

View file

@ -15,7 +15,6 @@
//! For usage, check the documentation on the `Jid` struct. //! For usage, check the documentation on the `Jid` struct.
use std::convert::{Into, TryFrom}; use std::convert::{Into, TryFrom};
use std::error::Error as StdError;
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
use stringprep::{nameprep, nodeprep, resourceprep}; use stringprep::{nameprep, nodeprep, resourceprep};
@ -23,66 +22,8 @@ use stringprep::{nameprep, nodeprep, resourceprep};
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
/// An error that signifies that a `Jid` cannot be parsed from a string. mod error;
#[derive(Debug)] pub use crate::error::JidParseError;
pub enum JidParseError {
/// Happens when there is no domain, that is either the string is empty,
/// starts with a /, or contains the @/ sequence.
NoDomain,
/// Happens when there is no resource, that is string contains no /.
NoResource,
/// Happens when the node is empty, that is the string starts with a @.
EmptyNode,
/// Happens when the resource is empty, that is the string ends with a /.
EmptyResource,
/// Happens when the JID is invalid according to stringprep. TODO: make errors
/// meaningful.
Stringprep(stringprep::Error),
}
impl From<stringprep::Error> for JidParseError {
fn from(e: stringprep::Error) -> JidParseError {
JidParseError::Stringprep(e)
}
}
impl PartialEq for JidParseError {
fn eq(&self, other: &JidParseError) -> bool {
use JidParseError as E;
match (self, other) {
(E::NoDomain, E::NoDomain) => true,
(E::NoResource, E::NoResource) => true,
(E::EmptyNode, E::EmptyNode) => true,
(E::EmptyResource, E::EmptyResource) => true,
(E::Stringprep(_), E::Stringprep(_)) => false, // TODO: fix that.
_ => false,
}
}
}
impl Eq for JidParseError {}
impl StdError for JidParseError {}
impl fmt::Display for JidParseError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(
fmt,
"{}",
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::Stringprep(_err) => "TODO",
}
)
}
}
/// An enum representing a Jabber ID. It can be either a `FullJid` or a `BareJid`. /// An enum representing a Jabber ID. It can be either a `FullJid` or a `BareJid`.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]