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
use std::borrow::Borrow;
use std::marker::PhantomData;
use std::net::Ipv4Addr;
use std::str::FromStr;
use std::error::Error as StdError;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::de::{Error, Visitor};
use super::mapping::{DefaultIpMapping, IpFieldType, IpMapping};

impl IpFieldType<DefaultIpMapping> for Ipv4Addr {}

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

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

# Examples

Defining an `ip` with a mapping:

```
use std::net::Ipv4Addr;
use elastic_types::ip::mapping::DefaultIpMapping;
use elastic_types::ip::Ip;

let ip = Ip::<DefaultIpMapping>::new(Ipv4Addr::new(127, 0, 0, 1));
```
*/
#[derive(Debug, Clone, PartialEq)]
pub struct Ip<TMapping>
where
    TMapping: IpMapping,
{
    value: Ipv4Addr,
    _m: PhantomData<TMapping>,
}

impl<TMapping> Ip<TMapping>
where
    TMapping: IpMapping,
{
    /**
    Creates a new `Ip` with the given mapping.
    
    # Examples
    
    Create a new `Ip` from a `Ip4vAddr`:
    
    ```
    use std::net::Ipv4Addr;
    use elastic_types::ip::mapping::DefaultIpMapping;
    use elastic_types::ip::Ip;
    
    let ip = Ip::<DefaultIpMapping>::new(Ipv4Addr::new(127, 0, 0, 1));
    ```
    */
    pub fn new<I>(ip: I) -> Ip<TMapping>
    where
        I: Into<Ipv4Addr>,
    {
        Ip {
            value: ip.into(),
            _m: PhantomData,
        }
    }

    /**
    Change the mapping of this ip.
    
    # Examples
    
    Change the mapping for a given `Ip`:
    
    ```
    # extern crate serde;
    # #[macro_use]
    # extern crate elastic_types;
    # fn main() {
    # use std::net::Ipv4Addr;
    # use elastic_types::prelude::*;
    # #[derive(Default)]
    # struct MyIpMapping;
    # impl IpMapping for MyIpMapping { }
    let es_ip = Ip::<DefaultIpMapping>::new(Ipv4Addr::new(127, 0, 0, 1));
    
    let ip: Ip<MyIpMapping> = Ip::remap(es_ip);
    # }
    ```
    */
    pub fn remap<TNewMapping>(ip: Ip<TMapping>) -> Ip<TNewMapping>
    where
        TNewMapping: IpMapping,
    {
        Ip::new(ip.value)
    }
}

impl<TMapping> IpFieldType<TMapping> for Ip<TMapping>
where
    TMapping: IpMapping,
{
}

impl_mapping_type!(Ipv4Addr, Ip, IpMapping);

// Serialize elastic ip
impl<TMapping> Serialize for Ip<TMapping>
where
    TMapping: IpMapping,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.value.to_string())
    }
}

// Deserialize elastic ip
impl<'de, TMapping> Deserialize<'de> for Ip<TMapping>
where
    TMapping: IpMapping,
{
    fn deserialize<D>(deserializer: D) -> Result<Ip<TMapping>, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Default)]
        struct IpVisitor<TMapping> {
            _m: PhantomData<TMapping>,
        }

        impl<'de, TMapping> Visitor<'de> for IpVisitor<TMapping>
        where
            TMapping: IpMapping,
        {
            type Value = Ip<TMapping>;

            fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                write!(formatter, "a json string containing an IpV4 address")
            }

            fn visit_string<E>(self, v: String) -> Result<Ip<TMapping>, E>
            where
                E: Error,
            {
                let de = try!(Ipv4Addr::from_str(&v).map_err(|e| E::custom(e.description().to_string())));

                Ok(Ip::new(de))
            }

            fn visit_str<E>(self, v: &str) -> Result<Ip<TMapping>, E>
            where
                E: Error,
            {
                let de = try!(Ipv4Addr::from_str(v).map_err(|e| E::custom(e.description().to_string())));

                Ok(Ip::new(de))
            }
        }

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

#[cfg(test)]
mod tests {
    use serde_json;
    use std::net::Ipv4Addr;

    use prelude::*;

    #[derive(Default)]
    struct MyIpMapping;
    impl IpMapping for MyIpMapping {}

    #[test]
    fn can_change_ip_mapping() {
        fn takes_custom_mapping(_: Ip<MyIpMapping>) -> bool {
            true
        }

        let ip: Ip<DefaultIpMapping> = Ip::new(Ipv4Addr::new(127, 0, 0, 1));

        assert!(takes_custom_mapping(Ip::remap(ip)));
    }

    #[test]
    fn serialise_elastic_ip() {
        let ip: Ip<DefaultIpMapping> = Ip::new(Ipv4Addr::new(127, 0, 0, 1));

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

        assert_eq!(r#""127.0.0.1""#, ser);
    }

    #[test]
    fn deserialise_elastic_ip() {
        let ip: Ip<DefaultIpMapping> = serde_json::from_str(r#""127.0.0.1""#).unwrap();

        assert_eq!(Ipv4Addr::new(127, 0, 0, 1), ip);
    }

}