//! Story's writing data, read from the kit (`kits/story/writing.json`): the //! questions Your story asks in order, what each time of day is called and //! the colour of its dot, and the tints characters wear. The words of the //! questions are the `story` line set's (`question.`). use std::sync::OnceLock; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct TimeBand { pub band: String, pub dot: String, pub words: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Tint { pub key: String, pub ink: String, pub paper: String, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Writing { pub format_version: u32, pub questions: Vec, pub times: Vec, pub tints: Vec, } /// The kit's writing data. pub fn writing() -> &'static Writing { static WRITING: OnceLock = OnceLock::new(); WRITING.get_or_init(|| serde_json::from_str(include_str!("../../kits/story/writing.json")).expect("the kit's writing data reads")) } /// The time of day a scene's time says: its whole words matched, the longest /// name first, so LATE NIGHT is night and LATER THAT NIGHT is too. pub fn band_of(time: &str) -> Option<&'static TimeBand> { let said: Vec = time.to_uppercase().split(|c: char| !c.is_alphanumeric()).filter(|word| !word.is_empty()).map(str::to_string).collect(); let holds = |name: &str| { let wanted: Vec<&str> = name.split_whitespace().collect(); !wanted.is_empty() && said.windows(wanted.len()).any(|run| run.iter().map(String::as_str).eq(wanted.iter().copied())) }; let mut named: Vec<(&TimeBand, &String)> = writing().times.iter().flat_map(|band| band.words.iter().map(move |word| (band, word))).collect(); named.sort_by_key(|(_, word)| std::cmp::Reverse(word.split_whitespace().count())); // A night named anywhere in the time outranks a softer word beside it. let night = named.iter().find(|(band, word)| band.band == "night" && holds(word)).map(|(band, _)| *band); night.or_else(|| named.iter().find(|(_, word)| holds(word)).map(|(band, _)| *band)) } /// The tint a character wears: the one they keep, else one their name always /// gives, so a story written before tints reads the same on every phone. pub fn tint_of(kept: &str, name: &str) -> &'static Tint { let tints = &writing().tints; tints.iter().find(|tint| tint.key == kept).unwrap_or_else(|| { let hash = name.trim().to_lowercase().bytes().fold(0x811c_9dc5_u32, |hash, byte| (hash ^ u32::from(byte)).wrapping_mul(0x0100_0193)); &tints[hash as usize % tints.len()] }) } /// The tint for a new character: the first the story's characters do not /// wear yet, else the one their name gives. pub fn next_tint(worn: &[String], name: &str) -> &'static Tint { writing().tints.iter().find(|tint| !worn.contains(&tint.key)).unwrap_or_else(|| tint_of("", name)) } #[cfg(test)] #[path = "story_writing_tests.rs"] mod tests; /// Story's themes, kit data (`kits/story/themes.json`; owner, 19 Nov 2026): /// a theme for the light appearance and one for the dark, the plain pair by /// default, the paper look (its typewriter and its hand) one more a person /// may choose. Each names its look, its faces' files and its colours; its /// name is the `story` line set's `theme.`. pub fn themes() -> &'static serde_json::Value { static THEMES: OnceLock = OnceLock::new(); THEMES.get_or_init(|| serde_json::from_str(include_str!("../../kits/story/themes.json")).expect("the kit's themes read")) }