//! Ownership is an admission chain. The serving writer verifies the owner's//! person-key signature before appending a fact; this reader checks the//! signer at the fact's stamp, as membership does for other admissions.usecrate::{driver::Driver, error::Result};use rusqlite::types::Value;#[derive(Clone)]pub(super) structOwner {pub id: String,pub person: String,pub stamp: String,pub signer: String,}pub(super) structHistory {pub owners: Vec<Owner>,}implHistory {pubfn owner(&self) -> Option<&Owner> {self.owners.last() }pubfn last_owned(&self, person: &str) -> Option<&Owner> {self.owners .iter() .rev() .find(|owner| owner.person == person) }}/// A historical read is exclusive of `before`, just like membership::at./// Ignore unauthorized admissions; a malformed newest row/// cannot displace the last valid owner or rewrite the birth admission.pub(super) fn read(db: &Driver, before: Option<&str>) -> Result<History> {let bound = Value::Text(before.unwrap_or("").into());let rows = db.all("SELECT id,workspace_id,person_key,admitted_by_person,invite_id,p_sig,admitted_at,_hlc \ FROM cotton_admissions WHERE rights='owner' AND (?='' OR _hlc<?) ORDER BY _hlc,id", &[bound.clone(), bound], )?;letmut history = History { owners: Vec::new() };for row in rows {let person = row.text("person_key").unwrap_or_default();let signer = row.text("admitted_by_person").unwrap_or_default();// Owner authority passes only by the current owner's own signed// admission; no invitation names an owner.if row.text("invite_id").is_some() {continue; }let next = Owner { id: row.text("id").unwrap_or_default().into(), person: person.into(), stamp: row.text("_hlc").unwrap_or_default().into(), signer: signer.into(), };letSome(current) = history.owner() else {// The birth admission is self-signed by its person, not a hand-over.if signer == person { history.owners.push(next); }continue; };if person == signer || person.is_empty() {continue; }if signer == current.person { history.owners.push(next); } elseif signer == current.signer && current.person != current.signer {// Both facts passed the serving writer while this signer was// still owner. A later fact from that same in-flight batch// supersedes the earlier choice, without rewriting either row. history.owners.pop(); history.owners.push(next); } }Ok(history)}