From 9188fa6e13ea7facfab5fb6f55bf26444ec2c01e Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 29 Nov 2026 03:15:05 +0000 Subject: document: values read and written through a view any kind of document gives document::view: Read (the value at a path, plain, typed, a map's keys) and Write (a value written, one deleted), which Automerge's documents, writes in progress and documents as they were all are. The member registry, the users, access control's reads and rules, the topology, forgetting and bootstrap read and write through them, and name neither Automerge's ReadDoc and Transactable nor document::store, which is what an Automerge document can do beyond. How a swarm holding a store judges what a change writes is handed to the rules (rules::judge_seen), being its kind of document's to say. --- src/access/sealed.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'src/access/sealed.rs') diff --git a/src/access/sealed.rs b/src/access/sealed.rs index 2b8085a..c30bea7 100644 --- a/src/access/sealed.rs +++ b/src/access/sealed.rs @@ -25,7 +25,8 @@ use serde_json::Value as JsonValue; use sha2::{Digest, Sha256}; use super::{agree, envelope, registry, sshsig}; -use crate::document::{ReadDoc, meta, store}; +use crate::document::meta; +use crate::document::view::{self, Read}; /// What every sealed value starts with. const PREFIX: &str = "cs1."; @@ -47,8 +48,8 @@ pub(crate) struct Declared { } /// What `doc` declares sealed; an entry that doesn't read as one is left out. -pub(crate) fn declared(doc: &impl ReadDoc) -> Vec { - let Ok(Some(JsonValue::Array(entries))) = store::read_plain(doc, &declared_at()) else { +pub(crate) fn declared(doc: &impl Read) -> Vec { + let Ok(Some(JsonValue::Array(entries))) = view::read_plain(doc, &declared_at()) else { return Vec::new(); }; entries @@ -140,11 +141,7 @@ pub(crate) fn recipients(sealed: &str) -> Result, String> { /// (as its writer saw it): a sealed value, or, signed by its user (`access::envelope`), /// one holding a sealed value or `null` (deleted); with readers declared, sealed for /// exactly their keys. -pub(crate) fn check( - doc: &impl ReadDoc, - declared: &Declared, - value: &JsonValue, -) -> Result<(), String> { +pub(crate) fn check(doc: &impl Read, declared: &Declared, value: &JsonValue) -> Result<(), String> { let JsonValue::String(text) = value else { return Err("a sealed value is a string (cs1.)".to_owned()); }; @@ -187,7 +184,7 @@ pub(crate) fn check( /// Checks what is at `path` in `doc` after a write there: nothing within a declared /// place but its value, and every value at one (at `path` or below it) as `check` says. /// Why not, if not. -pub(crate) fn kept(doc: &impl ReadDoc, path: &[String]) -> Result<(), String> { +pub(crate) fn kept(doc: &impl Read, path: &[String]) -> Result<(), String> { let declared = declared(doc); if declared.is_empty() || !reaches(&declared, path) { return Ok(()); @@ -196,7 +193,7 @@ pub(crate) fn kept(doc: &impl ReadDoc, path: &[String]) -> Result<(), String> { return Err(format!("/{} is within a sealed value", path.join("/"))); } fn walk( - doc: &impl ReadDoc, + doc: &impl Read, declared: &[Declared], value: &JsonValue, at: &mut Vec, @@ -215,7 +212,7 @@ pub(crate) fn kept(doc: &impl ReadDoc, path: &[String]) -> Result<(), String> { } Ok(()) } - match store::read_path(doc, path) { + match view::read_path(doc, path) { Ok(Some(value)) => walk(doc, &declared, &value, &mut path.to_vec()), Ok(None) => Ok(()), Err(error) => Err(error.to_string()), @@ -270,11 +267,11 @@ mod tests { change(&mut doc, |doc| { let mut alice = registry::entry("root", 1, &[]); alice["keys"] = json!({ "laptop": key(1).line(), "phone": key(2).line() }); - store::write_path(doc, ®istry::user_path("alice"), &alice).unwrap(); + view::write_path(doc, ®istry::user_path("alice"), &alice).unwrap(); let mut bob = registry::entry("root", 1, &[]); bob["keys"] = json!({ "laptop": key(3).line() }); - store::write_path(doc, ®istry::user_path("bob"), &bob).unwrap(); - store::write_path( + view::write_path(doc, ®istry::user_path("bob"), &bob).unwrap(); + view::write_path( doc, &declared_at(), &json!([ @@ -360,13 +357,13 @@ mod tests { let mut doc = swarm(); let all = sealed_for(&[key(1), key(2), key(3)]); change(&mut doc, |doc| { - store::write_path(doc, &path("secrets"), &json!({"db": all, "other": all})).unwrap(); - store::write_path(doc, &path("elsewhere"), &json!("plain")).unwrap(); + view::write_path(doc, &path("secrets"), &json!({"db": all, "other": all})).unwrap(); + view::write_path(doc, &path("elsewhere"), &json!("plain")).unwrap(); }); assert!(kept(&doc, &path("secrets")).is_ok()); assert!(kept(&doc, &path("elsewhere")).is_ok()); change(&mut doc, |doc| { - store::write_path(doc, &path("secrets/web"), &json!("plain")).unwrap(); + view::write_path(doc, &path("secrets/web"), &json!("plain")).unwrap(); }); assert!(kept(&doc, &path("secrets")).is_err()); assert!(kept(&doc, &path("secrets/web")).is_err()); -- cgit v1.3.1