phonenumber_fixed/
error.rs

1// Copyright (C) 2017 1aim GmbH
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use thiserror::Error;
16
17/// Metadata loading errors.
18#[derive(Error, Clone, Debug)]
19pub enum Metadata {
20	/// EOF was reached before the parsing was complete.
21	#[error("unexpected end of file")]
22	UnexpectedEof,
23
24	/// A mismatched tag was met.
25	#[error("mismatched tag: {0:?}")]
26	MismatchedTag(String),
27
28	/// A required value was missing.
29    #[error("{phase}: missing value: {name:?}")]
30    #[allow(unused)] // This is unused in the build script
31	MissingValue {
32		phase: String,
33		name:  String,
34	},
35
36	/// An element was not handled.
37	#[error("{phase}: unhandled element: {name:?}")]
38	UnhandledElement {
39		phase: String,
40		name:  String
41	},
42
43	/// An attribute was not handled.
44	#[error("{phase}: unhandled attribute: {name:?}={value:?}")]
45	UnhandledAttribute {
46		phase: String,
47		name:  String,
48		value: String,
49	},
50
51	/// An event was not handled.
52	#[error("{phase}: unhandled event: {event:?}")]
53	UnhandledEvent {
54		phase: String,
55		event: String,
56	}
57}
58
59/// Parsing errors.
60#[derive(Error, Clone, Debug)]
61pub enum Parse {
62	/// This generally indicates the string passed in had less than 3 digits in
63	/// it.
64    #[error("not a number")]
65    #[allow(unused)] // This is unused in the build script
66	NoNumber,
67
68	/// The country code supplied did not belong to a supported country or
69	/// non-geographical entity.
70    #[error("invalid country code")]
71    #[allow(unused)] // This is unused in the build script
72	InvalidCountryCode,
73
74	/// This indicates the string started with an international dialing prefix,
75	/// but after this was stripped from the number, had less digits than any
76	/// valid phone number (including country code) could have.
77    #[error("the number is too short after IDD")]
78    #[allow(unused)] // This is unused in the build script
79	TooShortAfterIdd,
80
81	/// This indicates the string, after any country code has been stripped, had
82	/// less digits than any valid phone number could have.
83    #[error("the number is too short after the country code")]
84    #[allow(unused)] // This is unused in the build script
85	TooShortNsn,
86
87	/// This indicates the string had more digits than any valid phone number
88	/// could have.
89    #[error("the number is too long")]
90    #[allow(unused)] // This is unused in the build script
91    TooLong,
92
93    /// A integer parts of a number is malformed, normally this should be caught by the parsing regexes.
94    #[error("malformed integer part in phone number: {0}")]
95    MalformedInteger(#[from] std::num::ParseIntError),
96}
97
98
99/// Loading of Database) Error
100#[derive(Error, Debug)]
101pub enum LoadMetadata {
102
103    /// Parsing XML failed, the XML is malformed.
104    #[error("Malformed Metadata XML: {0}")]
105    Xml(#[from] xml::Error),
106
107    /// Parsing UTF-8 string from XML failed.
108    #[error("Non UTF-8 string in Metadata XML: {0}")]
109    Utf8(#[from] std::str::Utf8Error),
110
111    /// Metadata Error
112    #[error("{0}")]
113    Metadata(#[from] Metadata),
114
115    /// Malformed integer in Metadata XML database
116    #[error("Malformed integer in Metadata XML: {0}")]
117    Integer(#[from] std::num::ParseIntError),
118
119    /// Malformed boolean in Metadata XML database
120    #[error("Malformed boolean in Metadata XML: {0}")]
121    Bool(#[from] std::str::ParseBoolError),
122
123    /// I/O-Error while reading Metadata XML database
124    #[error("I/O-Error in Metadata XML: {0}")]
125    Io(#[from] std::io::Error),
126
127    /// Malformed Regex in Metadata XML database
128    #[error("Malformed Regex: {0}")]
129    Regex(#[from] regex::Error),
130
131}