//! The one line a landing download says of itself: the kit's words for its//! stage (the fetch table's `stage` labels) with the stage's figures said the//! way a person reads them. A figure is never made up: a stage whose words//! need a figure that is absent is refused.//!//! Vectors: `vectors/fetch_stage.json`.use serde::Deserialize;/// What the line is made from.#[derive(Debug, Clone, Deserialize)]pubstructStage {/// The stage's id: `converting`, `sending`, `sending_original`, `smaller_copies` or `waiting`.pub stage: String,/// The kit's words, with `{done}`, `{of}` and `{since}` where they go.pub words: String, #[serde(default)]pub done: Option<f64>, #[serde(default)]pub of: Option<f64>,/// The clock time on the Mac, already in the person's zone (`19:37`). #[serde(default)]pub since: Option<String>,}/// A whole number with its thousands marked: `3,974`.fn grouped(n: u64) -> String {let digits = n.to_string();letmut out = String::with_capacity(digits.len() + digits.len() / 3);for (at, digit) in digits.chars().enumerate() {if at > 0 && (digits.len() - at) % 3 == 0 { out.push(','); } out.push(digit); } out}/// `done` and `of` in the stage's own unit. `done` never reads past `of`, and/// is rounded down so a stage never claims more than it has finished.fn figures(stage: &str, done: f64, of: f64) -> Result<(String, String), String> {let done = done.clamp(0.0, of);match stage {"converting" | "smaller_copies" => Ok((grouped((done / 60.0).floor() as u64), grouped((of / 60.0).ceil() as u64))),"sending" => Ok((grouped(done.floor() as u64), grouped(of.ceil() as u64))),"sending_original" => {const GB: f64 = 1_000_000_000.0;Ok((format!("{:.1}", (done / GB * 10.0).floor() / 10.0), format!("{:.1}", (of / GB * 10.0).ceil() / 10.0))) } other => Err(format!("fetch.stage_line.no_figures:{other}")), }}/// The line for `stage`, or why it cannot be said.pubfn line(stage: &Stage) -> Result<String, String> {if stage.stage == "waiting" {let since = stage.since.as_deref().filter(|since| !since.trim().is_empty()).ok_or("fetch.stage_line.waiting_needs_since")?;returnOk(stage.words.replace("{since}", since)); }let (Some(done), Some(of)) = (stage.done.filter(|n| n.is_finite()), stage.of.filter(|n| n.is_finite() && *n > 0.0)) else {returnErr(format!("fetch.stage_line.needs_done_and_of:{}", stage.stage)); };let (done, of) = figures(&stage.stage, done, of)?;Ok(stage.words.replace("{done}", &done).replace("{of}", &of))}#[cfg(test)]mod tests {use serde_json::Value; #[test]fn every_line_is_what_the_vectors_say() {let cases: Vec<Value> = serde_json::from_str(include_str!("../vectors/fetch_stage.json")).expect("vectors/fetch_stage.json"); assert!(!cases.is_empty());for case in cases {let answer = crate::boundary::call("fetch.stage_line", &case["in"].to_string());match case.get("line").and_then(Value::as_str) {Some(line) => assert_eq!(answer, Ok(serde_json::json!({ "line": line })), "{case}"),None => assert!(answer.is_err(), "{case}"), } } }}