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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use {ViewName, serde, std};

/// Container for a _map_ and optional _reduce_ function of a view.
///
/// `ViewFunction` is a convenience type for applications that work with view
/// functions in design documents. For more information about view functions and
/// design documents, please see the CouchDB documentation.
///
/// # Examples
///
/// ```
/// extern crate chill;
///
/// let view_function = chill::ViewFunction::new_with_reduce(
///     "function(doc) { emit(doc.key_thing, doc.value_thing); }",
///     "_sum");
///
/// assert_eq!("function(doc) { emit(doc.key_thing, doc.value_thing); }",
///            view_function.map);
/// assert_eq!(Some(String::from("_sum")), view_function.reduce);
/// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ViewFunction {
    /// The view's map function.
    ///
    /// For more information about _map functions_, please see the CouchDB
    /// documentation.
    ///
    pub map: String,

    /// The view's reduce function, if available.
    ///
    /// For more information about _reduce functions_, please see the CouchDB
    /// documentation.
    ///
    pub reduce: Option<String>,

    // This field exists to prevent applications from directly constructing this
    // struct.
    _dummy: std::marker::PhantomData<()>,
}

impl ViewFunction {
    /// Constructs a new `ViewFunction` that has no _reduce function_.
    pub fn new<M: Into<String>>(map: M) -> Self {
        ViewFunction {
            map: map.into(),
            reduce: None,
            _dummy: std::marker::PhantomData,
        }
    }

    /// Constructs a new `ViewFunction` that has a _reduce_ function.
    pub fn new_with_reduce<M: Into<String>, R: Into<String>>(map: M, reduce: R) -> Self {
        ViewFunction {
            map: map.into(),
            reduce: Some(reduce.into()),
            _dummy: std::marker::PhantomData,
        }
    }
}

impl serde::Deserialize for ViewFunction {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        enum Field {
            Map,
            Reduce,
        }

        impl serde::Deserialize for Field {
            fn deserialize<D>(deserializer: &mut D) -> Result<Field, D::Error>
                where D: serde::Deserializer
            {
                struct Visitor;

                impl serde::de::Visitor for Visitor {
                    type Value = Field;

                    fn visit_str<E>(&mut self, value: &str) -> Result<Field, E>
                        where E: serde::de::Error
                    {
                        match value {
                            "map" => Ok(Field::Map),
                            "reduce" => Ok(Field::Reduce),
                            _ => Err(E::unknown_field(value)),
                        }
                    }
                }

                deserializer.deserialize(Visitor)
            }
        }

        struct Visitor;

        impl serde::de::Visitor for Visitor {
            type Value = ViewFunction;

            fn visit_map<V>(&mut self, mut visitor: V) -> Result<Self::Value, V::Error>
                where V: serde::de::MapVisitor
            {
                let mut map = None;
                let mut reduce = None;

                loop {
                    match try!(visitor.visit_key()) {
                        Some(Field::Map) => {
                            map = Some(try!(visitor.visit_value()));
                        }
                        Some(Field::Reduce) => {
                            reduce = Some(try!(visitor.visit_value()));
                        }
                        None => {
                            break;
                        }
                    }
                }

                try!(visitor.end());

                let map = match map {
                    Some(x) => x,
                    None => try!(visitor.missing_field("map")),
                };

                Ok(ViewFunction {
                    map: map,
                    reduce: reduce,
                    _dummy: std::marker::PhantomData,
                })
            }
        }

        static FIELDS: &'static [&'static str] = &["map", "reduce"];
        deserializer.deserialize_struct("SavedAttachment", FIELDS, Visitor)
    }
}

impl serde::Serialize for ViewFunction {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        let len = if self.reduce.is_some() { 2 } else { 1 };
        let mut state = try!(serializer.serialize_struct("ViewFunction", len));
        try!(serializer.serialize_struct_elt(&mut state, "map", &self.map));
        if let Some(ref reduce) = self.reduce {
            try!(serializer.serialize_struct_elt(&mut state, "reduce", reduce));
        }
        serializer.serialize_struct_end(state)
    }
}

/// Container for the content of a design document.
///
/// `Design` is a convenience type for applications that create, read, or update
/// design documents.
///
/// Currently, `Design` supports only the `views` field of a design document.
/// For more information about design documents, please see the CouchDB
/// documentation.
///
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Design {
    /// The view functions stored within the design document, if any.
    pub views: std::collections::HashMap<ViewName, ViewFunction>,

    // This field exists to prevent applications from directly constructing this
    // struct.
    _dummy: std::marker::PhantomData<()>,
}

impl serde::Deserialize for Design {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        enum Field {
            Views,
        }

        impl serde::Deserialize for Field {
            fn deserialize<D>(deserializer: &mut D) -> Result<Field, D::Error>
                where D: serde::Deserializer
            {
                struct Visitor;

                impl serde::de::Visitor for Visitor {
                    type Value = Field;

                    fn visit_str<E>(&mut self, value: &str) -> Result<Field, E>
                        where E: serde::de::Error
                    {
                        match value {
                            "views" => Ok(Field::Views),
                            _ => Err(E::unknown_field(value)),
                        }
                    }
                }

                deserializer.deserialize(Visitor)
            }
        }

        struct Visitor;

        impl serde::de::Visitor for Visitor {
            type Value = Design;

            fn visit_map<V>(&mut self, mut visitor: V) -> Result<Self::Value, V::Error>
                where V: serde::de::MapVisitor
            {
                let mut views = None;

                loop {
                    match try!(visitor.visit_key()) {
                        Some(Field::Views) => {
                            views = Some(try!(visitor.visit_value()));
                        }
                        None => {
                            break;
                        }
                    }
                }

                try!(visitor.end());

                let views = match views {
                    Some(x) => x,
                    None => std::collections::HashMap::new(),
                };

                Ok(Design {
                    views: views,
                    _dummy: std::marker::PhantomData,
                })
            }
        }

        static FIELDS: &'static [&'static str] = &["views"];
        deserializer.deserialize_struct("Design", FIELDS, Visitor)
    }
}

impl serde::Serialize for Design {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        let mut state = try!(serializer.serialize_struct("Design", 1));
        try!(serializer.serialize_struct_elt(&mut state, "views", &self.views));
        serializer.serialize_struct_end(state)
    }
}

/// Builder for a design document's content.
///
/// `Builder` is a convenience type for applications that create new design
/// documents. For more information about design documents, please see the
/// CouchDB documentation.
///
#[derive(Debug)]
pub struct DesignBuilder {
    inner: Design,
}

impl DesignBuilder {
    /// Constructs a new builder containing empty design document content.
    pub fn new() -> Self {
        DesignBuilder {
            inner: Design {
                views: std::collections::HashMap::new(),
                _dummy: std::marker::PhantomData,
            },
        }
    }

    /// Returns the builder's design document content.
    pub fn unwrap(self) -> Design {
        self.inner
    }

    /// Inserts a view into the design document content.
    pub fn insert_view<V>(mut self, view_name: V, view_function: ViewFunction) -> Self
        where V: Into<ViewName>
    {
        self.inner.views.insert(view_name.into(), view_function);
        self
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use {serde_json, std};

    #[test]
    fn view_function_new() {

        let map_function = r#"function(doc) { emit(doc.key, doc.value); }"#;

        let expected = ViewFunction {
            map: String::from(map_function),
            reduce: None,
            _dummy: std::marker::PhantomData,
        };

        let got = ViewFunction::new(map_function);

        assert_eq!(expected, got);
    }

    #[test]
    fn view_function_new_with_reduce() {

        let map_function = r#"function(doc) { emit(doc.key, doc.value); }"#;
        let reduce_function = r#"_count"#;

        let expected = ViewFunction {
            map: String::from(map_function),
            reduce: Some(String::from(reduce_function)),
            _dummy: std::marker::PhantomData,
        };

        let got = ViewFunction::new_with_reduce(map_function, reduce_function);

        assert_eq!(expected, got);
    }

    #[test]
    fn view_function_serialize_without_reduce() {

        let view_function = ViewFunction::new("function(doc) { emit(doc.key_thing, doc.value_thing); }");

        let encoded = serde_json::to_string(&view_function).unwrap();

        let expected = serde_json::builder::ObjectBuilder::new()
            .insert("map", &view_function.map)
            .build();

        let got = serde_json::from_str(&encoded).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn view_function_serialize_with_reduce() {

        let view_function = ViewFunction::new_with_reduce("function(doc) { emit(doc.key_thing, doc.value_thing); }",
                                                          "_sum");

        let encoded = serde_json::to_string(&view_function).unwrap();

        let expected = serde_json::builder::ObjectBuilder::new()
            .insert("map", &view_function.map)
            .insert("reduce", &view_function.reduce)
            .build();

        let got = serde_json::from_str(&encoded).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn view_function_deserialize_ok_without_reduce() {

        let expected = ViewFunction::new("function(doc) { emit(doc.key_thing, doc.value_thing); }");

        let source = serde_json::builder::ObjectBuilder::new()
            .insert("map",
                    "function(doc) { emit(doc.key_thing, doc.value_thing); }")
            .build();

        let source = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str(&source).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn view_function_deserialize_ok_with_reduce() {

        let expected = ViewFunction::new_with_reduce("function(doc) { emit(doc.key_thing, doc.value_thing); }",
                                                     "_sum");

        let source = serde_json::builder::ObjectBuilder::new()
            .insert("map",
                    "function(doc) { emit(doc.key_thing, doc.value_thing); }")
            .insert("reduce", "_sum")
            .build();

        let source = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str(&source).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn view_function_deserialize_nok_missing_map() {

        let source = serde_json::builder::ObjectBuilder::new()
            .insert("reduce", "_sum")
            .build();

        let source = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str::<ViewFunction>(&source);
        expect_json_error_missing_field!(got, "map");
    }

    #[test]
    fn design_serialize() {

        let design = DesignBuilder::new()
            .insert_view("alpha",
                         ViewFunction::new("function(doc) { emit(doc.key_thing, doc.value_thing); }"))
            .insert_view("bravo",
                         ViewFunction::new_with_reduce("function(doc) { emit(doc.key_thing_2, doc.value_thing_2); }",
                                                       "_sum"))
            .unwrap();

        let encoded = serde_json::to_string(&design).unwrap();

        let expected = serde_json::builder::ObjectBuilder::new()
            .insert_object("views", |x| {
                x.insert_object("alpha", |x| {
                        x.insert("map",
                                 "function(doc) { emit(doc.key_thing, doc.value_thing); }")
                    })
                    .insert_object("bravo", |x| {
                        x.insert("map",
                                    "function(doc) { emit(doc.key_thing_2, doc.value_thing_2); }")
                            .insert("reduce", "_sum")
                    })
            })
            .build();

        let got = serde_json::from_str(&encoded).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn design_deserialize_ok_empty() {
        let expected = DesignBuilder::new().unwrap();
        let source = serde_json::builder::ObjectBuilder::new().build();
        let source = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str(&source).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn design_deserialize_ok_with_views() {

        let expected = DesignBuilder::new()
            .insert_view("alpha",
                         ViewFunction::new("function(doc) { emit(doc.key_thing, doc.value_thing); }"))
            .insert_view("bravo",
                         ViewFunction::new_with_reduce("function(doc) { emit(doc.key_thing_2, doc.value_thing_2); }",
                                                       "_sum"))
            .unwrap();

        let source = serde_json::builder::ObjectBuilder::new()
            .insert_object("views", |x| {
                x.insert_object("alpha", |x| {
                        x.insert("map",
                                 "function(doc) { emit(doc.key_thing, doc.value_thing); }")
                    })
                    .insert_object("bravo", |x| {
                        x.insert("map",
                                    "function(doc) { emit(doc.key_thing_2, doc.value_thing_2); }")
                            .insert("reduce", "_sum")
                    })
            })
            .build();

        let source = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str(&source).unwrap();
        assert_eq!(expected, got);
    }
}