ledb_types/
document.rs

1use super::{Identifier, KeyFields};
2use std::{
3    borrow::Cow,
4    hash::BuildHasher,
5    collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque},
6    rc::{Rc, Weak as RcWeak},
7    sync::{Arc, Mutex, RwLock, Weak as ArcWeak},
8};
9
10/// Primary key (document identifier)
11pub type Primary = u32;
12
13/// Identified document representation
14pub trait Document {
15    /// Get the name of primary field
16    fn primary_field() -> Identifier {
17        "$".into()
18    }
19
20    /// Get other key fields (indexes)
21    fn key_fields() -> KeyFields {
22        KeyFields::new()
23    }
24}
25
26impl<'a, T: Document> Document for &'a T {
27    fn primary_field() -> Identifier {
28        T::primary_field()
29    }
30
31    fn key_fields() -> KeyFields {
32        T::key_fields()
33    }
34}
35
36impl<'a, T: Document> Document for &'a mut T {
37    fn primary_field() -> Identifier {
38        T::primary_field()
39    }
40
41    fn key_fields() -> KeyFields {
42        T::key_fields()
43    }
44}
45
46impl<'a, T: Document> Document for &'a [T] {
47    fn primary_field() -> Identifier {
48        T::primary_field()
49    }
50
51    fn key_fields() -> KeyFields {
52        T::key_fields()
53    }
54}
55
56impl<'a, T: Document> Document for &'a mut [T] {
57    fn primary_field() -> Identifier {
58        T::primary_field()
59    }
60
61    fn key_fields() -> KeyFields {
62        T::key_fields()
63    }
64}
65
66impl<T: Document> Document for [T] {
67    fn primary_field() -> Identifier {
68        T::primary_field()
69    }
70
71    fn key_fields() -> KeyFields {
72        T::key_fields()
73    }
74}
75
76impl<T: Document> Document for Vec<T> {
77    fn primary_field() -> Identifier {
78        T::primary_field()
79    }
80
81    fn key_fields() -> KeyFields {
82        T::key_fields()
83    }
84}
85
86impl<T: Document> Document for VecDeque<T> {
87    fn primary_field() -> Identifier {
88        T::primary_field()
89    }
90
91    fn key_fields() -> KeyFields {
92        T::key_fields()
93    }
94}
95
96impl<T: Document, S: BuildHasher> Document for HashSet<T, S> {
97    fn primary_field() -> Identifier {
98        T::primary_field()
99    }
100
101    fn key_fields() -> KeyFields {
102        T::key_fields()
103    }
104}
105
106impl<K, T: Document, S: BuildHasher> Document for HashMap<K, T, S> {
107    fn primary_field() -> Identifier {
108        T::primary_field()
109    }
110
111    fn key_fields() -> KeyFields {
112        T::key_fields().with_parent("*")
113    }
114}
115
116impl<T: Document> Document for BTreeSet<T> {
117    fn primary_field() -> Identifier {
118        T::primary_field()
119    }
120
121    fn key_fields() -> KeyFields {
122        T::key_fields()
123    }
124}
125
126impl<K, T: Document> Document for BTreeMap<K, T> {
127    fn primary_field() -> Identifier {
128        T::primary_field()
129    }
130
131    fn key_fields() -> KeyFields {
132        T::key_fields().with_parent("*")
133    }
134}
135
136impl<'a, T: Document> Document for Box<T> {
137    fn primary_field() -> Identifier {
138        T::primary_field()
139    }
140
141    fn key_fields() -> KeyFields {
142        T::key_fields()
143    }
144}
145
146impl<'a, T: Document> Document for Rc<T> {
147    fn primary_field() -> Identifier {
148        T::primary_field()
149    }
150
151    fn key_fields() -> KeyFields {
152        T::key_fields()
153    }
154}
155
156impl<'a, T: Document> Document for RcWeak<T> {
157    fn primary_field() -> Identifier {
158        T::primary_field()
159    }
160
161    fn key_fields() -> KeyFields {
162        T::key_fields()
163    }
164}
165
166impl<'a, T: Document> Document for Arc<T> {
167    fn primary_field() -> Identifier {
168        T::primary_field()
169    }
170
171    fn key_fields() -> KeyFields {
172        T::key_fields()
173    }
174}
175
176impl<'a, T: Document> Document for ArcWeak<T> {
177    fn primary_field() -> Identifier {
178        T::primary_field()
179    }
180
181    fn key_fields() -> KeyFields {
182        T::key_fields()
183    }
184}
185
186impl<'a, T: Document> Document for Mutex<T> {
187    fn primary_field() -> Identifier {
188        T::primary_field()
189    }
190
191    fn key_fields() -> KeyFields {
192        T::key_fields()
193    }
194}
195
196impl<'a, T: Document> Document for RwLock<T> {
197    fn primary_field() -> Identifier {
198        T::primary_field()
199    }
200
201    fn key_fields() -> KeyFields {
202        T::key_fields()
203    }
204}
205
206impl<'a, T: Document + Clone> Document for Cow<'a, T> {
207    fn primary_field() -> Identifier {
208        T::primary_field()
209    }
210
211    fn key_fields() -> KeyFields {
212        T::key_fields()
213    }
214}
215
216impl<T: Document> Document for Option<T> {
217    fn primary_field() -> Identifier {
218        T::primary_field()
219    }
220
221    fn key_fields() -> KeyFields {
222        T::key_fields()
223    }
224}
225
226#[cfg(feature = "json")]
227impl Document for serde_json::Value {}
228
229#[cfg(feature = "cbor")]
230impl Document for serde_cbor::Value {}