error.rs

 1// Copyright (c) 2017, 2018 lumi <lumi@pew.im>
 2// Copyright (c) 2017, 2018, 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 3// Copyright (c) 2017, 2018, 2019 Maxime “pep” Buquet <pep@bouah.net>
 4// Copyright (c) 2017, 2018 Astro <astro@spaceboyz.net>
 5// Copyright (c) 2017 Bastien Orivel <eijebong@bananium.fr>
 6//
 7// This Source Code Form is subject to the terms of the Mozilla Public
 8// License, v. 2.0. If a copy of the MPL was not distributed with this
 9// file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11use std::error::Error as StdError;
12use std::fmt;
13
14/// An error that signifies that a `Jid` cannot be parsed from a string.
15#[derive(Debug, PartialEq, Eq)]
16pub enum Error {
17    /// Happens when the node is empty, that is the string starts with a @.
18    NodeEmpty,
19
20    /// Happens when there is no domain, that is either the string is empty,
21    /// starts with a /, or contains the @/ sequence.
22    DomainEmpty,
23
24    /// Happens when the resource is empty, that is the string ends with a /.
25    ResourceEmpty,
26
27    /// Happens when the localpart is longer than 1023 bytes.
28    NodeTooLong,
29
30    /// Happens when the domain is longer than 1023 bytes.
31    DomainTooLong,
32
33    /// Happens when the resource is longer than 1023 bytes.
34    ResourceTooLong,
35
36    /// Happens when the localpart is invalid according to nodeprep.
37    NodePrep,
38
39    /// Happens when the domain is invalid according to nameprep.
40    NamePrep,
41
42    /// Happens when the resource is invalid according to resourceprep.
43    ResourcePrep,
44
45    /// Happens when there is no resource, that is string contains no /.
46    ResourceMissingInFullJid,
47
48    /// Happens when parsing a bare JID and there is a resource.
49    ResourceInBareJid,
50}
51
52impl StdError for Error {}
53
54impl fmt::Display for Error {
55    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
56        fmt.write_str(match self {
57            Error::NodeEmpty => "nodepart empty despite the presence of a @",
58            Error::DomainEmpty => "no domain found in this JID",
59            Error::ResourceEmpty => "resource empty despite the presence of a /",
60            Error::NodeTooLong => "localpart longer than 1023 bytes",
61            Error::DomainTooLong => "domain longer than 1023 bytes",
62            Error::ResourceTooLong => "resource longer than 1023 bytes",
63            Error::NodePrep => "localpart doesn’t pass nodeprep validation",
64            Error::NamePrep => "domain doesn’t pass nameprep validation",
65            Error::ResourcePrep => "resource doesn’t pass resourceprep validation",
66            Error::ResourceMissingInFullJid => "no resource found in this full JID",
67            Error::ResourceInBareJid => "resource found while parsing a bare JID",
68        })
69    }
70}