1use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
11pub enum CacheHint {
12 Always,
14 UntilChanged,
16 RecentlyChanged,
26 SlidingPrefix {
29 stable_fraction: f32,
33 },
34 Never,
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn cache_hint_always_equality() {
44 assert_eq!(CacheHint::Always, CacheHint::Always);
45 assert_ne!(CacheHint::Always, CacheHint::Never);
46 }
47
48 #[test]
49 fn cache_hint_never_equality() {
50 assert_eq!(CacheHint::Never, CacheHint::Never);
51 assert_ne!(CacheHint::Never, CacheHint::UntilChanged);
52 }
53
54 #[test]
55 fn cache_hint_until_changed_equality() {
56 assert_eq!(CacheHint::UntilChanged, CacheHint::UntilChanged);
57 }
58
59 #[test]
60 fn cache_hint_recently_changed_is_distinct_from_until_changed() {
61 assert_eq!(CacheHint::RecentlyChanged, CacheHint::RecentlyChanged);
62 assert_ne!(CacheHint::RecentlyChanged, CacheHint::UntilChanged);
63 let dbg = format!("{:?}", CacheHint::RecentlyChanged);
64 assert!(dbg.contains("RecentlyChanged"));
65 }
66
67 #[test]
68 fn cache_hint_sliding_prefix_equality() {
69 let a = CacheHint::SlidingPrefix {
70 stable_fraction: 0.75,
71 };
72 let b = CacheHint::SlidingPrefix {
73 stable_fraction: 0.75,
74 };
75 assert_eq!(a, b);
76
77 let c = CacheHint::SlidingPrefix {
78 stable_fraction: 0.5,
79 };
80 assert_ne!(a, c);
81 }
82
83 #[test]
84 fn cache_hint_clone() {
85 let hint = CacheHint::SlidingPrefix {
86 stable_fraction: 0.8,
87 };
88 let cloned = hint;
89 assert_eq!(
90 cloned,
91 CacheHint::SlidingPrefix {
92 stable_fraction: 0.8
93 }
94 );
95 }
96
97 #[test]
98 fn cache_hint_debug() {
99 let hint = CacheHint::Always;
100 let dbg = format!("{:?}", hint);
101 assert!(dbg.contains("Always"));
102 }
103
104 #[test]
105 fn cache_hint_serde_roundtrip() {
106 let hints = vec![
107 CacheHint::Always,
108 CacheHint::UntilChanged,
109 CacheHint::RecentlyChanged,
110 CacheHint::SlidingPrefix {
111 stable_fraction: 0.75,
112 },
113 CacheHint::Never,
114 ];
115 for hint in hints {
116 let json = serde_json::to_string(&hint).unwrap();
117 let parsed: CacheHint = serde_json::from_str(&json).unwrap();
118 assert_eq!(hint, parsed);
119 }
120 }
121}