Cotton code
130 lines · 4.8 KB · RustRaw
//! The emotion vocabulary — ONE declaration, for every surface that names a//! feeling.//!//! A mood used to be a number from one to five. It is a MOMENT now: what a//! person said about how they are going, and the emotions named in it. The//! words have to come from somewhere fixed, and this is that somewhere.//!//! **Why a fixed set and not free tags.** The classifier picks from a JSON//! schema `enum`, so it needs a list to choose from; "anxious", "anxiety" and//! "stressed" as three free strings make every later read meaningless; a//! "most-named this week" line needs a stable key to count; and//! [english-only-no-translations] makes one English declaration the correct//! shape. Anything outside the set lives in the moment's `line`, which is the//! free surface and the one the search index reads.//!//! **Flat, and ALPHABETICAL.** No valence grouping and no declared order that//! runs from bad to good — either would rebuild the 1-to-5 scale under another//! name, which is the thing this vocabulary replaces. `awful`, `low`, `okay`,//! `good` and `great` are in the list because the five faces the phone used to//! draw had exactly those words, and the version-18 migration converts a stored//! score into one of them; they carry no more weight here than `hopeful` does.//!//! **There are three copies of this list and never a fourth.** The Rust here is//! the declaration; `cotton-gen` renders the Swift//! (`apps/cotton/Sources/CottonUI/Generated/Emotions.swift`, held by//! `pnpm gen:design:check`), and the `record_mood` catalogue entry carries the//! JSON-schema `enum`, held equal by `cotton-tools/tests/catalogue_words.rs`.//! Adding a word means editing THIS list and regenerating; a hand-written//! fourth copy is drift with nothing watching it./// Every emotion a moment may name, alphabetical.////// The slug IS the display word: lowercase, English, one word. A tool, a chip/// on the phone and a briefing line all say the same string, so nothing has to/// map between a key and a label.pub const EMOTIONS: &[&str] = &[    "afraid",    "angry",    "anxious",    "ashamed",    "awful",    "bored",    "calm",    "content",    "energised",    "excited",    "focused",    "frustrated",    "good",    "grateful",    "great",    "guilty",    "happy",    "hopeful",    "hurt",    "lonely",    "loved",    "low",    "numb",    "okay",    "overwhelmed",    "proud",    "relieved",    "restless",    "sad",    "stressed",    "tired",];/// Is this exactly one of the declared words?////// Exact, because the caller that matters is a validator: a tool refusing/// "Anxious " with a sentence is better than one that quietly accepts a word/// no other surface will ever match.pub fn is_emotion(word: &str) -> bool {    EMOTIONS.contains(&word)}/// The declared word a caller meant, or nothing.////// Trims and lowercases — a chip tapped on a phone and a word typed by an/// assistant arrive differently and mean the same thing — and answers the/// `&'static str` from [`EMOTIONS`], so everything downstream holds the/// declaration's own bytes rather than the caller's.pub fn normalise(word: &str) -> Option<&'static str> {    let wanted = word.trim().to_lowercase();    EMOTIONS.iter().copied().find(|known| *known == wanted)}/// The emotions stored on a row, read back from its JSON array.////// UNKNOWN WORDS ARE DROPPED, not carried: the column is written only through/// [`normalise`], so a word that is not in the vocabulary got there by a hand/// edit or by a build that declared more words than this one, and rendering it/// would put a label on a screen that no chip, filter or tally can match./// A column that is not an array at all reads as no emotions, which is what an/// empty moment already means.pub fn parse_emotions(stored: &str) -> Vec<String> {    let Ok(serde_json::Value::Array(values)) = serde_json::from_str::<serde_json::Value>(stored)    else {        return Vec::new();    };    let mut out: Vec<String> = Vec::new();    for value in values {        let Some(word) = value.as_str().and_then(normalise) else {            continue;        };        if !out.iter().any(|kept| kept == word) {            out.push(word.to_string());        }    }    out}/// The JSON array a row stores. `[]` is a moment with no emotion named, which/// is a real answer — the person said something and nobody has named the/// feeling in it yet.pub fn encode_emotions(emotions: &[String]) -> String {    serde_json::to_string(emotions).unwrap_or_else(|_| "[]".to_string())}/// The words as a person reads them: `tired, hopeful`. Empty for none, so a/// caller can ask `is_empty()` rather than compare against a placeholder.pub fn render_emotions(emotions: &[String]) -> String {    emotions.join(", ")}#[cfg(test)]#[path = "emotions_tests.rs"]mod tests;