| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
pub mod config; |
| 62 |
pub mod deliver; |
| 63 |
#[cfg(feature = "tauri")] |
| 64 |
pub mod notify; |
| 65 |
#[cfg(feature = "describe")] |
| 66 |
pub mod pane; |
| 67 |
|
| 68 |
pub use config::{Generated, Reach, Settings, Value}; |
| 69 |
pub use deliver::{Deliver, Occurrence, Outbox, Sweep}; |
| 70 |
#[cfg(feature = "tauri")] |
| 71 |
pub use notify::Notifier; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 84 |
pub enum Posture { |
| 85 |
|
| 86 |
On, |
| 87 |
|
| 88 |
Off, |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] |
| 107 |
pub enum CatchUp { |
| 108 |
|
| 109 |
#[default] |
| 110 |
Fire, |
| 111 |
|
| 112 |
Skip, |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 129 |
pub enum Setting { |
| 130 |
|
| 131 |
Toggle(bool), |
| 132 |
|
| 133 |
Seconds(i64), |
| 134 |
|
| 135 |
Count(i64), |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
Choice { |
| 141 |
|
| 142 |
options: &'static [&'static str], |
| 143 |
|
| 144 |
default: &'static str, |
| 145 |
}, |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 154 |
pub struct Knob { |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
pub id: &'static str, |
| 160 |
|
| 161 |
pub label: &'static str, |
| 162 |
|
| 163 |
pub value: Setting, |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
pub reach: Reach, |
| 170 |
} |
| 171 |
|
| 172 |
impl Knob { |
| 173 |
|
| 174 |
#[must_use] |
| 175 |
pub const fn new(id: &'static str, label: &'static str, value: Setting) -> Self { |
| 176 |
Self { |
| 177 |
id, |
| 178 |
label, |
| 179 |
value, |
| 180 |
reach: Reach::Synced, |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
#[must_use] |
| 190 |
pub const fn on_this_device(mut self) -> Self { |
| 191 |
self.reach = Reach::Local; |
| 192 |
self |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 204 |
pub struct Kind { |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
pub id: &'static str, |
| 212 |
|
| 213 |
pub title: &'static str, |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
pub summary: &'static str, |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
pub category: &'static str, |
| 225 |
|
| 226 |
pub ships: Posture, |
| 227 |
|
| 228 |
|
| 229 |
pub reach: Reach, |
| 230 |
|
| 231 |
pub restart: CatchUp, |
| 232 |
|
| 233 |
pub options: &'static [Knob], |
| 234 |
} |
| 235 |
|
| 236 |
impl Kind { |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
#[must_use] |
| 242 |
pub const fn new( |
| 243 |
id: &'static str, |
| 244 |
title: &'static str, |
| 245 |
summary: &'static str, |
| 246 |
category: &'static str, |
| 247 |
) -> Self { |
| 248 |
Self { |
| 249 |
id, |
| 250 |
title, |
| 251 |
summary, |
| 252 |
category, |
| 253 |
ships: Posture::Off, |
| 254 |
reach: Reach::Synced, |
| 255 |
restart: CatchUp::Fire, |
| 256 |
options: &[], |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
#[must_use] |
| 262 |
pub const fn shipping_on(mut self) -> Self { |
| 263 |
self.ships = Posture::On; |
| 264 |
self |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
#[must_use] |
| 269 |
pub const fn with(mut self, options: &'static [Knob]) -> Self { |
| 270 |
self.options = options; |
| 271 |
self |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
#[must_use] |
| 279 |
pub const fn on_this_device(mut self) -> Self { |
| 280 |
self.reach = Reach::Local; |
| 281 |
self |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
#[must_use] |
| 288 |
pub const fn quiet_after_restart(mut self) -> Self { |
| 289 |
self.restart = CatchUp::Skip; |
| 290 |
self |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
#[must_use] |
| 295 |
pub fn knob(&self, id: &str) -> Option<&Knob> { |
| 296 |
self.options.iter().find(|knob| knob.id == id) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 306 |
pub struct Registry { |
| 307 |
kinds: &'static [Kind], |
| 308 |
} |
| 309 |
|
| 310 |
impl Registry { |
| 311 |
|
| 312 |
#[must_use] |
| 313 |
pub const fn new(kinds: &'static [Kind]) -> Self { |
| 314 |
Self { kinds } |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
#[must_use] |
| 319 |
pub const fn kinds(&self) -> &'static [Kind] { |
| 320 |
self.kinds |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
#[must_use] |
| 325 |
pub fn kind(&self, id: &str) -> Option<&Kind> { |
| 326 |
self.kinds.iter().find(|kind| kind.id == id) |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
#[must_use] |
| 335 |
pub fn categories(&self) -> Vec<&'static str> { |
| 336 |
let mut seen: Vec<&'static str> = Vec::new(); |
| 337 |
for kind in self.kinds { |
| 338 |
if !seen.contains(&kind.category) { |
| 339 |
seen.push(kind.category); |
| 340 |
} |
| 341 |
} |
| 342 |
seen |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
pub fn check(&self) -> Result<(), Fault> { |
| 356 |
let mut seen: Vec<&'static str> = Vec::new(); |
| 357 |
for kind in self.kinds { |
| 358 |
if !is_key(kind.id) { |
| 359 |
return Err(Fault::BadId { id: kind.id }); |
| 360 |
} |
| 361 |
if seen.contains(&kind.id) { |
| 362 |
return Err(Fault::TwoKinds { id: kind.id }); |
| 363 |
} |
| 364 |
seen.push(kind.id); |
| 365 |
|
| 366 |
let mut knobs: Vec<&'static str> = Vec::new(); |
| 367 |
for knob in kind.options { |
| 368 |
if !is_key(knob.id) { |
| 369 |
return Err(Fault::BadId { id: knob.id }); |
| 370 |
} |
| 371 |
if knobs.contains(&knob.id) { |
| 372 |
return Err(Fault::TwoKnobs { |
| 373 |
kind: kind.id, |
| 374 |
id: knob.id, |
| 375 |
}); |
| 376 |
} |
| 377 |
knobs.push(knob.id); |
| 378 |
|
| 379 |
if let Setting::Choice { options, default } = knob.value |
| 380 |
&& !options.contains(&default) |
| 381 |
{ |
| 382 |
return Err(Fault::DefaultNotOffered { |
| 383 |
kind: kind.id, |
| 384 |
id: knob.id, |
| 385 |
}); |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
Ok(()) |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
fn is_key(id: &str) -> bool { |
| 399 |
!id.is_empty() |
| 400 |
&& id |
| 401 |
.chars() |
| 402 |
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-') |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 407 |
pub enum Fault { |
| 408 |
|
| 409 |
BadId { |
| 410 |
|
| 411 |
id: &'static str, |
| 412 |
}, |
| 413 |
|
| 414 |
TwoKinds { |
| 415 |
|
| 416 |
id: &'static str, |
| 417 |
}, |
| 418 |
|
| 419 |
TwoKnobs { |
| 420 |
|
| 421 |
kind: &'static str, |
| 422 |
|
| 423 |
id: &'static str, |
| 424 |
}, |
| 425 |
|
| 426 |
DefaultNotOffered { |
| 427 |
|
| 428 |
kind: &'static str, |
| 429 |
|
| 430 |
id: &'static str, |
| 431 |
}, |
| 432 |
} |
| 433 |
|
| 434 |
impl std::fmt::Display for Fault { |
| 435 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 436 |
match self { |
| 437 |
Self::BadId { id } => { |
| 438 |
write!( |
| 439 |
f, |
| 440 |
"`{id}` cannot be a config key: lowercase, digits and hyphens only" |
| 441 |
) |
| 442 |
} |
| 443 |
Self::TwoKinds { id } => write!(f, "two kinds are declared as `{id}`"), |
| 444 |
Self::TwoKnobs { kind, id } => { |
| 445 |
write!(f, "`{kind}` declares two knobs as `{id}`") |
| 446 |
} |
| 447 |
Self::DefaultNotOffered { kind, id } => { |
| 448 |
write!(f, "`{kind}.{id}` defaults to something it does not offer") |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
impl std::error::Error for Fault {} |
| 455 |
|
| 456 |
#[cfg(test)] |
| 457 |
mod tests { |
| 458 |
use super::*; |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
static KINDS: &[Kind] = &[ |
| 463 |
Kind::new( |
| 464 |
"snooze-expiry", |
| 465 |
"Snoozed items resurface", |
| 466 |
"When something you snoozed comes back.", |
| 467 |
"Reminders", |
| 468 |
) |
| 469 |
.shipping_on(), |
| 470 |
Kind::new( |
| 471 |
"digest", |
| 472 |
"Daily digest", |
| 473 |
"One summary of the day, at a time you pick.", |
| 474 |
"Summaries", |
| 475 |
) |
| 476 |
.with(&[ |
| 477 |
Knob::new("at", "When", Setting::Seconds(9 * 3600)), |
| 478 |
Knob::new( |
| 479 |
"include", |
| 480 |
"What to include", |
| 481 |
Setting::Choice { |
| 482 |
options: &["everything", "overdue only"], |
| 483 |
default: "overdue only", |
| 484 |
}, |
| 485 |
), |
| 486 |
]), |
| 487 |
]; |
| 488 |
|
| 489 |
static NOTIFS: Registry = Registry::new(KINDS); |
| 490 |
|
| 491 |
#[test] |
| 492 |
fn a_registry_is_a_constant_and_a_valid_one() { |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
assert!(NOTIFS.check().is_ok()); |
| 497 |
assert_eq!(NOTIFS.kinds().len(), 2); |
| 498 |
} |
| 499 |
|
| 500 |
#[test] |
| 501 |
fn a_kind_ships_off_unless_it_says_otherwise() { |
| 502 |
|
| 503 |
|
| 504 |
assert_eq!(NOTIFS.kind("digest").unwrap().ships, Posture::Off); |
| 505 |
assert_eq!(NOTIFS.kind("snooze-expiry").unwrap().ships, Posture::On); |
| 506 |
} |
| 507 |
|
| 508 |
#[test] |
| 509 |
fn a_knob_carries_its_type_and_its_default_as_one_value() { |
| 510 |
let digest = NOTIFS.kind("digest").unwrap(); |
| 511 |
assert_eq!(digest.knob("at").unwrap().value, Setting::Seconds(9 * 3600)); |
| 512 |
assert_eq!(digest.options.len(), 2); |
| 513 |
|
| 514 |
assert!(NOTIFS.kind("snooze-expiry").unwrap().options.is_empty()); |
| 515 |
} |
| 516 |
|
| 517 |
#[test] |
| 518 |
fn categories_keep_the_order_the_app_declared_them_in() { |
| 519 |
|
| 520 |
|
| 521 |
assert_eq!(NOTIFS.categories(), vec!["Reminders", "Summaries"]); |
| 522 |
} |
| 523 |
|
| 524 |
#[test] |
| 525 |
fn an_unknown_id_is_absent_rather_than_a_panic() { |
| 526 |
assert!(NOTIFS.kind("nope").is_none()); |
| 527 |
assert!(NOTIFS.kind("digest").unwrap().knob("nope").is_none()); |
| 528 |
} |
| 529 |
|
| 530 |
#[test] |
| 531 |
fn two_kinds_under_one_id_would_share_a_config_key() { |
| 532 |
static CLASH: &[Kind] = &[ |
| 533 |
Kind::new("digest", "One", "First.", "A"), |
| 534 |
Kind::new("digest", "Two", "Second.", "B"), |
| 535 |
]; |
| 536 |
assert_eq!( |
| 537 |
Registry::new(CLASH).check(), |
| 538 |
Err(Fault::TwoKinds { id: "digest" }) |
| 539 |
); |
| 540 |
} |
| 541 |
|
| 542 |
#[test] |
| 543 |
fn two_knobs_under_one_id_would_too() { |
| 544 |
static CLASH: &[Kind] = &[Kind::new("digest", "Digest", "Daily.", "A").with(&[ |
| 545 |
Knob::new("at", "When", Setting::Seconds(0)), |
| 546 |
Knob::new("at", "Also when", Setting::Count(0)), |
| 547 |
])]; |
| 548 |
assert_eq!( |
| 549 |
Registry::new(CLASH).check(), |
| 550 |
Err(Fault::TwoKnobs { |
| 551 |
kind: "digest", |
| 552 |
id: "at" |
| 553 |
}) |
| 554 |
); |
| 555 |
} |
| 556 |
|
| 557 |
#[test] |
| 558 |
fn an_id_that_cannot_be_a_config_key_is_refused() { |
| 559 |
|
| 560 |
|
| 561 |
for bad in ["Digest", "daily.digest", "daily digest", ""] { |
| 562 |
let kinds: &'static [Kind] = Box::leak(Box::new([Kind::new(bad, "T", "S", "C")])); |
| 563 |
assert_eq!( |
| 564 |
Registry::new(kinds).check(), |
| 565 |
Err(Fault::BadId { id: bad }), |
| 566 |
"{bad:?} should be refused" |
| 567 |
); |
| 568 |
} |
| 569 |
|
| 570 |
static GOOD: &[Kind] = &[Kind::new("snooze-expiry", "T", "S", "C")]; |
| 571 |
assert!(Registry::new(GOOD).check().is_ok()); |
| 572 |
} |
| 573 |
|
| 574 |
#[test] |
| 575 |
fn a_choice_cannot_default_to_something_it_does_not_offer() { |
| 576 |
static BAD: &[Kind] = &[ |
| 577 |
Kind::new("digest", "Digest", "Daily.", "A").with(&[Knob::new( |
| 578 |
"include", |
| 579 |
"What", |
| 580 |
Setting::Choice { |
| 581 |
options: &["everything"], |
| 582 |
default: "overdue only", |
| 583 |
}, |
| 584 |
)]), |
| 585 |
]; |
| 586 |
assert_eq!( |
| 587 |
Registry::new(BAD).check(), |
| 588 |
Err(Fault::DefaultNotOffered { |
| 589 |
kind: "digest", |
| 590 |
id: "include" |
| 591 |
}) |
| 592 |
); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn a_fault_says_which_declaration_is_wrong() { |
| 597 |
|
| 598 |
|
| 599 |
assert!( |
| 600 |
Fault::TwoKnobs { |
| 601 |
kind: "digest", |
| 602 |
id: "at" |
| 603 |
} |
| 604 |
.to_string() |
| 605 |
.contains("`digest` declares two knobs as `at`") |
| 606 |
); |
| 607 |
} |
| 608 |
} |
| 609 |
|