ledb_types/lib.rs
1/*!
2
3# Types and traits for storable documents
4
5## Document trait
6
7The basic trait which should be implemented for structs which designed to be handled as documents.
8
9```rust
10use serde::{Serialize, Deserialize};
11use ledb_types::{Document, Identifier, Primary, KeyFields, KeyType, IndexKind};
12
13#[derive(Serialize, Deserialize)]
14struct MyDoc {
15 // define optional primary key field
16 id: Option<Primary>,
17 // define other fields
18 title: String,
19 tag: Vec<String>,
20 timestamp: u32,
21 // define nested document
22 meta: MetaData,
23}
24
25#[derive(Serialize, Deserialize)]
26struct MetaData {
27 // define index field
28 keywords: Vec<String>,
29 // define other fields
30 description: String,
31}
32
33impl Document for MyDoc {
34 // declare primary key field name
35 fn primary_field() -> Identifier {
36 "id".into()
37 }
38
39 // declare other key fields
40 fn key_fields() -> KeyFields {
41 KeyFields::new()
42 // add key fields of document
43 .with_field(("title", KeyType::String, IndexKind::Unique))
44 .with_field(("tag", KeyType::String, IndexKind::Index))
45 .with_field(("timestamp", KeyType::Int, IndexKind::Unique))
46 // add key fields from nested document
47 .with_fields(MetaData::key_fields().with_parent("meta"))
48 }
49}
50
51impl Document for MetaData {
52 // declare key fields for index
53 fn key_fields() -> KeyFields {
54 KeyFields::new()
55 // add key fields of document
56 .with_field(("keywords", KeyType::String, IndexKind::Index))
57 }
58}
59```
60
61## DocumentKeyType trait
62
63This trait maps rust types to key types.
64
65*/
66
67//#[cfg(feature = "json")]
68//extern crate serde_json;
69
70//#[cfg(feature = "cbor")]
71//extern crate serde_cbor;
72
73//#[cfg(feature = "bytes")]
74//extern crate bytes;
75
76mod document;
77mod identifier;
78mod index;
79
80pub use self::document::*;
81pub use self::identifier::*;
82pub use self::index::*;