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 core::fmt;
12
13/// An error that signifies that a `Jid` cannot be parsed from a string.
14#[derive(Debug, PartialEq, Eq)]
15pub enum Error {
16    /// Happens when the node is empty, that is the string starts with a @.
17    NodeEmpty,
18
19    /// Happens when there is no domain, that is either the string is empty,
20    /// starts with a /, or contains the @/ sequence.
21    DomainEmpty,
22
23    /// Happens when the resource is empty, that is the string ends with a /.
24    ResourceEmpty,
25
26    /// Happens when the localpart is longer than 1023 bytes.
27    NodeTooLong,
28
29    /// Happens when the domain is longer than 1023 bytes.
30    DomainTooLong,
31
32    /// Happens when the resource is longer than 1023 bytes.
33    ResourceTooLong,
34
35    /// Happens when the localpart is invalid according to nodeprep.
36    NodePrep,
37
38    /// Happens when the domain is invalid according to nameprep.
39    NamePrep,
40
41    /// Happens when the resource is invalid according to resourceprep.
42    ResourcePrep,
43
44    /// Happens when there is no resource, that is string contains no /.
45    ResourceMissingInFullJid,
46
47    /// Happens when parsing a bare JID and there is a resource.
48    ResourceInBareJid,
49}
50
51impl core::error::Error for Error {}
52
53impl fmt::Display for Error {
54    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
55        fmt.write_str(match self {
56            Error::NodeEmpty => "nodepart empty despite the presence of a @",
57            Error::DomainEmpty => "no domain found in this JID",
58            Error::ResourceEmpty => "resource empty despite the presence of a /",
59            Error::NodeTooLong => "localpart longer than 1023 bytes",
60            Error::DomainTooLong => "domain longer than 1023 bytes",
61            Error::ResourceTooLong => "resource longer than 1023 bytes",
62            Error::NodePrep => "localpart doesn’t pass nodeprep validation",
63            Error::NamePrep => "domain doesn’t pass nameprep validation",
64            Error::ResourcePrep => "resource doesn’t pass resourceprep validation",
65            Error::ResourceMissingInFullJid => "no resource found in this full JID",
66            Error::ResourceInBareJid => "resource found while parsing a bare JID",
67        })
68    }
69}