1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use core::fmt;
#[cfg(feature = "std")]
use std::error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ErrorKind {
ExpressionNotClosed,
InvalidCharacter,
InvalidExpression,
InvalidPercentEncoding,
InvalidUtf8,
UnexpectedValueType,
UnsupportedOperator,
}
impl ErrorKind {
#[must_use]
fn as_str(self) -> &'static str {
match self {
Self::ExpressionNotClosed => "expression not closed",
Self::InvalidCharacter => "invalid character",
Self::InvalidExpression => "invalid expression",
Self::InvalidPercentEncoding => "invalid percent-encoded triplets",
Self::InvalidUtf8 => "invalid utf-8 byte sequence",
Self::UnexpectedValueType => "unexpected value type for the variable",
Self::UnsupportedOperator => "unsupported operator",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Error {
kind: ErrorKind,
location: usize,
}
impl Error {
#[inline]
#[must_use]
pub(super) fn new(kind: ErrorKind, location: usize) -> Self {
Self { kind, location }
}
}
impl fmt::Display for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"invalid URI template: {} (at {}-th byte)",
self.kind.as_str(),
self.location
)
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl error::Error for Error {}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub struct CreationError<T> {
source: T,
error: Error,
}
#[cfg(feature = "alloc")]
impl<T> CreationError<T> {
#[must_use]
pub fn into_source(self) -> T {
self.source
}
#[must_use]
pub fn validation_error(&self) -> Error {
self.error
}
#[must_use]
pub(crate) fn new(error: Error, source: T) -> Self {
Self { source, error }
}
}
#[cfg(feature = "alloc")]
impl<T: fmt::Debug> fmt::Debug for CreationError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CreationError")
.field("source", &self.source)
.field("error", &self.error)
.finish()
}
}
#[cfg(feature = "alloc")]
impl<T: Clone> Clone for CreationError<T> {
fn clone(&self) -> Self {
Self {
source: self.source.clone(),
error: self.error,
}
}
}
#[cfg(feature = "alloc")]
impl<T> fmt::Display for CreationError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.error.fmt(f)
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<T: fmt::Debug> error::Error for CreationError<T> {}