Cotton code
101 lines · 3.0 KB · RustRaw
//! Every word Days says, per language (`words.json`). A new language is a//! new entry in the file, never new code; a language the file does not hold//! reads as English.use std::collections::HashMap;use std::sync::OnceLock;use serde::Deserialize;#[derive(Debug, Clone, Deserialize)]pub struct Words {    pub weekdays: Vec<String>,    pub weekdays_short: Vec<String>,    pub months: Vec<String>,    pub months_short: Vec<String>,    pub numbers: Vec<String>,    pub to: String,    pub from: String,    pub birthday: String,    pub quiet: String,    pub quiet_one: String,    pub all: String,    pub more: String,    pub no_group: String,    pub month_title: String,    pub pill_day: String,    pub pill_join: String,    pub no_day: String,    pub google_says: String,    pub yours_says: String,    pub google_read_only: String,    pub not_repeating: String,    pub not_found: String,    pub end_after_this: String,    pub end_after_today: String,    pub end_group_after_this: String,    pub end_group_after_today: String,    pub every_day: String,    pub every_n_days: String,    pub every_weekday: String,    pub every_week_on: String,    pub every_n_weeks_on: String,    pub every_month: String,    pub every_n_months: String,    pub every_month_on: String,    pub every_n_months_on: String,    pub every_year_on: String,    pub every_n_years_on: String,    pub repeats: String,    pub and: String,    pub late: String,    pub repeating: String,    pub dropped: String,    pub done: String,    pub nothing_late: String,    pub nothing_repeats: String,    pub nothing_dropped: String,    pub nothing_done: String,    pub nothing_found: String,    pub this_year_day: String,    pub other_year_day: String,    pub ordinals: Vec<String>,}fn all() -> &'static HashMap<String, Words> {    static ALL: OnceLock<HashMap<String, Words>> = OnceLock::new();    ALL.get_or_init(|| {        let raw: serde_json::Map<String, serde_json::Value> =            serde_json::from_str(include_str!("words.json")).expect("days/words.json reads");        raw.into_iter()            .filter(|(key, _)| key != "about")            .map(|(key, value)| (key, serde_json::from_value(value).expect("a days words entry reads")))            .collect()    })}/// The words of `language` (`en`, `hi`, …), English where the file has none.pub fn words(language: &str) -> &'static Words {    let all = all();    all.get(language).or_else(|| all.get("en")).expect("days/words.json holds en")}/// `template` with each `{name}` filled.pub fn fill(template: &str, values: &[(&str, &str)]) -> String {    let mut out = template.to_string();    for (name, value) in values {        out = out.replace(&format!("{{{name}}}"), value);    }    out}impl Words {    /// A count in words ("four"), digits past the words the file holds.    pub fn count(&self, n: i64) -> String {        usize::try_from(n)            .ok()            .and_then(|at| self.numbers.get(at))            .cloned()            .unwrap_or_else(|| n.to_string())    }}