fluent_uri/
error.rs

1//! Error types.
2
3use crate::internal::NoInput;
4
5/// Detailed cause of a [`ParseError`].
6#[derive(Clone, Copy, Debug)]
7pub(crate) enum ParseErrorKind {
8    /// Invalid percent-encoded octet that is either non-hexadecimal or incomplete.
9    ///
10    /// The error index points to the percent character "%" of the octet.
11    InvalidOctet,
12    /// Unexpected character that is not allowed by the URI/IRI syntax.
13    ///
14    /// The error index points to the first byte of the character.
15    UnexpectedChar,
16    /// Invalid IPv6 address.
17    ///
18    /// The error index points to the first byte of the address.
19    InvalidIpv6Addr,
20    /// The scheme component is not present.
21    NoScheme,
22}
23
24/// An error occurred when parsing a URI/IRI (reference).
25#[derive(Clone, Copy)]
26pub struct ParseError<I = NoInput> {
27    pub(crate) index: usize,
28    pub(crate) kind: ParseErrorKind,
29    pub(crate) input: I,
30}
31
32impl ParseError {
33    pub(crate) fn with_input<I>(self, input: I) -> ParseError<I> {
34        ParseError {
35            index: self.index,
36            kind: self.kind,
37            input,
38        }
39    }
40}
41
42impl<I: AsRef<str>> ParseError<I> {
43    /// Recovers the input that was attempted to parse into a URI/IRI (reference).
44    #[must_use]
45    pub fn into_input(self) -> I {
46        self.input
47    }
48
49    /// Returns the error with the input stripped.
50    #[must_use]
51    pub fn strip_input(&self) -> ParseError {
52        ParseError {
53            index: self.index,
54            kind: self.kind,
55            input: NoInput,
56        }
57    }
58}
59
60#[cfg(feature = "std")]
61impl<I> std::error::Error for ParseError<I> {}
62
63/// Detailed cause of a [`BuildError`].
64#[derive(Clone, Copy, Debug)]
65pub(crate) enum BuildErrorKind {
66    NonAbemptyPath,
67    PathStartingWithDoubleSlash,
68    ColonInFirstPathSegment,
69}
70
71/// An error occurred when building a URI/IRI (reference).
72#[derive(Clone, Copy, Debug)]
73pub struct BuildError(pub(crate) BuildErrorKind);
74
75#[cfg(feature = "std")]
76impl std::error::Error for BuildError {}
77
78/// Detailed cause of a [`ResolveError`].
79#[derive(Clone, Copy, Debug)]
80pub(crate) enum ResolveErrorKind {
81    InvalidBase,
82    OpaqueBase,
83    // PathUnderflow,
84}
85
86/// An error occurred when resolving a URI/IRI reference.
87#[derive(Clone, Copy, Debug)]
88pub struct ResolveError(pub(crate) ResolveErrorKind);
89
90#[cfg(feature = "std")]
91impl std::error::Error for ResolveError {}