1
0
mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git synced 2024-06-12 03:04:03 +02:00
xmpp-rs/jid/src/error.rs
Emmanuel Gil Peyrot 48f77acb69 jid: Make the crate no_std
Only std::error::Error is missing in this mode, but that’s only waiting
for its stabilisation, see this issue:
https://github.com/rust-lang/rust/issues/103765
2024-05-04 10:28:25 +00:00

73 lines
2.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 core::fmt;
#[cfg(feature = "std")]
use std::error::Error as StdError;
/// An error that signifies that a `Jid` cannot be parsed from a string.
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
/// Happens when the node is empty, that is the string starts with a @.
NodeEmpty,
/// Happens when there is no domain, that is either the string is empty,
/// starts with a /, or contains the @/ sequence.
DomainEmpty,
/// Happens when the resource is empty, that is the string ends with a /.
ResourceEmpty,
/// Happens when the localpart is longer than 1023 bytes.
NodeTooLong,
/// Happens when the domain is longer than 1023 bytes.
DomainTooLong,
/// Happens when the resource is longer than 1023 bytes.
ResourceTooLong,
/// Happens when the localpart is invalid according to nodeprep.
NodePrep,
/// Happens when the domain is invalid according to nameprep.
NamePrep,
/// Happens when the resource is invalid according to resourceprep.
ResourcePrep,
/// Happens when there is no resource, that is string contains no /.
ResourceMissingInFullJid,
/// Happens when parsing a bare JID and there is a resource.
ResourceInBareJid,
}
#[cfg(feature = "std")]
impl StdError for Error {}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(match self {
Error::NodeEmpty => "nodepart empty despite the presence of a @",
Error::DomainEmpty => "no domain found in this JID",
Error::ResourceEmpty => "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 doesnt pass nodeprep validation",
Error::NamePrep => "domain doesnt pass nameprep validation",
Error::ResourcePrep => "resource doesnt pass resourceprep validation",
Error::ResourceMissingInFullJid => "no resource found in this full JID",
Error::ResourceInBareJid => "resource found while parsing a bare JID",
})
}
}