73 lines · 3.4 KB · RustRaw
//! Story's themes (owner, 19 Nov 2026): one theme for the phone's light//! appearance and one for its dark, read from the kit//! (`kits/story/themes.json`). The plain pair stands until the person picks//! another, and the pick is this phone's own, kept beside the copy//! (`story-theme.json`), as the fan keeps the view it stood on.//!//! `story.themes {}` → `{ themes: [theme…], light: id, dark: id, words: { title, light, dark } }`,//! a theme being the kit's (`id, dark, look: "plain"|"paper", faces: { text,//! voice, script }, tint_strength, colours: { ground, sheet, text, dim, faint,//! rule, accent, on_accent, answer, note, insert, mic }, voice_scale`) with//! its `name`.//! `story.theme.set { appearance: "light"|"dark", id }` answers the same.use serde_json::{json, Map, Value};use cotton_core::rules::story_writing::themes as kit;use super::line;use crate::methods::MethodError;use crate::shell::client::ClientController;const FILE: &str = "story-theme.json";fn kept(client: &ClientController) -> Map<String, Value> { std::fs::read(client.layout().client_dir().join(FILE)).ok().and_then(|bytes| serde_json::from_slice(&bytes).ok()).unwrap_or_default()}fn known(id: &str, dark: bool) -> bool { kit()["themes"].as_array().is_some_and(|all| all.iter().any(|theme| theme["id"] == json!(id) && theme["dark"] == json!(dark)))}/// The theme standing for an appearance: the kept pick while the kit still holds it, else the kit's default.fn standing(kept: &Map<String, Value>, appearance: &str) -> Value { let dark = appearance == "dark"; kept.get(appearance).and_then(Value::as_str).filter(|id| known(id, dark)).map(|id| json!(id)).unwrap_or_else(|| kit()["default"][appearance].clone())}pub(super) fn answer(client: &ClientController) -> Result<Value, MethodError> { let kept = kept(client); let themes = kit()["themes"] .as_array() .cloned() .unwrap_or_default() .into_iter() .map(|mut theme| { let id = theme["id"].as_str().unwrap_or_default().to_string(); theme["name"] = json!(line(&format!("theme.{id}"), &[])?); Ok(theme) }) .collect::<Result<Vec<Value>, MethodError>>()?; Ok(json!({ "themes": themes, "light": standing(&kept, "light"), "dark": standing(&kept, "dark"), "words": { "title": line("theme.title", &[])?, "light": line("theme.light", &[])?, "dark": line("theme.dark", &[])? }, }))}pub(super) fn set(client: &ClientController, request: &Value) -> Result<Value, MethodError> { let appearance = match request["appearance"].as_str() { Some(said @ ("light" | "dark")) => said, _ => return Err(MethodError::new("bad_request", "appearance is light or dark")), }; let id = request["id"].as_str().filter(|id| known(id, appearance == "dark")).ok_or_else(|| MethodError::new("not_found", "the kit holds no such theme for that appearance"))?; let mut file = kept(client); file.insert(appearance.to_string(), json!(id)); let dir = client.layout().client_dir(); let bytes = serde_json::to_vec(&file).map_err(|why| MethodError::new("unwritable", why.to_string()))?; // not a copy: Story's themes file. cotton_client::whole_file::replace(&dir.join(FILE), &bytes) .map_err(|why| MethodError::new("unwritable", format!("the theme could not be kept: {why}")))?; answer(client)}