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 JidParseError {
17 /// Happens when there is no domain, that is either the string is empty,
18 /// starts with a /, or contains the @/ sequence.
19 NoDomain,
20
21 /// Happens when there is no resource, that is string contains no /.
22 NoResource,
23
24 /// Happens when the node is empty, that is the string starts with a @.
25 EmptyNode,
26
27 /// Happens when the resource is empty, that is the string ends with a /.
28 EmptyResource,
29
30 /// Happens when the localpart is longer than 1023 bytes.
31 NodeTooLong,
32
33 /// Happens when the domain is longer than 1023 bytes.
34 DomainTooLong,
35
36 /// Happens when the resource is longer than 1023 bytes.
37 ResourceTooLong,
38
39 /// Happens when the localpart is invalid according to nodeprep.
40 NodePrep,
41
42 /// Happens when the domain is invalid according to nameprep.
43 NamePrep,
44
45 /// Happens when the resource is invalid according to resourceprep.
46 ResourcePrep,
47
48 /// Happens when parsing a bare JID and there is a resource.
49 ResourceInBareJid,
50}
51
52impl StdError for JidParseError {}
53
54impl fmt::Display for JidParseError {
55 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
56 fmt.write_str(match self {
57 JidParseError::NoDomain => "no domain found in this JID",
58 JidParseError::NoResource => "no resource found in this full JID",
59 JidParseError::EmptyNode => "nodepart empty despite the presence of a @",
60 JidParseError::EmptyResource => "resource empty despite the presence of a /",
61 JidParseError::NodeTooLong => "localpart longer than 1023 bytes",
62 JidParseError::DomainTooLong => "domain longer than 1023 bytes",
63 JidParseError::ResourceTooLong => "resource longer than 1023 bytes",
64 JidParseError::NodePrep => "localpart doesn’t pass nodeprep validation",
65 JidParseError::NamePrep => "domain doesn’t pass nameprep validation",
66 JidParseError::ResourcePrep => "resource doesn’t pass resourceprep validation",
67 JidParseError::ResourceInBareJid => "resource found while parsing a bare JID",
68 })
69 }
70}