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
use std::marker::PhantomData;
use serde::{Serialize, Serializer};
use serde::ser::SerializeStruct;
pub trait DocumentFieldType<M>
where
M: DocumentMapping,
{
}
pub const OBJECT_DATATYPE: &'static str = "object";
pub const DYNAMIC_DATATYPE: &'static str = "dynamic";
pub const NESTED_DATATYPE: &'static str = "nested";
pub trait DocumentMapping
where
Self: PropertiesMapping + Default,
{
fn name() -> &'static str;
fn data_type() -> &'static str {
NESTED_DATATYPE
}
fn dynamic() -> Option<Dynamic> {
None
}
fn enabled() -> Option<bool> {
None
}
fn include_in_all() -> Option<bool> {
None
}
}
pub trait PropertiesMapping {
fn props_len() -> usize;
fn serialize_props<S>(state: &mut S) -> Result<(), S::Error>
where
S: SerializeStruct;
}
#[derive(Default)]
struct Properties<TMapping>
where
TMapping: DocumentMapping,
{
_m: PhantomData<TMapping>,
}
impl<TMapping> Serialize for Properties<TMapping>
where
TMapping: DocumentMapping,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = try!(serializer.serialize_struct("properties", TMapping::props_len()));
try!(TMapping::serialize_props(&mut state));
state.end()
}
}
#[derive(Debug, Clone, Copy)]
pub enum Dynamic {
True,
False,
Strict,
}
impl Serialize for Dynamic {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
Dynamic::True => serializer.serialize_bool(true),
Dynamic::False => serializer.serialize_bool(false),
Dynamic::Strict => serializer.serialize_str("strict"),
}
}
}
mod private {
use serde::{Serialize, Serializer};
use serde::ser::SerializeStruct;
use document::{DocumentType, IndexDocumentMapping};
use private::field::{DocumentField, FieldMapping, FieldType};
use super::{DocumentFieldType, DocumentMapping, Properties, OBJECT_DATATYPE};
#[derive(Default)]
pub struct DocumentPivot;
impl<TField, TMapping> FieldType<TMapping, DocumentPivot> for TField
where
TMapping: DocumentMapping,
TField: DocumentFieldType<TMapping>,
{
}
impl<TDocument, TMapping> DocumentFieldType<TMapping> for TDocument
where
TDocument: DocumentType<Mapping = TMapping>,
TMapping: DocumentMapping,
{
}
impl<TMapping> FieldMapping<DocumentPivot> for TMapping
where
TMapping: DocumentMapping,
{
type DocumentField = DocumentField<TMapping, DocumentPivot>;
fn data_type() -> &'static str {
<Self as DocumentMapping>::data_type()
}
}
impl<TMapping> Serialize for DocumentField<TMapping, DocumentPivot>
where
TMapping: FieldMapping<DocumentPivot> + DocumentMapping,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let ty = <TMapping as DocumentMapping>::data_type();
let (is_object, has_props) = (ty == OBJECT_DATATYPE, TMapping::props_len() > 0);
let props_len = match (is_object, has_props) {
(true, true) => 5,
(true, false) | (false, true) => 4,
(false, false) => 3,
};
let mut state = try!(serializer.serialize_struct("mapping", props_len));
try!(state.serialize_field("type", ty));
ser_field!(state, "dynamic", TMapping::dynamic());
ser_field!(state, "include_in_all", TMapping::include_in_all());
if is_object {
ser_field!(state, "enabled", TMapping::enabled());
}
if has_props {
try!(state.serialize_field("properties", &Properties::<TMapping>::default()));
}
state.end()
}
}
impl<TMapping> Serialize for IndexDocumentMapping<TMapping>
where
TMapping: DocumentMapping,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = try!(serializer.serialize_struct("mapping", 1));
try!(state.serialize_field("properties", &Properties::<TMapping>::default()));
state.end()
}
}
}