iri_string/template/
error.rs

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
145
146
147
148
149
150
151
152
153
154
//! Errors related to URI templates.

use core::fmt;

#[cfg(feature = "std")]
use std::error;

/// Template construction and expansion error kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ErrorKind {
    /// Cannot write to the backend.
    WriteFailed,
    /// Expression is not closed.
    ExpressionNotClosed,
    /// Invalid character.
    InvalidCharacter,
    /// Invalid expression.
    InvalidExpression,
    /// Invalid percent-encoded triplets.
    InvalidPercentEncoding,
    /// Invalid UTF-8 bytes.
    InvalidUtf8,
    /// Unexpected value type for the variable.
    UnexpectedValueType,
    /// Unsupported operator, including operators reserved for future.
    UnsupportedOperator,
}

impl ErrorKind {
    /// Returns the error message.
    #[must_use]
    fn as_str(self) -> &'static str {
        match self {
            Self::WriteFailed => "failed to write to the backend writer",
            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",
        }
    }
}

/// Template construction and expansion error.
///
// Note that this type should implement `Copy` trait.
// To return additional non-`Copy` data as an error, use wrapper type
// (as `std::string::FromUtf8Error` contains `std::str::Utf8Error`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Error {
    /// Error kind.
    kind: ErrorKind,
    /// Location (byte position of the error).
    location: usize,
}

impl Error {
    /// Creates a new `Error`.
    ///
    /// For internal use.
    #[inline]
    #[must_use]
    pub(super) fn new(kind: ErrorKind, location: usize) -> Self {
        Self { kind, location }
    }

    /// Returns the byte position the error is detected.
    ///
    /// NOTE: This is not a part of the public API since the value to be
    /// returned (i.e., the definition of the "position" of an error) is not
    /// guaranteed to be stable.
    #[cfg(test)]
    pub(super) fn location(&self) -> usize {
        self.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")]
impl error::Error for Error {}

/// Error on conversion into a URI template type.
// TODO: Unifiable to `types::CreationError`?
#[cfg(feature = "alloc")]
pub struct CreationError<T> {
    /// Soruce data.
    source: T,
    /// Validation error.
    error: Error,
}

#[cfg(feature = "alloc")]
impl<T> CreationError<T> {
    /// Returns the source data.
    #[must_use]
    pub fn into_source(self) -> T {
        self.source
    }

    /// Returns the validation error.
    #[must_use]
    pub fn validation_error(&self) -> Error {
        self.error
    }

    /// Creates a new `CreationError`.
    #[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")]
impl<T: fmt::Debug> error::Error for CreationError<T> {}