Skip to main content

web_url/error/
error.rs

1use crate::Error::*;
2use std::fmt::{Display, Formatter};
3
4/// An error parsing a web-based URL.
5#[non_exhaustive]
6#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
7pub enum Error {
8    /// The scheme was invalid.
9    InvalidScheme,
10
11    /// The URL had user info, which is not supported.
12    UserInfoNotSupported,
13
14    /// The host was invalid.
15    InvalidHost,
16
17    /// The port was invalid.
18    InvalidPort,
19
20    /// The path was invalid.
21    InvalidPath,
22
23    /// The query was invalid.
24    InvalidQuery,
25
26    /// The query parameter was invalid.
27    InvalidParam,
28
29    /// The fragment was invalid.
30    InvalidFragment,
31
32    /// The URL was too long. (must be under 4 GiB)
33    UrlTooLong,
34}
35
36impl Error {
37    //! Display
38
39    /// Gets the error message.
40    #[must_use]
41    pub const fn message(self) -> &'static str {
42        match self {
43            InvalidScheme => "invalid scheme",
44            UserInfoNotSupported => "user info is not supported",
45            InvalidHost => "invalid host",
46            InvalidPort => "invalid port",
47            InvalidPath => "invalid path",
48            InvalidQuery => "invalid query",
49            InvalidParam => "invalid query parameter",
50            InvalidFragment => "invalid fragment",
51            UrlTooLong => "URL too long (>= 4 GiB)",
52        }
53    }
54}
55
56impl Display for Error {
57    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
58        f.pad(self.message())
59    }
60}
61
62impl std::error::Error for Error {}