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 /// Happens when parsing a JID which has two @ before the resource.
51 TooManyAts,
52
53 /// Happens when the domain is invalid according to idna.
54 Idna,
55}
56
57impl core::error::Error for Error {}
58
59impl fmt::Display for Error {
60 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
61 fmt.write_str(match self {
62 Error::NodeEmpty => "nodepart empty despite the presence of a @",
63 Error::DomainEmpty => "no domain found in this JID",
64 Error::ResourceEmpty => "resource empty despite the presence of a /",
65 Error::NodeTooLong => "localpart longer than 1023 bytes",
66 Error::DomainTooLong => "domain longer than 1023 bytes",
67 Error::ResourceTooLong => "resource longer than 1023 bytes",
68 Error::NodePrep => "localpart doesn’t pass nodeprep validation",
69 Error::NamePrep => "domain doesn’t pass nameprep validation",
70 Error::ResourcePrep => "resource doesn’t pass resourceprep validation",
71 Error::ResourceMissingInFullJid => "no resource found in this full JID",
72 Error::ResourceInBareJid => "resource found while parsing a bare JID",
73 Error::TooManyAts => "second @ found before parsing the resource",
74 Error::Idna => "domain doesn’t pass idna validation",
75 })
76 }
77}