| 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 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
use quasi_declare::declare; |
| 82 |
use quasi_router::layout::Tone; |
| 83 |
use quasi_router::{Action, Choice, Locating, Outcome, Request, Response, RouteError, Router, Tag}; |
| 84 |
|
| 85 |
use super::Panels; |
| 86 |
|
| 87 |
|
| 88 |
const FOLDER: &str = "folder"; |
| 89 |
|
| 90 |
|
| 91 |
const BROWSER: &str = "/"; |
| 92 |
|
| 93 |
|
| 94 |
const NAME: &str = "name"; |
| 95 |
|
| 96 |
|
| 97 |
const INTERRUPT: &str = |
| 98 |
"An import or bulk action is running and will be cancelled. Open this library anyway?"; |
| 99 |
|
| 100 |
|
| 101 |
const FORGET: &str = |
| 102 |
"Remove this library from the list? Its files and database are left where they are."; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
const STYLE_HINT: &str = "Cannot be changed after the library is created."; |
| 107 |
|
| 108 |
|
| 109 |
pub fn routes(router: Router<Panels<'_>>) -> Router<Panels<'_>> { |
| 110 |
router |
| 111 |
.post("/settings/storage/open/{at}", open) |
| 112 |
.post("/settings/storage/rename/{at}", rename_row) |
| 113 |
.post("/settings/storage/rename/{at}/save", rename) |
| 114 |
.post("/settings/storage/cancel-rename", cancel_rename) |
| 115 |
.post("/settings/storage/forget/{at}", forget) |
| 116 |
.post("/settings/storage/locate/{at}", locate) |
| 117 |
.post("/settings/storage/relocate/{at}", relocate) |
| 118 |
.post("/settings/storage/scan", scan) |
| 119 |
.post("/settings/storage/orphans", orphans) |
| 120 |
.post("/settings/storage/backfill", backfill) |
| 121 |
.post("/settings/storage/verify", verify) |
| 122 |
.post("/settings/storage/folder", folder) |
| 123 |
.post("/settings/storage/draft/{key}", draft) |
| 124 |
.post("/settings/storage/create", create) |
| 125 |
.post("/settings/storage/add", add_existing) |
| 126 |
.post("/settings/storage/discard", discard) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
fn open(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 135 |
state.storage.open(row(&request)?); |
| 136 |
Ok(Response::from(Outcome::Goto(Action::get(BROWSER)))) |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
fn rename_row(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 145 |
state.storage.rename_row(Some(row(&request)?)); |
| 146 |
settled(state) |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
fn rename(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 155 |
let at = row(&request)?; |
| 156 |
let typed = request.payload.get(NAME).unwrap_or_default().trim(); |
| 157 |
if !typed.is_empty() { |
| 158 |
state.storage.rename(at, typed); |
| 159 |
} |
| 160 |
state.storage.rename_row(None); |
| 161 |
settled(state) |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
fn cancel_rename(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 166 |
state.storage.rename_row(None); |
| 167 |
settled(state) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
fn forget(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 172 |
state.storage.forget(row(&request)?); |
| 173 |
settled(state) |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
fn locate(_state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 182 |
let at = row(&request)?; |
| 183 |
Ok(Response::locate(Locating::folder( |
| 184 |
"Locate library directory", |
| 185 |
Action::post(format!("/settings/storage/relocate/{at}")), |
| 186 |
FOLDER, |
| 187 |
))) |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
fn relocate(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 192 |
let at = row(&request)?; |
| 193 |
let folder = request.payload.get(FOLDER).unwrap_or_default(); |
| 194 |
if !folder.is_empty() { |
| 195 |
state.storage.relocate(at, folder); |
| 196 |
} |
| 197 |
settled(state) |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
fn scan(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 202 |
state.storage.rescan(); |
| 203 |
settled(state) |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
fn orphans(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 208 |
state.storage.cleanup_orphans(); |
| 209 |
settled(state) |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
fn backfill(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 214 |
state.storage.backfill(); |
| 215 |
settled(state) |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
fn verify(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 220 |
state.storage.verify(); |
| 221 |
settled(state) |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
fn folder(_state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 230 |
Ok(Response::locate(Locating::folder( |
| 231 |
"Choose folder", |
| 232 |
Action::post("/settings/storage/draft/folder"), |
| 233 |
FOLDER, |
| 234 |
))) |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
fn draft(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 244 |
let key = request.captures.require("key")?; |
| 245 |
match key { |
| 246 |
NAME => state |
| 247 |
.storage |
| 248 |
.draft_name(request.payload.get(NAME).unwrap_or_default()), |
| 249 |
FOLDER => { |
| 250 |
let folder = request.payload.get(FOLDER).unwrap_or_default(); |
| 251 |
if !folder.is_empty() { |
| 252 |
state.storage.draft_folder(folder); |
| 253 |
} |
| 254 |
} |
| 255 |
"style" => { |
| 256 |
let style = request.payload.get("style").unwrap_or_default(); |
| 257 |
state.storage.draft_style(style == "reference"); |
| 258 |
} |
| 259 |
_ => return Err(RouteError::not_found("no such field")), |
| 260 |
} |
| 261 |
settled(state) |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
fn create(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 266 |
if !state.storage.draft().ready() { |
| 267 |
return Err(RouteError::not_found("the form is not finished")); |
| 268 |
} |
| 269 |
state.storage.create(); |
| 270 |
Ok(Response::from(Outcome::Goto(Action::get(BROWSER)))) |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
fn add_existing(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 275 |
if !state.storage.draft().ready() { |
| 276 |
return Err(RouteError::not_found("the form is not finished")); |
| 277 |
} |
| 278 |
state.storage.add_existing(); |
| 279 |
Ok(Response::from(Outcome::Goto(Action::get(BROWSER)))) |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
fn discard(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 284 |
state.storage.discard(); |
| 285 |
settled(state) |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
fn settled(state: &Panels<'_>) -> Result<Response, RouteError> { |
| 290 |
super::settings::showing(state) |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
fn row(request: &Request) -> Result<usize, RouteError> { |
| 295 |
request |
| 296 |
.captures |
| 297 |
.require("at")? |
| 298 |
.parse() |
| 299 |
.map_err(|_| RouteError::not_found("that is not a row")) |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
pub(super) struct Storage { |
| 304 |
|
| 305 |
libraries: Vec<Library>, |
| 306 |
|
| 307 |
renaming: Option<Renaming>, |
| 308 |
|
| 309 |
maintenance: Maintenance, |
| 310 |
|
| 311 |
loose: bool, |
| 312 |
|
| 313 |
draft: Draft, |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
struct Library { |
| 318 |
|
| 319 |
name: String, |
| 320 |
|
| 321 |
shown: String, |
| 322 |
|
| 323 |
badge: Option<&'static str>, |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
meta: Option<String>, |
| 332 |
|
| 333 |
at: usize, |
| 334 |
|
| 335 |
open: Option<Open>, |
| 336 |
|
| 337 |
locatable: bool, |
| 338 |
|
| 339 |
forgettable: bool, |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
struct Open { |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
interrupting: bool, |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
struct Renaming { |
| 354 |
|
| 355 |
at: usize, |
| 356 |
|
| 357 |
current: String, |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
struct Maintenance { |
| 367 |
|
| 368 |
scan: &'static str, |
| 369 |
|
| 370 |
scanning: bool, |
| 371 |
|
| 372 |
counted: Option<Counted>, |
| 373 |
|
| 374 |
backfill: &'static str, |
| 375 |
|
| 376 |
backfilling: bool, |
| 377 |
|
| 378 |
|
| 379 |
busy: bool, |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
struct Counted { |
| 384 |
|
| 385 |
totals: String, |
| 386 |
|
| 387 |
age: String, |
| 388 |
|
| 389 |
tone: Tone, |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
struct Draft { |
| 394 |
|
| 395 |
name: String, |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
error: Option<&'static str>, |
| 401 |
|
| 402 |
folder: Option<String>, |
| 403 |
|
| 404 |
style: &'static str, |
| 405 |
|
| 406 |
loose: bool, |
| 407 |
|
| 408 |
ready: bool, |
| 409 |
|
| 410 |
started: bool, |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
pub(super) fn read(state: &Panels<'_>) -> Storage { |
| 415 |
let all = state.storage.libraries(); |
| 416 |
let scan = state.storage.scan(); |
| 417 |
let interrupting = state.storage.interrupting(); |
| 418 |
let draft = state.storage.draft(); |
| 419 |
let scanning = state.storage.scanning(); |
| 420 |
let backfilling = state.storage.backfilling(); |
| 421 |
Storage { |
| 422 |
libraries: all |
| 423 |
.iter() |
| 424 |
.enumerate() |
| 425 |
.map(|(at, entry)| Library { |
| 426 |
name: entry.name.clone(), |
| 427 |
shown: entry.shown.clone(), |
| 428 |
badge: if entry.active { |
| 429 |
Some("active") |
| 430 |
} else if entry.reachable { |
| 431 |
None |
| 432 |
} else { |
| 433 |
Some("offline") |
| 434 |
}, |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
meta: match (entry.active, entry.reachable, scan) { |
| 439 |
(true, _, Some(scan)) => Some(format!( |
| 440 |
"{} samples \u{b7} {}", |
| 441 |
scan.samples, |
| 442 |
bytes(scan.total_bytes), |
| 443 |
)), |
| 444 |
(false, false, _) => Some(format!("Last known path: {}", entry.path)), |
| 445 |
_ => None, |
| 446 |
}, |
| 447 |
at, |
| 448 |
open: (!entry.active && entry.reachable).then_some(Open { interrupting }), |
| 449 |
locatable: !entry.active && !entry.reachable, |
| 450 |
forgettable: !entry.active, |
| 451 |
}) |
| 452 |
.collect(), |
| 453 |
renaming: state.storage.renaming().map(|at| Renaming { |
| 454 |
at, |
| 455 |
current: all |
| 456 |
.get(at) |
| 457 |
.map(|entry| entry.name.clone()) |
| 458 |
.unwrap_or_default(), |
| 459 |
}), |
| 460 |
maintenance: Maintenance { |
| 461 |
scan: if scanning { "Scanning..." } else { "Scan" }, |
| 462 |
scanning, |
| 463 |
counted: scan.map(|scan| { |
| 464 |
let (age, stale) = scan_age(scan.age_secs); |
| 465 |
Counted { |
| 466 |
totals: format!( |
| 467 |
"{} samples, {} total, {} database", |
| 468 |
scan.samples, |
| 469 |
bytes(scan.total_bytes), |
| 470 |
bytes(scan.db_bytes), |
| 471 |
), |
| 472 |
age, |
| 473 |
tone: if stale { Tone::Warning } else { Tone::Neutral }, |
| 474 |
} |
| 475 |
}), |
| 476 |
backfill: if backfilling { |
| 477 |
"Backfilling audio features..." |
| 478 |
} else { |
| 479 |
"Backfill audio features" |
| 480 |
}, |
| 481 |
backfilling, |
| 482 |
busy: state.storage.busy(), |
| 483 |
}, |
| 484 |
loose: state.storage.loose_files(), |
| 485 |
draft: Draft { |
| 486 |
error: (draft.folder.is_some() && draft.name.trim().is_empty()) |
| 487 |
.then_some("A library needs a name."), |
| 488 |
style: if draft.reference_in_place { |
| 489 |
"reference" |
| 490 |
} else { |
| 491 |
"copy" |
| 492 |
}, |
| 493 |
loose: draft.reference_in_place, |
| 494 |
ready: draft.ready(), |
| 495 |
started: draft.started(), |
| 496 |
name: draft.name, |
| 497 |
folder: draft.folder, |
| 498 |
}, |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
declare! { |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
pub(super) shape section(storage: &Storage) -> Vec<Node>; |
| 510 |
|
| 511 |
section "Storage"; |
| 512 |
text "Each library is an independent sample collection with its own \ |
| 513 |
database and files. A library can contain multiple vaults (top-level \ |
| 514 |
browse buckets)."; |
| 515 |
include libraries(storage); |
| 516 |
|
| 517 |
for renaming in storage.renaming.iter() { |
| 518 |
include rename_form(renaming); |
| 519 |
} |
| 520 |
|
| 521 |
extend maintenance(&storage.maintenance); |
| 522 |
|
| 523 |
toned "This library uses loose-files mode. Samples are referenced in place, \ |
| 524 |
not duplicated." Tone::Warning when storage.loose; |
| 525 |
|
| 526 |
extend add_library(&storage.draft); |
| 527 |
} |
| 528 |
|
| 529 |
declare! { |
| 530 |
|
| 531 |
shape libraries(storage: &Storage) -> Node; |
| 532 |
|
| 533 |
list { |
| 534 |
for library in storage.libraries.iter() { |
| 535 |
row &library.name { |
| 536 |
secondary &library.shown; |
| 537 |
for &badge in library.badge.iter() { |
| 538 |
token Tag::badge(badge); |
| 539 |
} |
| 540 |
for line in library.meta.iter() { |
| 541 |
meta line; |
| 542 |
} |
| 543 |
|
| 544 |
for open in library.open.iter() { |
| 545 |
|
| 546 |
|
| 547 |
activate to post "/settings/storage/open/{library.at}" |
| 548 |
unless open.interrupting; |
| 549 |
act "Open" to post "/settings/storage/open/{library.at}" { |
| 550 |
confirm INTERRUPT when open.interrupting; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
act "Rename" to post "/settings/storage/rename/{library.at}"; |
| 555 |
act "Locate" to post "/settings/storage/locate/{library.at}" |
| 556 |
when library.locatable; |
| 557 |
act "Remove" to post "/settings/storage/forget/{library.at}" |
| 558 |
when library.forgettable { |
| 559 |
tone Danger; |
| 560 |
confirm FORGET; |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
declare! { |
| 568 |
|
| 569 |
shape rename_form(renaming: &Renaming) -> Node; |
| 570 |
|
| 571 |
form post "/settings/storage/rename/{renaming.at}/save" { |
| 572 |
submit "Save"; |
| 573 |
field Text NAME "New name" { |
| 574 |
value &renaming.current; |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
declare! { |
| 580 |
|
| 581 |
|
| 582 |
shape maintenance(maintenance: &Maintenance) -> Vec<Node>; |
| 583 |
|
| 584 |
act maintenance.scan to post "/settings/storage/scan" { |
| 585 |
disabled when maintenance.scanning; |
| 586 |
} |
| 587 |
for counted in maintenance.counted.iter() { |
| 588 |
text &counted.totals; |
| 589 |
toned &counted.age counted.tone; |
| 590 |
} |
| 591 |
|
| 592 |
text "Free disk by deleting samples no longer referenced anywhere in the \ |
| 593 |
library. Local-only: other synced devices keep their own copies."; |
| 594 |
act "Cleanup orphans" to post "/settings/storage/orphans"; |
| 595 |
|
| 596 |
text "Compute the audio feature data used by tag suggestions for samples \ |
| 597 |
that don't have it yet. Runs in the background: keep working; it \ |
| 598 |
yields to any analysis you start and resumes later."; |
| 599 |
act maintenance.backfill to post "/settings/storage/backfill" { |
| 600 |
disabled when maintenance.backfilling; |
| 601 |
} |
| 602 |
|
| 603 |
text "Re-hash every stored sample and confirm its bytes still match its \ |
| 604 |
content address. Catches silent on-disk corruption. Runs in the \ |
| 605 |
background: the result appears in the status line."; |
| 606 |
act "Verify library integrity" to post "/settings/storage/verify" { |
| 607 |
disabled when maintenance.busy; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
declare! { |
| 612 |
|
| 613 |
shape add_library(draft: &Draft) -> Vec<Node>; |
| 614 |
|
| 615 |
section "Add Library"; |
| 616 |
field Text "create_name" "Name" { |
| 617 |
value &draft.name; |
| 618 |
required; |
| 619 |
for &why in draft.error.iter() { |
| 620 |
error why; |
| 621 |
} |
| 622 |
writes Action::post("/settings/storage/draft/name"); |
| 623 |
} |
| 624 |
act "Choose folder..." to post "/settings/storage/folder"; |
| 625 |
for folder in draft.folder.iter() { |
| 626 |
text folder; |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
field Radio "style" "Storage style" { |
| 634 |
option Choice::new("copy", "Copy samples into library (recommended)"); |
| 635 |
option Choice::new("reference", "Reference samples in place (loose-files mode)"); |
| 636 |
value draft.style; |
| 637 |
hint STYLE_HINT; |
| 638 |
writes Action::post("/settings/storage/draft/style"); |
| 639 |
} |
| 640 |
toned "Moving or deleting originals will break references. This cannot be \ |
| 641 |
undone." Tone::Warning when draft.loose; |
| 642 |
|
| 643 |
text "Create New makes an empty library in that folder. Add Existing adopts \ |
| 644 |
one that is already there."; |
| 645 |
act "Create New" to post "/settings/storage/create" { |
| 646 |
disabled unless draft.ready; |
| 647 |
} |
| 648 |
act "Add Existing" to post "/settings/storage/add" { |
| 649 |
disabled unless draft.ready; |
| 650 |
} |
| 651 |
act "Cancel" to post "/settings/storage/discard" { |
| 652 |
disabled unless draft.started; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
fn bytes(count: u64) -> String { |
| 658 |
crate::ui::widgets::format_bytes(count) |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
fn scan_age(age_secs: i64) -> (String, bool) { |
| 666 |
let stale = age_secs >= 86_400; |
| 667 |
|
| 668 |
|
| 669 |
let suffix = if stale { " Re-scan to refresh." } else { "" }; |
| 670 |
let text = if age_secs < 120 { |
| 671 |
format!("Last scanned just now.{suffix}") |
| 672 |
} else if age_secs < 3_600 { |
| 673 |
format!("Last scanned {} minutes ago.{suffix}", age_secs / 60) |
| 674 |
} else if age_secs < 86_400 { |
| 675 |
let hours = age_secs / 3_600; |
| 676 |
format!( |
| 677 |
"Last scanned {hours} hour{} ago.{suffix}", |
| 678 |
if hours == 1 { "" } else { "s" } |
| 679 |
) |
| 680 |
} else { |
| 681 |
let days = age_secs / 86_400; |
| 682 |
format!( |
| 683 |
"Last scanned {days} day{} ago.{suffix}", |
| 684 |
if days == 1 { "" } else { "s" } |
| 685 |
) |
| 686 |
}; |
| 687 |
(text, stale) |
| 688 |
} |
| 689 |
|
| 690 |
#[cfg(test)] |
| 691 |
mod tests { |
| 692 |
use super::scan_age; |
| 693 |
|
| 694 |
#[test] |
| 695 |
fn scan_age_reads_fresh_and_stale() { |
| 696 |
assert_eq!(scan_age(30), ("Last scanned just now.".to_owned(), false)); |
| 697 |
assert_eq!( |
| 698 |
scan_age(300), |
| 699 |
("Last scanned 5 minutes ago.".to_owned(), false) |
| 700 |
); |
| 701 |
assert_eq!( |
| 702 |
scan_age(3_600), |
| 703 |
("Last scanned 1 hour ago.".to_owned(), false) |
| 704 |
); |
| 705 |
assert_eq!( |
| 706 |
scan_age(7_200), |
| 707 |
("Last scanned 2 hours ago.".to_owned(), false) |
| 708 |
); |
| 709 |
let (text, stale) = scan_age(172_800); |
| 710 |
assert!(stale); |
| 711 |
assert_eq!(text, "Last scanned 2 days ago. Re-scan to refresh."); |
| 712 |
assert_eq!( |
| 713 |
scan_age(90_000).0, |
| 714 |
"Last scanned 1 day ago. Re-scan to refresh." |
| 715 |
); |
| 716 |
} |
| 717 |
} |
| 718 |
|