| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 19 |
pub enum Posture { |
| 20 |
|
| 21 |
|
| 22 |
#[default] |
| 23 |
Local, |
| 24 |
|
| 25 |
Synced, |
| 26 |
} |
| 27 |
|
| 28 |
impl Posture { |
| 29 |
|
| 30 |
pub const fn replicated(self) -> bool { |
| 31 |
matches!(self, Posture::Synced) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 41 |
pub struct PolicyRow { |
| 42 |
pub key: &'static str, |
| 43 |
pub replicated: bool, |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#[derive(Debug, Clone, Copy)] |
| 53 |
pub struct ConfigSpec { |
| 54 |
table: &'static str, |
| 55 |
keys: &'static [(&'static str, Posture)], |
| 56 |
} |
| 57 |
|
| 58 |
impl ConfigSpec { |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
pub const fn new(table: &'static str, keys: &'static [(&'static str, Posture)]) -> Self { |
| 66 |
Self { table, keys } |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
pub const fn table(&self) -> &'static str { |
| 71 |
self.table |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
pub fn keys(&self) -> impl Iterator<Item = (&'static str, Posture)> + '_ { |
| 76 |
self.keys.iter().copied() |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
pub fn posture(&self, key: &str) -> Posture { |
| 86 |
self.keys |
| 87 |
.iter() |
| 88 |
.rev() |
| 89 |
.find(|(name, _)| *name == key) |
| 90 |
.map_or(Posture::Local, |(_, posture)| *posture) |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
pub fn is_synced(&self, key: &str) -> bool { |
| 96 |
self.posture(key).replicated() |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pub fn policy_rows(&self) -> impl Iterator<Item = PolicyRow> + '_ { |
| 106 |
self.keys.iter().map(|(key, posture)| PolicyRow { |
| 107 |
key, |
| 108 |
replicated: posture.replicated(), |
| 109 |
}) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
#[cfg(test)] |
| 114 |
mod tests { |
| 115 |
use super::*; |
| 116 |
|
| 117 |
const SPEC: ConfigSpec = ConfigSpec::new( |
| 118 |
"app_config", |
| 119 |
&[ |
| 120 |
("theme", Posture::Synced), |
| 121 |
("sidebar_visible", Posture::Synced), |
| 122 |
("mirror_path", Posture::Local), |
| 123 |
], |
| 124 |
); |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn the_default_posture_is_local() { |
| 128 |
assert_eq!(Posture::default(), Posture::Local); |
| 129 |
assert!(!Posture::Local.replicated()); |
| 130 |
assert!(Posture::Synced.replicated()); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
#[test] |
| 136 |
fn an_undeclared_key_is_local_not_synced() { |
| 137 |
assert_eq!(SPEC.posture("never_heard_of_it"), Posture::Local); |
| 138 |
assert!(!SPEC.is_synced("never_heard_of_it")); |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn declared_keys_report_their_posture() { |
| 143 |
assert!(SPEC.is_synced("theme")); |
| 144 |
assert!(SPEC.is_synced("sidebar_visible")); |
| 145 |
assert!(!SPEC.is_synced("mirror_path"), "a local path never syncs"); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
#[test] |
| 151 |
fn policy_rows_carry_every_declared_key_and_its_replication() { |
| 152 |
let rows: Vec<PolicyRow> = SPEC.policy_rows().collect(); |
| 153 |
assert_eq!(rows.len(), 3); |
| 154 |
assert_eq!( |
| 155 |
rows[0], |
| 156 |
PolicyRow { |
| 157 |
key: "theme", |
| 158 |
replicated: true |
| 159 |
}, |
| 160 |
); |
| 161 |
assert_eq!( |
| 162 |
rows[2], |
| 163 |
PolicyRow { |
| 164 |
key: "mirror_path", |
| 165 |
replicated: false, |
| 166 |
}, |
| 167 |
); |
| 168 |
|
| 169 |
assert!(!rows.iter().any(|r| r.key == "never_heard_of_it")); |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
fn the_table_and_key_set_are_readable() { |
| 174 |
assert_eq!(SPEC.table(), "app_config"); |
| 175 |
let keys: Vec<&str> = SPEC.keys().map(|(k, _)| k).collect(); |
| 176 |
assert_eq!(keys, ["theme", "sidebar_visible", "mirror_path"]); |
| 177 |
} |
| 178 |
} |
| 179 |
|