Cotton code
84 lines · 3.2 KB · RustRaw
//! A scene heading and its parts: `INT. KITCHEN - NIGHT` is setting `INT.`,//! place `KITCHEN`, time `NIGHT`. The core keeps a scene's heading and its//! setting, place and time in step with these.use serde::{Deserialize, Serialize};/// The settings a heading opens with, the longest spelling first, each with/// the one the story keeps (`INT.`, `EXT.`, `INT./EXT.`).const SETTINGS: [(&str, &str); 9] = [    ("INT./EXT", "INT./EXT."),    ("EXT./INT", "INT./EXT."),    ("INT/EXT", "INT./EXT."),    ("EXT/INT", "INT./EXT."),    ("I/E", "INT./EXT."),    ("INT", "INT."),    ("EXT", "EXT."),    ("EST", "EXT."),    ("E/I", "INT./EXT."),];/// The setting `trimmed` opens with and the bytes it takes (its dot too):/// the prefix in any case, then a dot or a space.fn setting_prefix(trimmed: &str) -> Option<(&'static str, usize)> {    SETTINGS.iter().find_map(|(prefix, setting)| {        let head = trimmed.as_bytes().get(..prefix.len())?;        if !head.eq_ignore_ascii_case(prefix.as_bytes()) {            return None;        }        let rest = &trimmed[prefix.len()..];        match rest.chars().next() {            Some('.') => Some((*setting, prefix.len() + 1)),            Some(c) if c.is_whitespace() => Some((*setting, prefix.len())),            _ => None,        }    })}/// Does this line read as a scene heading by its setting?pub fn is_heading(trimmed: &str) -> bool {    setting_prefix(trimmed).is_some()}#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]pub struct Parts {    /// `INT.`, `EXT.` or `INT./EXT.`; none for a heading with no setting.    pub setting: Option<String>,    pub place: String,    pub time: Option<String>,}/// A heading read into its parts. The time is what follows the last ` - `.pub fn parse(heading: &str) -> Parts {    let mut text = heading.trim();    if let Some(rest) = text.strip_prefix('.').filter(|rest| !rest.starts_with('.')) {        text = rest.trim_start();    }    let (setting, rest) = match setting_prefix(text) {        Some((setting, taken)) => (Some(setting.to_string()), text[taken..].trim()),        None => (None, text),    };    let split = [" - ", " – ", " — "].iter().filter_map(|dash| rest.rfind(dash).map(|at| (at, dash.len()))).max_by_key(|(at, _)| *at);    let (place, time) = match split {        Some((at, len)) => (rest[..at].trim(), Some(rest[at + len..].trim()).filter(|time| !time.is_empty())),        None => (rest.trim(), None),    };    Parts { setting, place: place.to_string(), time: time.map(str::to_string) }}/// A heading written from its parts: `INT. KITCHEN - NIGHT`, the place and/// time in capitals where their script has them. No setting is `INT.`.pub fn compose(parts: &Parts) -> String {    let setting = parts.setting.as_deref().filter(|setting| !setting.is_empty()).unwrap_or("INT.");    let mut out = setting.to_string();    let place = parts.place.trim().to_uppercase();    if !place.is_empty() {        out.push(' ');        out.push_str(&place);    }    if let Some(time) = parts.time.as_deref().map(str::trim).filter(|time| !time.is_empty()) {        out.push_str(" - ");        out.push_str(&time.to_uppercase());    }    out}