Cotton code
90 lines · 3.1 KB · RustRaw
//! A token's REACH — the databases one call may touch (design 3.8, B1).//!//! A personal token reaches every database on this server its owner owns,//! Mine first; a seat or place token reaches its one file. A [`Ctx`] stays//! bound to one file; the wrapper that fans a call out//! (`cotton_server::mcp::places`) builds one `Ctx` per file it touches, each//! [`Ctx::within`] the same `Reach`.//!//! What an assistant reads says DATABASE: the wrapper's argument is//! `database`, a merged row carries `database` and `databaseId`. The Rust//! names here kept the word they landed with (place, `ReachFile`).//!//! [`Ctx`]: crate::mcp::dispatch::Ctx//! [`Ctx::within`]: crate::mcp::dispatch::Ctx::within/// One database a token reaches.#[derive(Debug, Clone, PartialEq, Eq)]pub struct ReachFile {    /// The file's id — its workspace id, what a caller passes as `database`    /// and what each merged row carries as `databaseId`.    pub id: String,    /// What the owner calls it ("Mine", "the product team's place").    pub name: String,    /// Is this the owner's personal database?    pub is_mine: bool,}/// Why a reach is what it is.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum ReachBasis {    /// The registry names the token's file as its owner's personal row: every    /// database that owner owns.    Personal,    /// A seat or place token, a worker, an assistant run or the phone: its    /// one file, never Mine unless that file is Mine.    OneFile,}/// The files one call may touch, Mine first.#[derive(Debug, Clone, PartialEq, Eq)]pub struct Reach {    pub files: Vec<ReachFile>,    pub basis: ReachBasis,}/// A `database` outside reach, or one that does not exist.////// ONE answer for both, so a seat token cannot probe for databases it does/// not reach by telling the two apart.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub struct NoSuchPlace;impl NoSuchPlace {    pub const SENTENCE: &'static str = "no such database";}impl std::fmt::Display for NoSuchPlace {    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {        f.write_str(Self::SENTENCE)    }}impl std::error::Error for NoSuchPlace {}impl Reach {    /// One file and nothing else.    pub fn only(file: ReachFile) -> Reach {        Reach {            files: vec![file],            basis: ReachBasis::OneFile,        }    }    /// The file a `database` argument names, by its id.    pub fn place(&self, place: &str) -> Result<&ReachFile, NoSuchPlace> {        if let Some(file) = self.files.iter().find(|file| file.id == place) { return Ok(file); }        if place.eq_ignore_ascii_case("personal") { return self.mine().ok_or(NoSuchPlace); }        let mut matches = self.files.iter().filter(|file| file.name == place);        match (matches.next(), matches.next()) { (Some(file), None) => Ok(file), _ => Err(NoSuchPlace) }    }    /// The owner's personal database, when this reach holds it.    pub fn mine(&self) -> Option<&ReachFile> {        self.files.iter().find(|file| file.is_mine)    }}#[cfg(test)]#[path = "places_tests.rs"]mod tests;