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
use std::borrow::Borrow;
use std::marker::PhantomData;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::de::{Error, Visitor};
use super::mapping::{BooleanFieldType, BooleanMapping, DefaultBooleanMapping};

impl BooleanFieldType<DefaultBooleanMapping> for bool {}

/**
An Elasticsearch `boolean` with a mapping.

Where the mapping isn't custom, you can use the standard library `bool` instead.

# Examples

Defining a `bool` with a mapping:

```
# use elastic_types::prelude::*;
let boolean = Boolean::<DefaultBooleanMapping>::new(true);
```
*/
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Boolean<TMapping>
where
    TMapping: BooleanMapping,
{
    value: bool,
    _m: PhantomData<TMapping>,
}

impl<TMapping> Boolean<TMapping>
where
    TMapping: BooleanMapping,
{
    /**
    Creates a new `Boolean` with the given mapping.
    
    # Examples
    
    Create a new `Boolean` from a `bool`:
    
    ```
    # use elastic_types::prelude::*;
    let boolean = Boolean::<DefaultBooleanMapping>::new(false);
    ```
    */
    pub fn new<I>(boolean: I) -> Boolean<TMapping>
    where
        I: Into<bool>,
    {
        Boolean {
            value: boolean.into(),
            _m: PhantomData,
        }
    }

    /**
    Change the mapping of this boolean.
    
    # Examples
    
    Change the mapping for a given `Boolean`:
    
    ```
    # extern crate serde;
    # #[macro_use]
    # extern crate elastic_types;
    # fn main() {
    # use elastic_types::prelude::*;
    # #[derive(Default)]
    # struct MyBooleanMapping;
    # impl BooleanMapping for MyBooleanMapping { }
    let boolean = Boolean::<DefaultBooleanMapping>::new(true);
    
    let boolean: Boolean<MyBooleanMapping> = Boolean::remap(boolean);
    # }
    ```
    */
    pub fn remap<TNewMapping>(boolean: Boolean<TMapping>) -> Boolean<TNewMapping>
    where
        TNewMapping: BooleanMapping,
    {
        Boolean::<TNewMapping>::new(boolean.value)
    }
}

impl<TMapping> BooleanFieldType<TMapping> for Boolean<TMapping>
where
    TMapping: BooleanMapping,
{
}

impl_mapping_type!(bool, Boolean, BooleanMapping);

impl<TMapping> Serialize for Boolean<TMapping>
where
    TMapping: BooleanMapping,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_bool(self.value)
    }
}

impl<'de, TMapping> Deserialize<'de> for Boolean<TMapping>
where
    TMapping: BooleanMapping,
{
    fn deserialize<D>(deserializer: D) -> Result<Boolean<TMapping>, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Default)]
        struct BooleanVisitor<TMapping> {
            _m: PhantomData<TMapping>,
        }

        impl<'de, TMapping> Visitor<'de> for BooleanVisitor<TMapping>
        where
            TMapping: BooleanMapping,
        {
            type Value = Boolean<TMapping>;

            fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                write!(formatter, "a json boolean")
            }

            fn visit_bool<E>(self, v: bool) -> Result<Boolean<TMapping>, E>
            where
                E: Error,
            {
                Ok(Boolean::<TMapping>::new(v))
            }
        }

        deserializer.deserialize_any(BooleanVisitor::<TMapping>::default())
    }
}

#[cfg(test)]
mod tests {
    use serde_json;

    use prelude::*;

    #[derive(Default)]
    struct MyBooleanMapping;
    impl BooleanMapping for MyBooleanMapping {}

    #[test]
    fn can_change_boolean_mapping() {
        fn takes_custom_mapping(_: Boolean<MyBooleanMapping>) -> bool {
            true
        }

        let boolean: Boolean<DefaultBooleanMapping> = Boolean::new(true);

        assert!(takes_custom_mapping(Boolean::remap(boolean)));
    }

    #[test]
    fn serialise_elastic_boolean() {
        let boolean: Boolean<DefaultBooleanMapping> = Boolean::new(true);

        let ser = serde_json::to_string(&boolean).unwrap();

        assert_eq!("true", ser);
    }

    #[test]
    fn deserialise_elastic_boolean() {
        let boolean: Boolean<DefaultBooleanMapping> = serde_json::from_str("true").unwrap();

        assert_eq!(true, boolean);
    }

}