| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
use crate::{Kind, Knob, Posture, Registry, Setting}; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] |
| 45 |
pub enum Reach { |
| 46 |
|
| 47 |
|
| 48 |
#[default] |
| 49 |
Synced, |
| 50 |
|
| 51 |
Local, |
| 52 |
} |
| 53 |
|
| 54 |
#[cfg(feature = "synckit")] |
| 55 |
impl Reach { |
| 56 |
|
| 57 |
#[must_use] |
| 58 |
pub const fn posture(self) -> synckit_config::Posture { |
| 59 |
match self { |
| 60 |
Self::Synced => synckit_config::Posture::Synced, |
| 61 |
Self::Local => synckit_config::Posture::Local, |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 72 |
pub enum Value { |
| 73 |
|
| 74 |
Toggle(bool), |
| 75 |
|
| 76 |
Seconds(i64), |
| 77 |
|
| 78 |
Count(i64), |
| 79 |
|
| 80 |
Choice(String), |
| 81 |
} |
| 82 |
|
| 83 |
impl Value { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
#[must_use] |
| 89 |
pub fn text(&self) -> String { |
| 90 |
match self { |
| 91 |
Self::Toggle(on) => (*on).to_string(), |
| 92 |
Self::Seconds(n) | Self::Count(n) => n.to_string(), |
| 93 |
Self::Choice(picked) => picked.clone(), |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
#[must_use] |
| 99 |
pub const fn is_on(&self) -> bool { |
| 100 |
matches!(self, Self::Toggle(true)) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
#[must_use] |
| 105 |
pub const fn number(&self) -> Option<i64> { |
| 106 |
match self { |
| 107 |
Self::Seconds(n) | Self::Count(n) => Some(*n), |
| 108 |
_ => None, |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
#[must_use] |
| 114 |
pub fn picked(&self) -> Option<&str> { |
| 115 |
match self { |
| 116 |
Self::Choice(picked) => Some(picked), |
| 117 |
_ => None, |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
#[must_use] |
| 123 |
pub fn of(setting: Setting) -> Self { |
| 124 |
match setting { |
| 125 |
Setting::Toggle(on) => Self::Toggle(on), |
| 126 |
Setting::Seconds(n) => Self::Seconds(n), |
| 127 |
Setting::Count(n) => Self::Count(n), |
| 128 |
Setting::Choice { default, .. } => Self::Choice(default.to_string()), |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
#[must_use] |
| 138 |
pub fn read(setting: Setting, stored: &str) -> Option<Self> { |
| 139 |
match setting { |
| 140 |
Setting::Toggle(_) => stored.parse().ok().map(Self::Toggle), |
| 141 |
Setting::Seconds(_) => stored.parse().ok().map(Self::Seconds), |
| 142 |
Setting::Count(_) => stored.parse().ok().map(Self::Count), |
| 143 |
Setting::Choice { options, .. } => options |
| 144 |
.contains(&stored) |
| 145 |
.then(|| Self::Choice(stored.to_string())), |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 156 |
pub struct Generated { |
| 157 |
|
| 158 |
pub key: String, |
| 159 |
|
| 160 |
pub kind: &'static str, |
| 161 |
|
| 162 |
pub knob: Option<&'static str>, |
| 163 |
|
| 164 |
pub label: &'static str, |
| 165 |
|
| 166 |
pub reach: Reach, |
| 167 |
|
| 168 |
pub default: Value, |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
pub trait Settings { |
| 179 |
|
| 180 |
fn get(&self, key: &str) -> Option<String>; |
| 181 |
} |
| 182 |
|
| 183 |
impl<F> Settings for F |
| 184 |
where |
| 185 |
F: Fn(&str) -> Option<String>, |
| 186 |
{ |
| 187 |
fn get(&self, key: &str) -> Option<String> { |
| 188 |
self(key) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
impl<S: ::std::hash::BuildHasher> Settings for std::collections::HashMap<String, String, S> { |
| 201 |
fn get(&self, key: &str) -> Option<String> { |
| 202 |
self.get(key).cloned() |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 211 |
pub struct Unset; |
| 212 |
|
| 213 |
impl Settings for Unset { |
| 214 |
fn get(&self, _key: &str) -> Option<String> { |
| 215 |
None |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
#[must_use] |
| 221 |
pub fn enabled_key(kind: &str) -> String { |
| 222 |
format!("{kind}.enabled") |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
#[must_use] |
| 227 |
pub fn knob_key(kind: &str, knob: &str) -> String { |
| 228 |
format!("{kind}.{knob}") |
| 229 |
} |
| 230 |
|
| 231 |
impl Kind { |
| 232 |
|
| 233 |
#[must_use] |
| 234 |
pub fn enabled_config(&self) -> Generated { |
| 235 |
Generated { |
| 236 |
key: enabled_key(self.id), |
| 237 |
kind: self.id, |
| 238 |
knob: None, |
| 239 |
label: self.title, |
| 240 |
reach: self.reach, |
| 241 |
default: Value::Toggle(matches!(self.ships, Posture::On)), |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
#[must_use] |
| 247 |
pub fn knob_config(&self, knob: &Knob) -> Generated { |
| 248 |
Generated { |
| 249 |
key: knob_key(self.id, knob.id), |
| 250 |
kind: self.id, |
| 251 |
knob: Some(knob.id), |
| 252 |
label: knob.label, |
| 253 |
reach: knob.reach, |
| 254 |
default: Value::of(knob.value), |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
pub fn config(&self) -> impl Iterator<Item = Generated> + '_ { |
| 260 |
std::iter::once(self.enabled_config()) |
| 261 |
.chain(self.options.iter().map(|knob| self.knob_config(knob))) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
impl Registry { |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
pub fn config(&self) -> impl Iterator<Item = Generated> + '_ { |
| 271 |
self.kinds().iter().flat_map(Kind::config) |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
pub fn is_on(&self, kind: &str, settings: &impl Settings) -> bool { |
| 280 |
self.value(kind, None, settings).is_some_and(|v| v.is_on()) |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
pub fn value(&self, kind: &str, knob: Option<&str>, settings: &impl Settings) -> Option<Value> { |
| 288 |
let kind = self.kind(kind)?; |
| 289 |
let (key, setting, fallback) = match knob { |
| 290 |
None => ( |
| 291 |
enabled_key(kind.id), |
| 292 |
Setting::Toggle(matches!(kind.ships, Posture::On)), |
| 293 |
Value::Toggle(matches!(kind.ships, Posture::On)), |
| 294 |
), |
| 295 |
Some(name) => { |
| 296 |
let knob = kind.knob(name)?; |
| 297 |
( |
| 298 |
knob_key(kind.id, knob.id), |
| 299 |
knob.value, |
| 300 |
Value::of(knob.value), |
| 301 |
) |
| 302 |
} |
| 303 |
}; |
| 304 |
Some( |
| 305 |
settings |
| 306 |
.get(&key) |
| 307 |
.and_then(|stored| Value::read(setting, &stored)) |
| 308 |
.unwrap_or(fallback), |
| 309 |
) |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
#[cfg(feature = "synckit")] |
| 317 |
pub fn postures(&self) -> Vec<(String, synckit_config::Posture)> { |
| 318 |
self.config() |
| 319 |
.map(|generated| (generated.key, generated.reach.posture())) |
| 320 |
.collect() |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
#[cfg(feature = "synckit")] |
| 341 |
#[must_use] |
| 342 |
pub fn spec(&self, table: &'static str) -> synckit_config::ConfigSpec { |
| 343 |
let keys: Vec<(&'static str, synckit_config::Posture)> = self |
| 344 |
.config() |
| 345 |
.map(|generated| { |
| 346 |
let key: &'static str = Box::leak(generated.key.into_boxed_str()); |
| 347 |
(key, generated.reach.posture()) |
| 348 |
}) |
| 349 |
.collect(); |
| 350 |
synckit_config::ConfigSpec::new(table, Box::leak(keys.into_boxed_slice())) |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
#[cfg(test)] |
| 355 |
mod tests { |
| 356 |
use super::*; |
| 357 |
use crate::{Kind, Knob, Registry, Setting}; |
| 358 |
use std::collections::HashMap; |
| 359 |
|
| 360 |
static KINDS: &[Kind] = &[ |
| 361 |
Kind::new( |
| 362 |
"snooze-expiry", |
| 363 |
"Snoozed items resurface", |
| 364 |
"When something you snoozed comes back.", |
| 365 |
"Reminders", |
| 366 |
) |
| 367 |
.shipping_on(), |
| 368 |
Kind::new( |
| 369 |
"event-reminder", |
| 370 |
"Event reminders", |
| 371 |
"Before an event starts. Not the Events tab dot, which is `event_lead_minutes`.", |
| 372 |
"Reminders", |
| 373 |
) |
| 374 |
.shipping_on() |
| 375 |
.quiet_after_restart() |
| 376 |
.with(&[ |
| 377 |
Knob::new("lead", "How long before", Setting::Seconds(900)), |
| 378 |
Knob::new( |
| 379 |
"sound", |
| 380 |
"Sound", |
| 381 |
Setting::Choice { |
| 382 |
options: &["chime", "silent"], |
| 383 |
default: "chime", |
| 384 |
}, |
| 385 |
) |
| 386 |
.on_this_device(), |
| 387 |
]), |
| 388 |
Kind::new( |
| 389 |
"digest", |
| 390 |
"Daily digest", |
| 391 |
"One summary of the day.", |
| 392 |
"Summaries", |
| 393 |
) |
| 394 |
.with(&[Knob::new("items", "How many items", Setting::Count(10))]), |
| 395 |
]; |
| 396 |
|
| 397 |
static NOTIFS: Registry = Registry::new(KINDS); |
| 398 |
|
| 399 |
fn stored(pairs: &[(&str, &str)]) -> impl Settings { |
| 400 |
let map: HashMap<String, String> = pairs |
| 401 |
.iter() |
| 402 |
.map(|(k, v)| ((*k).to_string(), (*v).to_string())) |
| 403 |
.collect(); |
| 404 |
move |key: &str| map.get(key).cloned() |
| 405 |
} |
| 406 |
|
| 407 |
#[test] |
| 408 |
fn a_kind_generates_its_on_off_and_one_key_per_knob() { |
| 409 |
let keys: Vec<String> = NOTIFS.config().map(|g| g.key).collect(); |
| 410 |
assert_eq!( |
| 411 |
keys, |
| 412 |
vec![ |
| 413 |
"snooze-expiry.enabled", |
| 414 |
"event-reminder.enabled", |
| 415 |
"event-reminder.lead", |
| 416 |
"event-reminder.sound", |
| 417 |
"digest.enabled", |
| 418 |
"digest.items", |
| 419 |
] |
| 420 |
); |
| 421 |
} |
| 422 |
|
| 423 |
#[test] |
| 424 |
fn a_generated_key_cannot_collide_with_a_hand_written_one() { |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
let generated: Vec<String> = NOTIFS.config().map(|g| g.key).collect(); |
| 430 |
assert!(!generated.iter().any(|k| k == "event_lead_minutes")); |
| 431 |
assert!(generated.iter().all(|k| k.contains('.'))); |
| 432 |
} |
| 433 |
|
| 434 |
#[test] |
| 435 |
fn the_default_comes_from_the_declaration_and_from_nowhere_else() { |
| 436 |
let by_key: HashMap<String, Value> = NOTIFS.config().map(|g| (g.key, g.default)).collect(); |
| 437 |
assert_eq!(by_key["snooze-expiry.enabled"], Value::Toggle(true)); |
| 438 |
assert_eq!(by_key["digest.enabled"], Value::Toggle(false)); |
| 439 |
assert_eq!(by_key["event-reminder.lead"], Value::Seconds(900)); |
| 440 |
assert_eq!(by_key["digest.items"], Value::Count(10)); |
| 441 |
assert_eq!( |
| 442 |
by_key["event-reminder.sound"], |
| 443 |
Value::Choice("chime".to_string()) |
| 444 |
); |
| 445 |
} |
| 446 |
|
| 447 |
#[test] |
| 448 |
fn a_preference_syncs_and_a_machine_fact_does_not() { |
| 449 |
let by_key: HashMap<String, Reach> = NOTIFS.config().map(|g| (g.key, g.reach)).collect(); |
| 450 |
assert_eq!(by_key["event-reminder.lead"], Reach::Synced); |
| 451 |
assert_eq!(by_key["event-reminder.enabled"], Reach::Synced); |
| 452 |
|
| 453 |
|
| 454 |
assert_eq!(by_key["event-reminder.sound"], Reach::Local); |
| 455 |
} |
| 456 |
|
| 457 |
#[test] |
| 458 |
fn a_stored_value_wins_over_the_default() { |
| 459 |
let settings = stored(&[("digest.items", "3"), ("digest.enabled", "true")]); |
| 460 |
assert_eq!( |
| 461 |
NOTIFS.value("digest", Some("items"), &settings), |
| 462 |
Some(Value::Count(3)) |
| 463 |
); |
| 464 |
assert!(NOTIFS.is_on("digest", &settings)); |
| 465 |
} |
| 466 |
|
| 467 |
#[test] |
| 468 |
fn an_unset_key_reads_as_its_declared_default() { |
| 469 |
assert_eq!( |
| 470 |
NOTIFS.value("digest", Some("items"), &Unset), |
| 471 |
Some(Value::Count(10)) |
| 472 |
); |
| 473 |
assert!(!NOTIFS.is_on("digest", &Unset)); |
| 474 |
assert!(NOTIFS.is_on("snooze-expiry", &Unset)); |
| 475 |
} |
| 476 |
|
| 477 |
#[test] |
| 478 |
fn a_row_that_cannot_be_read_falls_back_rather_than_failing() { |
| 479 |
|
| 480 |
|
| 481 |
let settings = stored(&[ |
| 482 |
("digest.items", "not a number"), |
| 483 |
("event-reminder.sound", "foghorn"), |
| 484 |
]); |
| 485 |
assert_eq!( |
| 486 |
NOTIFS.value("digest", Some("items"), &settings), |
| 487 |
Some(Value::Count(10)) |
| 488 |
); |
| 489 |
assert_eq!( |
| 490 |
NOTIFS.value("event-reminder", Some("sound"), &settings), |
| 491 |
Some(Value::Choice("chime".to_string())) |
| 492 |
); |
| 493 |
} |
| 494 |
|
| 495 |
#[test] |
| 496 |
fn an_undeclared_kind_is_off_rather_than_absent() { |
| 497 |
|
| 498 |
|
| 499 |
assert!(!NOTIFS.is_on("nope", &Unset)); |
| 500 |
assert_eq!(NOTIFS.value("nope", None, &Unset), None); |
| 501 |
assert_eq!(NOTIFS.value("digest", Some("nope"), &Unset), None); |
| 502 |
} |
| 503 |
|
| 504 |
#[test] |
| 505 |
fn every_value_round_trips_through_the_text_the_store_holds() { |
| 506 |
for setting in [ |
| 507 |
Setting::Toggle(true), |
| 508 |
Setting::Seconds(900), |
| 509 |
Setting::Count(10), |
| 510 |
Setting::Choice { |
| 511 |
options: &["chime", "silent"], |
| 512 |
default: "chime", |
| 513 |
}, |
| 514 |
] { |
| 515 |
let value = Value::of(setting); |
| 516 |
assert_eq!(Value::read(setting, &value.text()), Some(value)); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
#[cfg(feature = "synckit")] |
| 521 |
#[test] |
| 522 |
fn the_spec_is_produced_from_the_declaration_rather_than_written_beside_it() { |
| 523 |
let spec = NOTIFS.spec("user_config"); |
| 524 |
assert_eq!(spec.table(), "user_config"); |
| 525 |
assert!(spec.is_synced("event-reminder.lead")); |
| 526 |
assert!(!spec.is_synced("event-reminder.sound")); |
| 527 |
|
| 528 |
assert!(!spec.is_synced("event-reminder.whatever")); |
| 529 |
assert_eq!(spec.keys().count(), NOTIFS.config().count()); |
| 530 |
} |
| 531 |
} |
| 532 |
|