Cotton code
80 lines · 3.0 KB · RustRaw
//! A table's visibility as a definition spells it. One reading for the phone//! and the Worker: absent is `owner`; `public`, `staff`, `customer`, `owner`//! or `mine(<column>)`; any other word is refused.//!//! Vectors: `vectors/visibility.json`./// Who reads a table's rows.#[derive(Debug, Clone, PartialEq, Eq)]pub enum Visibility {    Public,    Mine(String),    Staff,    /// A member reads only the rows whose `person` column names them; staff    /// and the owner read them all.    Customer,    /// The personal database's own rows: its one person's, read by nobody    /// else (the personal package's tables). A phone holds them as its    /// person's own.    Owner,}/// The refusal a word that is no visibility gets: a `refusal.definition.*`/// line key, which the host says in words.pub const REFUSED: &str = "visibility_is_public_mine_staff_customer_or_owner";impl Visibility {    /// The visibility a word names; `Err` holds the refusal's line key.    pub fn parse(value: Option<&str>) -> Result<Self, &'static str> {        match value {            None | Some("owner") => Ok(Self::Owner),            Some("staff") => Ok(Self::Staff),            Some("public") => Ok(Self::Public),            Some("customer") => Ok(Self::Customer),            Some(value) => {                let column = value                    .strip_prefix("mine(")                    .and_then(|v| v.strip_suffix(')'))                    .filter(|v| {                        !v.is_empty()                            && v.bytes().enumerate().all(|(i, b)| b.is_ascii_lowercase() || b == b'_' || (i > 0 && b.is_ascii_digit()))                    })                    .ok_or(REFUSED)?;                Ok(Self::Mine(column.into()))            }        }    }    /// As the Worker reads it: `{"kind": …}`, with `through` for mine.    pub fn to_json(&self) -> serde_json::Value {        use serde_json::json;        match self {            Self::Public => json!({ "kind": "public" }),            Self::Staff => json!({ "kind": "staff" }),            Self::Customer => json!({ "kind": "customer" }),            Self::Owner => json!({ "kind": "owner" }),            Self::Mine(through) => json!({ "kind": "mine", "through": through }),        }    }}#[cfg(test)]mod tests {    use super::Visibility;    use serde_json::{json, Value};    /// RULE: both sides read a visibility word the same way (the Worker's    /// visibility.test.ts reads the same vectors).    #[test]    fn visibility_words_read_as_the_shared_vectors_say() {        let file: Value = serde_json::from_str(include_str!("../vectors/visibility.json")).unwrap();        for case in file["cases"].as_array().unwrap() {            let got = Visibility::parse(case["word"].as_str()).map(|v| v.to_json());            if case["refused"] == json!(true) {                assert!(got.is_err(), "{case}");            } else {                assert_eq!(got.as_ref().ok(), Some(&case["want"]), "{case}");            }        }    }}