Cotton code
89 lines · 3.4 KB · RustRaw
//! Voice's pure rules: what heard words become as written text. The phone//! core runs them for dictation (`cotton-ffi` `phone::voice`, whose header is//! the platform contract), the Mac for meeting transcripts and the Worker,//! as WASM, for subtitle cues: one casing, one spoken punctuation, one//! spacing and one line breaking everywhere.//!//! - [`language`]: a language is a data file (`languages/<code>.json`).//! - [`format`]: words as heard → text as written.//! - [`session`]: live partials, second passes and the person's edits//!   reconciled into one text that never changes what the person wrote.//! - [`cues`]: timed words → meeting lines and subtitle cues.//!//! At the WASM boundary (`boundary::call_json`): `voice.languages`,//! `voice.format`, `voice.lines` and `voice.cues`.pub mod cues;pub mod format;pub mod language;pub mod session;use serde::Deserialize;use serde_json::{json, Value};#[derive(Deserialize)]struct Common {    #[serde(default)]    languages: Vec<String>,    #[serde(default)]    vocabulary: Vec<String>,    #[serde(default)]    upper_case: bool,}impl Common {    fn rules(&self) -> format::Rules {        let codes: Vec<&str> = self.languages.iter().map(String::as_str).collect();        format::Rules::of(&codes, self.vocabulary.clone())    }}/// The boundary's `voice.*` rules, JSON in and out.pub fn call(name: &str, value: Value) -> Result<Value, String> {    match name {        "voice.languages" => Ok(json!(language::all()            .iter()            .map(|language| json!({ "code": language.language, "name": language.name }))            .collect::<Vec<_>>())),        "voice.format" => {            #[derive(Deserialize)]            struct Input {                text: String,                #[serde(default)]                before: String,                #[serde(flatten)]                common: Common,            }            let input: Input = serde_json::from_value(value).map_err(|error| error.to_string())?;            let rules = input.common.rules();            let context = rules.context_after(&input.before);            let options = format::Options { upper_case: input.common.upper_case, command: false };            Ok(json!({ "text": format::format(&input.text, &rules, context, options) }))        }        "voice.lines" => {            #[derive(Deserialize)]            struct Input {                words: Vec<cues::TimedWord>,                #[serde(flatten)]                common: Common,            }            let input: Input = serde_json::from_value(value).map_err(|error| error.to_string())?;            Ok(json!(cues::lines(&input.words, &input.common.rules(), input.common.upper_case)))        }        "voice.cues" => {            #[derive(Deserialize)]            struct Input {                words: Vec<cues::TimedWord>,                #[serde(default)]                cut: cues::CueRules,                #[serde(flatten)]                common: Common,            }            let input: Input = serde_json::from_value(value).map_err(|error| error.to_string())?;            Ok(json!(cues::cues(&input.words, &input.common.rules(), input.common.upper_case, input.cut)))        }        // The boundary routes only the names above here; any other is the        // boundary's own unknown rule, said by its name.        other => Err(other.to_string()),    }}