Cotton code
64 lines · 2.7 KB · RustRaw
//! Where the household a file belongs to answers — its name and its relay,//! read off the file's own workspace row.//!//! One reader, because every crate that prints, parses or clones a//! household's address has the file open and none of them may assume the//! relay: the Mac's `repos` worker and its session host, the MCP tools, and//! the phone's reads all sit above this crate.use cotton_core::front_door::FrontDoor;use cotton_core::relay_address::HouseholdAddress;use cotton_store::driver::Driver;use rusqlite::types::Value as SqlValue;/// The household's address as `cotton_workspaces` holds it: `relay_name`/// under `relay_domain` / `relay_tunnel_host` (both NULL is Cotton's relay).////// `workspace_id` names the row for a caller that has it; `None` reads the/// file's one row. No row, no name, and a stored relay that cannot be used all/// answer an address with nothing to print/// ([`HouseholdAddress::from_stored`]) — never the name under Cotton's relay.pub fn read_household_address(    driver: &Driver,    workspace_id: Option<&str>,) -> Result<HouseholdAddress, String> {    const COLUMNS: &str = "SELECT relay_name, relay_domain, relay_tunnel_host FROM cotton_workspaces";    let row = match workspace_id {        Some(id) => driver.get(            &format!("{COLUMNS} WHERE id = ?"),            &[SqlValue::Text(id.to_string())],        ),        None => driver.get(&format!("{COLUMNS} LIMIT 1"), &[]),    }    .map_err(|error| error.to_string())?;    Ok(match row {        Some(row) => HouseholdAddress::from_stored(            row.text("relay_name"),            row.text("relay_domain"),            row.text("relay_tunnel_host"),        ),        None => HouseholdAddress::none(),    })}/// The household's front door as `cotton_workspaces` holds it: `front_door`/// and `front_door_address` (both NULL is the relay).////// No row is the relay — a file with no household has no door to have changed./// A door this build does not know, or an address that is not one, is the/// error [`FrontDoor::from_stored`] gives, never the relay.pub fn read_front_door(driver: &Driver, workspace_id: Option<&str>) -> Result<FrontDoor, String> {    const COLUMNS: &str = "SELECT front_door, front_door_address FROM cotton_workspaces";    let row = match workspace_id {        Some(id) => driver.get(            &format!("{COLUMNS} WHERE id = ?"),            &[SqlValue::Text(id.to_string())],        ),        None => driver.get(&format!("{COLUMNS} LIMIT 1"), &[]),    }    .map_err(|error| error.to_string())?;    match row {        Some(row) => FrontDoor::from_stored(row.text("front_door"), row.text("front_door_address")),        None => Ok(FrontDoor::relay()),    }}