aboutsummaryrefslogtreecommitdiffstats
path: root/src/document/view.rs
blob: 018e8c0a15d5d4c3a7412e64f638a818e1df7641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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)
}