rocket_http/parse/uri/
error.rs

1use std::{fmt, convert};
2use std::borrow::Cow;
3
4use pear::error::Expected;
5use pear::input::ParseError;
6use crate::parse::uri::RawInput;
7use crate::ext::IntoOwned;
8
9/// Error emitted on URI parse failure.
10///
11/// Internally, the type includes information about where the parse error
12/// occurred (the error's context) and information about what went wrong.
13/// Externally, this information can be retrieved (in textual form) through its
14/// `Display` implementation. In other words, by printing a value of this type.
15#[derive(Debug)]
16pub struct Error<'a> {
17    pub(crate) expected: Expected<u8, Cow<'a, [u8]>>,
18    pub(crate) index: usize,
19}
20
21#[doc(hidden)]
22impl<'a> From<ParseError<RawInput<'a>>> for Error<'a> {
23    fn from(inner: ParseError<RawInput<'a>>) -> Self {
24        let expected = inner.error.map(convert::identity, |v| v.values.into());
25        Error { expected, index: inner.info.context.start }
26    }
27}
28
29impl Error<'_> {
30    /// Returns the byte index into the text where the error occurred if it is
31    /// known.
32    ///
33    /// # Example
34    ///
35    /// ```rust
36    /// # extern crate rocket;
37    /// use rocket::http::uri::Origin;
38    ///
39    /// let err = Origin::parse("/foo bar").unwrap_err();
40    /// assert_eq!(err.index(), 4);
41    /// ```
42    pub fn index(&self) -> usize {
43        self.index
44    }
45}
46
47impl fmt::Display for Error<'_> {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        write!(f, "{} at index {}", self.expected, self.index)
50    }
51}
52
53impl IntoOwned for Error<'_> {
54    type Owned = Error<'static>;
55
56    fn into_owned(self) -> Error<'static> {
57        Error {
58            expected: self.expected.map(|t| t, |s| s.into_owned().into()),
59            index: self.index
60        }
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use crate::parse::uri::origin_from_str;
67
68    macro_rules! check_err {
69        ($url:expr => $error:expr) => {{
70            let e = origin_from_str($url).unwrap_err();
71            assert_eq!(e.to_string(), $error.to_string())
72        }}
73    }
74
75    #[test]
76    fn check_display() {
77        check_err!("a" => "expected token '/' but found 'a' at index 0");
78        check_err!("?" => "expected token '/' but found '?' at index 0");
79        check_err!("θΏ™" => "expected token '/' but found byte 232 at index 0");
80    }
81}