aboutsummaryrefslogtreecommitdiffstats
path: root/src/document/view.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/document/view.rs')
-rw-r--r--src/document/view.rs118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/document/view.rs b/src/document/view.rs
new file mode 100644
index 0000000..018e8c0
--- /dev/null
+++ b/src/document/view.rs
@@ -0,0 +1,118 @@
+//! A document to read and to write, whatever kind it is of: what the node asks of one
+//! where it asks no more than values at paths (`docs/document.md`, The seam). The
+//! member registry, the users, access control's reads and the rules read through `Read`,
+//! and what writes protocol data writes through `Write`; what an Automerge document can
+//! do beyond that (`store`) is asked of it as such.
+//!
+//! Automerge's documents, its writes in progress and a document as it was (`at::At`) are
+//! all read so, and its writes in progress written so.
+
+use serde_json::Value as JsonValue;
+
+use super::store::{self, StoreError};
+use super::{ReadDoc, Transactable};
+
+/// A document to read: as it is, as a write in progress has it, or as it was.
+pub(crate) trait Read {
+ /// The value at a path; the empty path is everything.
+ fn read_path(&self, path: &[String]) -> Result<Option<JsonValue>, StoreError>;
+
+ /// `read_path` of a value its user may have signed (`access::envelope`): an envelope
+ /// read as the value it holds, unchecked (the policy checked it before it went in),
+ /// a signed `null` as nothing.
+ fn read_plain(&self, path: &[String]) -> Result<Option<JsonValue>, StoreError>;
+
+ /// `read_plain`, telling apart what plain JSON shows alike, where the document's
+ /// kind stores them apart: a text, tagged, from a string.
+ fn read_typed(&self, path: &[String]) -> Result<Option<JsonValue>, StoreError>;
+
+ /// The keys of the map at `path`; None if there is no map there (nothing, or another
+ /// value).
+ fn keys_at(&self, path: &[String]) -> Result<Option<Vec<String>>, StoreError>;
+}
+
+/// A document being written: what is written is read back at once (`Read`), and goes in
+/// or not as a whole.
+pub(crate) trait Write: Read {
+ /// Writes the value at `path`, making the maps on the way: afterwards it reads as
+ /// `value`.
+ fn write_path(&mut self, path: &[String], value: &JsonValue) -> Result<(), StoreError>;
+
+ /// Deletes the value at `path`. With `prune`, the maps above it that it leaves empty
+ /// go too.
+ fn delete_path(&mut self, path: &[String], prune: bool) -> Result<(), StoreError>;
+}
+
+impl<T: ReadDoc> Read for T {
+ fn read_path(&self, path: &[String]) -> Result<Option<JsonValue>, StoreError> {
+ store::read_path(self, path)
+ }
+
+ fn read_plain(&self, path: &[String]) -> Result<Option<JsonValue>, StoreError> {
+ store::read_plain(self, path)
+ }
+
+ fn read_typed(&self, path: &[String]) -> Result<Option<JsonValue>, StoreError> {
+ let typed = super::typed::JsonForm {
+ typed: true,
+ ..Default::default()
+ };
+ store::read_plain_as(self, path, typed)
+ }
+
+ fn keys_at(&self, path: &[String]) -> Result<Option<Vec<String>>, StoreError> {
+ store::keys_at(self, path)
+ }
+}
+
+impl<T: Transactable> Write for T {
+ fn write_path(&mut self, path: &[String], value: &JsonValue) -> Result<(), StoreError> {
+ store::write_path(self, path, value)
+ }
+
+ fn delete_path(&mut self, path: &[String], prune: bool) -> Result<(), StoreError> {
+ store::delete_path(self, path, prune)
+ }
+}
+
+/// [`Read::read_path`].
+pub(crate) fn read_path(
+ doc: &(impl Read + ?Sized),
+ path: &[String],
+) -> Result<Option<JsonValue>, StoreError> {
+ doc.read_path(path)
+}
+
+/// [`Read::read_plain`].
+pub(crate) fn read_plain(
+ doc: &(impl Read + ?Sized),
+ path: &[String],
+) -> Result<Option<JsonValue>, StoreError> {
+ doc.read_plain(path)
+}
+
+/// [`Read::keys_at`].
+pub(crate) fn keys_at(
+ doc: &(impl Read + ?Sized),
+ path: &[String],
+) -> Result<Option<Vec<String>>, StoreError> {
+ doc.keys_at(path)
+}
+
+/// [`Write::write_path`].
+pub(crate) fn write_path(
+ doc: &mut (impl Write + ?Sized),
+ path: &[String],
+ value: &JsonValue,
+) -> Result<(), StoreError> {
+ doc.write_path(path, value)
+}
+
+/// [`Write::delete_path`].
+pub(crate) fn delete_path(
+ doc: &mut (impl Write + ?Sized),
+ path: &[String],
+ prune: bool,
+) -> Result<(), StoreError> {
+ doc.delete_path(path, prune)
+}