Cotton code
86 lines · 3.3 KB · RustRaw
//! THE LETTERS WRITTEN: what the ink has narrowed a showing deck or tier to.//! It is a filter by leading letters, not a search (`rules::ink_match`). A//! digit is a letter here like any other. The letters are the search state//! (owner, 3 Nov 2026: "Every time there's some time gap … it starts from//! the beginning. We don't need that"): a letter written always extends//! them, however long after the last; a scratch-out clears them, the pill's//! tap takes the last back and its hold clears them, and what is showing//! changing (another tool, another view) clears them; a page turned keeps//! them.//!//! Two shapes edit them too (owner, 3 Nov 2026: "just a couple of extra//! shapes they can learn"): a short level stroke to the left takes the last//! letter back, and a short level stroke to the right clears them all. Short//! is under `swipe_at_once`; a longer level line is the page swipe as it//! always was, and so is a short one with no letters to edit.use serde::{Deserialize, Serialize};use super::{Knobs, Point};/// A shape that edits the letters written rather than adding one.#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]#[serde(rename_all = "lowercase")]pub enum Edit {    /// A short level stroke to the left, or the pill's tap: the last letter goes.    Backspace,    /// A scratch-out, the pill held, or a short level stroke to the right:    /// they all go.    Clear,}#[derive(Debug, Clone, PartialEq, Eq, Default)]pub struct Letters {    pub letters: String,    /// Which of the letters, by place, the model doubted: the readout draws    /// them lighter.    pub doubted: Vec<bool>,}impl Letters {    pub fn is_empty(&self) -> bool {        self.letters.is_empty()    }    /// `letter` written after these: always added to them. A `doubtful`    /// letter narrows as any other.    pub fn adding(&self, letter: &str, doubtful: bool) -> Letters {        let mut doubted = self.doubted.clone();        doubted.extend(std::iter::repeat_n(doubtful, letter.chars().count()));        Letters { letters: format!("{}{letter}", self.letters), doubted }    }    /// `edit` made: the last letter taken back, or every one.    pub fn applying(&self, edit: Edit) -> Letters {        match edit {            Edit::Backspace => {                let mut letters = self.letters.clone();                letters.pop();                let mut doubted = self.doubted.clone();                doubted.pop();                Letters { letters, doubted }            }            Edit::Clear => Letters::default(),        }    }    /// Whether the letter at `place` was doubted.    pub fn doubts(&self, place: usize) -> bool {        self.doubted.get(place).copied().unwrap_or(false)    }    /// What a straight line from `from` to `to` does to the letters: None    /// when it edits nothing (a long or upright line, or no letters to edit),    /// and the page swipe stays its own.    pub fn edit_by_line(&self, from: Point, to: Point, knobs: &Knobs) -> Option<Edit> {        let dx = to.x - from.x;        if dx.abs() <= (to.y - from.y).abs() || dx.abs() >= knobs.swipe_at_once || self.letters.is_empty() {            return None;        }        Some(if dx < 0.0 { Edit::Backspace } else { Edit::Clear })    }}#[cfg(test)]#[path = "letters_tests.rs"]mod letters_tests;