Skip to main content

web_url/error/
invalid_url_error.rs

1use crate::Error;
2use std::fmt::{Display, Formatter};
3
4/// An error parsing a web-based URL from an owned string.
5///
6/// The invalid URL string can be recovered, like `std::string::FromUtf8Error`.
7#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
8pub struct InvalidUrlError {
9    error: Error,
10    url: String,
11}
12
13impl InvalidUrlError {
14    //! Construction
15
16    /// Creates a new invalid URL error.
17    pub(crate) const fn new(error: Error, url: String) -> Self {
18        Self { error, url }
19    }
20}
21
22impl InvalidUrlError {
23    //! Properties
24
25    /// Gets the parse error.
26    #[must_use]
27    pub const fn error(&self) -> Error {
28        self.error
29    }
30
31    /// Gets the invalid URL string.
32    #[must_use]
33    pub fn url(&self) -> &str {
34        self.url.as_str()
35    }
36}
37
38impl InvalidUrlError {
39    //! Deconstruction
40
41    /// Converts the error back into the invalid URL string.
42    #[must_use]
43    pub fn into_url(self) -> String {
44        self.url
45    }
46}
47
48impl From<InvalidUrlError> for Error {
49    fn from(error: InvalidUrlError) -> Self {
50        error.error
51    }
52}
53
54impl Display for InvalidUrlError {
55    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
56        f.pad(self.error.message())
57    }
58}
59
60/// The `source` is not the parse error: its message is this error's message, so a chain would just print it twice. Use
61/// `error` for the typed parse error.
62impl std::error::Error for InvalidUrlError {}