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