waterui_url/
error.rs

1//! Error types for URL parsing.
2
3use core::fmt;
4
5/// Error type returned when URL parsing fails.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct ParseError {
8    kind: ParseErrorKind,
9}
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12enum ParseErrorKind {
13    /// The URL string is empty
14    Empty,
15}
16
17impl ParseError {
18    /// Creates a new `ParseError` for empty URL strings.
19    pub(crate) const fn empty() -> Self {
20        Self {
21            kind: ParseErrorKind::Empty,
22        }
23    }
24}
25
26impl fmt::Display for ParseError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self.kind {
29            ParseErrorKind::Empty => write!(f, "URL string is empty"),
30        }
31    }
32}
33
34#[cfg(feature = "std")]
35impl std::error::Error for ParseError {}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[cfg(feature = "std")]
42    #[test]
43    fn test_parse_error_display() {
44        use alloc::string::ToString;
45        let error = ParseError::empty();
46        assert_eq!(error.to_string(), "URL string is empty");
47    }
48
49    #[test]
50    fn test_parse_error_debug() {
51        use alloc::format;
52        let error = ParseError::empty();
53        assert_eq!(format!("{error:?}"), "ParseError { kind: Empty }");
54    }
55
56    #[test]
57    fn test_parse_error_equality() {
58        let error1 = ParseError::empty();
59        let error2 = ParseError::empty();
60        assert_eq!(error1, error2);
61    }
62}