cookie_monster/
error.rs

1use std::fmt::Display;
2
3/// All errors that can be returned while parsing or serializing cookies.
4#[derive(Debug, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum Error {
7    // Name - value
8    EqualsNotFound,
9    NameEmpty,
10    InvalidName,
11    InvalidValue(char),
12
13    // Expires
14    ExpiresFmt,
15
16    // cookie-value
17    PercentDecodeError,
18
19    // Path
20    InvalidPathValue(char),
21    EmptyPathValue,
22    NoLeadingSlash,
23}
24
25impl Display for Error {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        let err = match self {
28            Error::EqualsNotFound => "No '=' found in the cookie",
29            Error::NameEmpty => "The cookie name is empty",
30            Error::InvalidName => "The cookie name contains an invalid character",
31            Error::InvalidValue(c) => {
32                return write!(f, "The cookie value contains an invalid value {c}");
33            }
34            Error::ExpiresFmt => "Failed to format the expires value",
35            Error::PercentDecodeError => "An error occured while decoding",
36            Error::InvalidPathValue(c) => {
37                return write!(f, "The path attribute contains an invalid value ({c})");
38            }
39            Error::EmptyPathValue => "The path attribute is empty",
40            Error::NoLeadingSlash => "The path attribute does not start with a leading slash",
41        };
42
43        f.write_str(err)
44    }
45}
46
47impl std::error::Error for Error {}