lychee_lib/types/
cache.rs

1use std::fmt::Display;
2
3use serde::{Deserialize, Deserializer, Serialize};
4
5use crate::{ErrorKind, Status, StatusCodeExcluder};
6
7/// Representation of the status of a cached request. This is kept simple on
8/// purpose because the type gets serialized to a cache file and might need to
9/// be parsed by other tools or edited by humans.
10#[derive(Debug, Serialize, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
11pub enum CacheStatus {
12    /// The cached request delivered a valid response
13    Ok(u16),
14    /// The cached request failed before
15    Error(Option<u16>),
16    /// The request was excluded (skipped)
17    Excluded,
18    /// The protocol is not yet supported
19    // We no longer cache unsupported files as they might be supported in future
20    // versions.
21    // Nevertheless, keep for compatibility when deserializing older cache
22    // files, even though this no longer gets serialized. Can be removed at a
23    // later point in time.
24    Unsupported,
25}
26
27impl<'de> Deserialize<'de> for CacheStatus {
28    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
29    where
30        D: Deserializer<'de>,
31    {
32        let status = <&str as Deserialize<'de>>::deserialize(deserializer)?;
33        match status {
34            "Excluded" => Ok(CacheStatus::Excluded),
35            // Keep for compatibility with older cache files, even though this
36            // no longer gets serialized. Can be removed at a later point in
37            // time.
38            "Unsupported" => Ok(CacheStatus::Unsupported),
39            other => match other.parse::<u16>() {
40                Ok(code) => match code {
41                    // classify successful status codes as cache status success
42                    // Does not account for status code overrides passed through
43                    // the 'accept' flag. Instead, this is handled at a higher level
44                    // when the cache status is converted to a status.
45                    200..=299 => Ok(CacheStatus::Ok(code)),
46                    // classify redirects, client errors, & server errors as cache status error
47                    _ => Ok(CacheStatus::Error(Some(code))),
48                },
49                Err(_) => Ok(CacheStatus::Error(None)),
50            },
51        }
52    }
53}
54
55impl Display for CacheStatus {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        match self {
58            Self::Ok(_) => write!(f, "OK (cached)"),
59            Self::Error(_) => write!(f, "Error (cached)"),
60            Self::Excluded => write!(f, "Excluded (cached)"),
61            Self::Unsupported => write!(f, "Unsupported (cached)"),
62        }
63    }
64}
65
66impl From<&Status> for CacheStatus {
67    fn from(s: &Status) -> Self {
68        match s {
69            Status::Cached(s) => *s,
70            // Reqwest treats unknown status codes as Ok(StatusCode).
71            // TODO: Use accepted status codes to decide whether this is a
72            // success or failure
73            Status::Ok(code) | Status::UnknownStatusCode(code) => Self::Ok(code.as_u16()),
74            Status::Excluded => Self::Excluded,
75            Status::Unsupported(_) => Self::Unsupported,
76            Status::Redirected(code, _) => Self::Error(Some(code.as_u16())),
77            Status::Timeout(code) => Self::Error(code.map(|code| code.as_u16())),
78            Status::Error(e) => match e {
79                ErrorKind::RejectedStatusCode(code) => Self::Error(Some(code.as_u16())),
80                ErrorKind::ReadResponseBody(e) | ErrorKind::BuildRequestClient(e) => {
81                    match e.status() {
82                        Some(code) => Self::Error(Some(code.as_u16())),
83                        None => Self::Error(None),
84                    }
85                }
86                _ => Self::Error(None),
87            },
88        }
89    }
90}
91
92impl From<CacheStatus> for Option<u16> {
93    fn from(val: CacheStatus) -> Self {
94        match val {
95            CacheStatus::Ok(status) => Some(status),
96            CacheStatus::Error(status) => status,
97            _ => None,
98        }
99    }
100}
101
102impl CacheStatus {
103    /// Returns `true` if the cache status is excluded by the given [`StatusCodeExcluder`].
104    #[must_use]
105    pub fn is_excluded(&self, excluder: &StatusCodeExcluder) -> bool {
106        match Option::<u16>::from(*self) {
107            Some(status) => excluder.contains(status),
108            _ => false,
109        }
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use serde::Deserialize;
116    use serde::de::value::{BorrowedStrDeserializer, Error as DeserializerError};
117
118    use crate::CacheStatus;
119
120    fn deserialize_cache_status(s: &str) -> Result<CacheStatus, DeserializerError> {
121        let deserializer: BorrowedStrDeserializer<DeserializerError> =
122            BorrowedStrDeserializer::new(s);
123        CacheStatus::deserialize(deserializer)
124    }
125
126    #[test]
127    fn test_deserialize_cache_status_success_code() {
128        assert_eq!(deserialize_cache_status("200"), Ok(CacheStatus::Ok(200)));
129    }
130
131    #[test]
132    fn test_deserialize_cache_status_error_code() {
133        assert_eq!(
134            deserialize_cache_status("404"),
135            Ok(CacheStatus::Error(Some(404)))
136        );
137    }
138
139    #[test]
140    fn test_deserialize_cache_status_excluded() {
141        assert_eq!(
142            deserialize_cache_status("Excluded"),
143            Ok(CacheStatus::Excluded)
144        );
145    }
146
147    #[test]
148    fn test_deserialize_cache_status_unsupported() {
149        assert_eq!(
150            deserialize_cache_status("Unsupported"),
151            Ok(CacheStatus::Unsupported)
152        );
153    }
154
155    #[test]
156    fn test_deserialize_cache_status_blank() {
157        assert_eq!(deserialize_cache_status(""), Ok(CacheStatus::Error(None)));
158    }
159}