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
/*!
# Derive macro for defining storable documents
This crate helps to turn rust structures into documents which can be stored, indexed and queried.
## Defining documents
You may turn any struct into a document using `Document` in derive annotation like this:
```rust
use serde::{Serialize, Deserialize};
use ledb::{Document};
#[derive(Serialize, Deserialize, Document)]
struct MyDoc {
// primary field
#[document(primary)]
id: Option<u32>,
// other fields
}
```
This generates `Document` trait implementation for struct `MyDoc`.
It requires single field marked as primary key per document.
Currently primary key should be an integer only.
Also it not needed to be an optional field, but in this case you should take care of parsing (for example add `serde(default)` annotation).
## Defining key fields for indexing
To turn document field into key you can add document index annotation to it:
```rust
# extern crate serde;
# extern crate ledb;
#
use serde::{Serialize, Deserialize};
use ledb::{Document};
#[derive(Serialize, Deserialize, Document)]
struct MyDoc {
// primary field
#[serde(default)]
#[document(primary)]
id: u32,
// unique string key
#[document(unique)]
title: String,
// normal string index
#[document(index)]
keywords: Vec<String>,
// unique int key
#[document(unique)]
timestamp: u64,
}
```
## Overriding key types
In some cases it may be ambiguous to determine actual type of key by field type.
For example, when you try to index binary data using `Vec<u8>`, the actually determined key type is an integer (u8).
So you required to override key type manually using annotation like so:
```rust
# extern crate serde;
# extern crate serde_bytes;
# extern crate ledb;
#
use serde::{Serialize, Deserialize};
use serde_bytes;
use ledb::{Document};
#[derive(Serialize, Deserialize, Document)]
struct MyDoc {
#[document(primary)]
id: u32,
// ...
#[document(unique binary)]
#[serde(with = "serde_bytes")]
hash: Vec<u8>,
}
```
## Nested documents
Of course you can add nested documents which may also have key fields:
```rust
# extern crate serde;
# extern crate ledb;
#
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use ledb::{Document};
#[derive(Serialize, Deserialize, Document)]
struct MyDoc {
// primary field
#[document(primary)]
#[serde(default)]
id: u32,
// ...fields
// simple nested document
#[document(nested)]
meta: Meta,
// list of nested documents
#[document(nested)]
links: Vec<Link>,
// map of nested documents
#[document(nested)]
props: HashMap<String, Prop>,
}
#[derive(Serialize, Deserialize, Document)]
#[document(nested)]
struct Meta {
#[document(index)]
title: String,
#[document(index)]
author: String,
annotation: String,
}
#[derive(Serialize, Deserialize, Document)]
#[document(nested)]
struct Link {
href: String,
text: String,
}
#[derive(Serialize, Deserialize, Document)]
#[document(nested)]
struct Prop {
value: String,
required: bool,
}
```
The primary key field is omitted for nested documents.
The nested documents should be explicitly marked as nested using `#[document(nested)]` directive as shown above.
**NOTE**: When the `#[serde(flatten)]` directive is used the key fields of nested documents will be transferred to owner.
## Simple usage example
```rust
# extern crate serde;
# extern crate ledb;
#
use serde::{Serialize, Deserialize};
use ledb::{Document};
#[derive(Serialize, Deserialize, Document)]
struct MyDoc {
// define optional primary key field
#[document(primary)]
id: Option<u64>,
// define unique key field
#[document(unique)]
title: String,
// define index fields
#[document(index)]
tag: Vec<String>,
#[document(unique)]
timestamp: u32,
// define nested document
#[document(nested)]
meta: MetaData,
}
#[derive(Serialize, Deserialize, Document)]
#[document(nested)]
struct MetaData {
// define index field
#[document(index)]
keywords: Vec<String>,
// define other fields
description: String,
}
```
It will generate the `Document` traits like so:
```ignore
impl Document for MyDoc {
// declare primary key field name
fn primary_field() -> Identifier {
"id".into()
}
// declare other key fields for index
fn key_fields() -> KeyFields {
KeyFields::new()
// add key fields of document
.with_field(("title", String::key_type(), IndexKind::Unique))
.with_field(("tag", String::key_type(), IndexKind::Index))
.with_field(("timestamp", u32::key_type(), IndexKind::Unique))
// add key fields from nested document
.with_fields(MetaData::key_fields().with_parent("meta"))
}
}
impl Document for MetaData {
// declare key fields for index
fn key_fields() -> KeyFields {
KeyFields::new()
// add key fields of document
.with_field(("keywords", KeyType::String, IndexKind::Index))
}
}
```
*/
use derive_document_wrapped;
use TokenStream;
use quote;
use ;