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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use std::fmt::{Display, Write};

use serde::{Deserialize, Serialize};

use crate::document::{BorrowedDocument, CollectionDocument, DocumentId, OwnedDocument, Revision};
use crate::key::Key;
use crate::schema::view::map::Mappings;
use crate::schema::{Map, SerializedCollection};

/// The header of a `Document`.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub struct Header {
    /// The id of the Document. Unique across the collection the document is
    /// contained within.
    pub id: DocumentId,

    /// The revision of the stored document.
    pub revision: Revision,
}

/// A type that can return a [`Header`].
pub trait HasHeader {
    /// Returns the header for this instance.
    fn header(&self) -> Result<Header, crate::Error>;
}

impl HasHeader for Header {
    fn header(&self) -> Result<Header, crate::Error> {
        Ok(self.clone())
    }
}

/// View mapping emit functions. Used when implementing a view's `map()`
/// function.
pub trait Emit {
    /// Creates a `Map` result with an empty key and value.
    fn emit(&self) -> Result<Mappings<(), ()>, crate::Error> {
        self.emit_key_and_value((), ())
    }

    /// Creates a `Map` result with an empty key and value if `condition` is
    /// true.
    fn emit_if(&self, condition: bool) -> Result<Mappings<(), ()>, crate::Error> {
        if condition {
            self.emit()
        } else {
            Ok(Mappings::default())
        }
    }

    /// Creates a `Map` result with a `key` and an empty value.
    fn emit_key<K>(&self, key: K) -> Result<Mappings<K, ()>, crate::Error> {
        self.emit_key_and_value(key, ())
    }

    /// Creates a `Map` result with `value` and an empty key.
    fn emit_value<Value>(&self, value: Value) -> Result<Mappings<(), Value>, crate::Error> {
        self.emit_key_and_value((), value)
    }

    /// Creates a `Map` result with a `key` and `value`.
    fn emit_key_and_value<K, Value>(
        &self,
        key: K,
        value: Value,
    ) -> Result<Mappings<K, Value>, crate::Error>;
}

impl Emit for Header {
    fn emit_key_and_value<K, Value>(
        &self,
        key: K,
        value: Value,
    ) -> Result<Mappings<K, Value>, crate::Error> {
        Ok(Mappings::Simple(Some(Map::new(self.clone(), key, value))))
    }
}

impl Display for Header {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.id.fmt(f)?;
        f.write_char('@')?;
        self.revision.fmt(f)
    }
}

/// A header for a [`CollectionDocument`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct CollectionHeader<PrimaryKey> {
    /// The unique id of the document.
    pub id: PrimaryKey,
    /// The revision of the document.
    pub revision: Revision,
}

impl<PrimaryKey> Emit for CollectionHeader<PrimaryKey>
where
    PrimaryKey: for<'k> Key<'k>,
{
    fn emit_key_and_value<K, Value>(
        &self,
        key: K,
        value: Value,
    ) -> Result<Mappings<K, Value>, crate::Error> {
        let header = Header::try_from(self.clone())?;
        Ok(Mappings::Simple(Some(Map::new(header, key, value))))
    }
}

impl<PrimaryKey> HasHeader for CollectionHeader<PrimaryKey>
where
    PrimaryKey: for<'k> Key<'k>,
{
    fn header(&self) -> Result<Header, crate::Error> {
        Header::try_from(self.clone())
    }
}

impl HasHeader for OwnedDocument {
    fn header(&self) -> Result<Header, crate::Error> {
        self.header.header()
    }
}

impl<'a> HasHeader for BorrowedDocument<'a> {
    fn header(&self) -> Result<Header, crate::Error> {
        self.header.header()
    }
}

impl<C> HasHeader for CollectionDocument<C>
where
    C: SerializedCollection,
{
    fn header(&self) -> Result<Header, crate::Error> {
        self.header.header()
    }
}

impl<PrimaryKey> TryFrom<Header> for CollectionHeader<PrimaryKey>
where
    PrimaryKey: for<'k> Key<'k>,
{
    type Error = crate::Error;

    fn try_from(value: Header) -> Result<Self, Self::Error> {
        Ok(Self {
            id: value.id.deserialize::<PrimaryKey>()?,
            revision: value.revision,
        })
    }
}

impl<'a, PrimaryKey> TryFrom<&'a Header> for CollectionHeader<PrimaryKey>
where
    PrimaryKey: for<'k> Key<'k>,
{
    type Error = crate::Error;

    fn try_from(value: &'a Header) -> Result<Self, Self::Error> {
        Ok(Self {
            id: value.id.deserialize::<PrimaryKey>()?,
            revision: value.revision,
        })
    }
}

impl<PrimaryKey> TryFrom<CollectionHeader<PrimaryKey>> for Header
where
    PrimaryKey: for<'k> Key<'k>,
{
    type Error = crate::Error;

    fn try_from(value: CollectionHeader<PrimaryKey>) -> Result<Self, Self::Error> {
        Ok(Self {
            id: DocumentId::new(&value.id)?,
            revision: value.revision,
        })
    }
}

impl<'a, PrimaryKey> TryFrom<&'a CollectionHeader<PrimaryKey>> for Header
where
    PrimaryKey: for<'k> Key<'k>,
{
    type Error = crate::Error;

    fn try_from(value: &'a CollectionHeader<PrimaryKey>) -> Result<Self, Self::Error> {
        Ok(Self {
            id: DocumentId::new(&value.id)?,
            revision: value.revision,
        })
    }
}

/// A header with either a serialized or deserialized primary key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AnyHeader<PrimaryKey> {
    /// A serialized header.
    Serialized(Header),
    /// A deserialized header.
    Collection(CollectionHeader<PrimaryKey>),
}

impl<PrimaryKey> AnyHeader<PrimaryKey>
where
    PrimaryKey: for<'k> Key<'k>,
{
    /// Returns the contained header as a [`Header`].
    pub fn into_header(self) -> Result<Header, crate::Error> {
        match self {
            AnyHeader::Serialized(header) => Ok(header),
            AnyHeader::Collection(header) => Header::try_from(header),
        }
    }
}

#[test]
fn emissions_tests() -> Result<(), crate::Error> {
    use crate::schema::Map;
    use crate::test_util::Basic;

    let doc = BorrowedDocument::with_contents::<Basic, _>(&1, &Basic::default())?;

    assert_eq!(
        doc.header.emit()?,
        Mappings::Simple(Some(Map::new(doc.header.clone(), (), ())))
    );

    assert_eq!(
        doc.header.emit_key(1)?,
        Mappings::Simple(Some(Map::new(doc.header.clone(), 1, ())))
    );

    assert_eq!(
        doc.header.emit_value(1)?,
        Mappings::Simple(Some(Map::new(doc.header.clone(), (), 1)))
    );

    assert_eq!(
        doc.header.emit_key_and_value(1, 2)?,
        Mappings::Simple(Some(Map::new(doc.header, 1, 2)))
    );

    Ok(())
}

#[test]
fn chained_mappings_test() -> Result<(), crate::Error> {
    use crate::schema::Map;
    use crate::test_util::Basic;

    let doc = BorrowedDocument::with_contents::<Basic, _>(&1, &Basic::default())?;

    assert_eq!(
        doc.header.emit()?.and(doc.header.emit()?),
        Mappings::List(vec![
            Map::new(doc.header.clone(), (), ()),
            Map::new(doc.header, (), ())
        ])
    );

    Ok(())
}

#[test]
fn header_display_test() {
    let original_contents = b"one";
    let revision = Revision::new(original_contents);
    let header = Header {
        id: DocumentId::new(&42_u64).unwrap(),
        revision,
    };
    assert_eq!(
        header.to_string(),
        "7$2a@0-7692c3ad3540bb803c020b3aee66cd8887123234ea0c6e7143c0add73ff431ed"
    );
}