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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::borrow::Cow;
use serde::{Deserialize, Serialize};
use crate::schema::{collection, map, Map};
mod revision;
pub use revision::Revision;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Header {
pub id: u64,
pub revision: Revision,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Document<'a> {
pub collection: collection::Id,
pub header: Cow<'a, Header>,
pub contents: Cow<'a, [u8]>,
}
impl<'a> Document<'a> {
#[must_use]
pub fn new(id: u64, contents: Cow<'a, [u8]>, collection: collection::Id) -> Self {
let revision = Revision::new(&contents);
Self {
header: Cow::Owned(Header { id, revision }),
contents,
collection,
}
}
pub fn with_contents<S: Serialize>(
id: u64,
contents: &S,
collection: collection::Id,
) -> Result<Self, serde_cbor::Error> {
let contents = Cow::from(serde_cbor::to_vec(contents)?);
Ok(Self::new(id, contents, collection))
}
pub fn contents<D: Deserialize<'a>>(&'a self) -> Result<D, serde_cbor::Error> {
serde_cbor::from_slice(&self.contents)
}
pub fn set_contents<S: Serialize>(&mut self, contents: &S) -> Result<(), serde_cbor::Error> {
self.contents = Cow::from(serde_cbor::to_vec(contents)?);
Ok(())
}
#[must_use]
pub fn create_new_revision(&self, contents: Cow<'a, [u8]>) -> Option<Self> {
self.header
.revision
.next_revision(&contents)
.map(|revision| Self {
header: Cow::Owned(Header {
id: self.header.id,
revision,
}),
contents,
collection: self.collection.clone(),
})
}
#[must_use]
pub fn emit(&self) -> Map<(), ()> {
self.emit_key_and_value((), ())
}
#[must_use]
pub fn emit_key<Key: map::Key>(&self, key: Key) -> Map<Key, ()> {
self.emit_key_and_value(key, ())
}
#[must_use]
pub fn emit_value<Value: Serialize>(&self, value: Value) -> Map<(), Value> {
self.emit_key_and_value((), value)
}
#[must_use]
pub fn emit_key_and_value<Key: map::Key, Value: Serialize>(
&self,
key: Key,
value: Value,
) -> Map<Key, Value> {
Map::new(self.header.id, key, value)
}
#[must_use]
pub fn to_owned(&self) -> Document<'static> {
Document::<'static> {
collection: self.collection.clone(),
header: Cow::Owned(self.header.as_ref().clone()),
contents: Cow::Owned(self.contents.as_ref().to_vec()),
}
}
}
#[test]
fn emissions_tests() -> Result<(), crate::Error> {
use crate::{
schema::{Collection, Map},
test_util::Basic,
};
let doc = Document::with_contents(1, &Basic::default(), Basic::collection_id())?;
assert_eq!(doc.emit(), Map::new(doc.header.id, (), ()));
assert_eq!(doc.emit_key(1), Map::new(doc.header.id, 1, ()));
assert_eq!(doc.emit_value(1), Map::new(doc.header.id, (), 1));
assert_eq!(doc.emit_key_and_value(1, 2), Map::new(doc.header.id, 1, 2));
Ok(())
}