rust_eureka/request/
leaseinfo.rs

1use serde::ser::{Serialize, Serializer, SerializeStruct};
2use serde::de::{Deserialize, Deserializer, Visitor, Error as DeError, MapAccess};
3use std::fmt;
4
5const LEASE_INFO: &'static str = "LeaseInfo";
6const EVICTION_DURATION_IN_SECS: &'static str = "evictionDurationInSecs";
7const FIELDS: &'static [&'static str] = &[EVICTION_DURATION_IN_SECS];
8
9#[derive(Debug, PartialEq)]
10pub struct LeaseInfo {
11    pub eviction_duration_in_secs: Option<u32>
12}
13
14impl Serialize for LeaseInfo {
15    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
16        S: Serializer {
17        let mut s = serializer.serialize_struct(LEASE_INFO, 1)?;
18        // if not specified we will serialize the default of 90
19        let result = self.eviction_duration_in_secs.unwrap_or(90);
20        s.serialize_field(EVICTION_DURATION_IN_SECS, &result)?;
21        s.end()
22    }
23}
24
25impl<'de> Deserialize<'de> for LeaseInfo {
26    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
27        D: Deserializer<'de> {
28        enum Field { EvictionDurationInSecs };
29
30        impl<'de> Deserialize<'de> for Field {
31            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
32                D: Deserializer<'de> {
33                struct FieldVisitor;
34
35                impl<'de> Visitor<'de> for FieldVisitor {
36                    type Value = Field;
37
38                    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
39                        formatter.write_str("Expecting eviction_duration_in_secs")
40                    }
41
42                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where
43                        E: DeError {
44
45                        match v {
46                            EVICTION_DURATION_IN_SECS => Ok(Field::EvictionDurationInSecs),
47                            _ => Err(DeError::unknown_field(v, FIELDS))
48                        }
49                    }
50
51                }
52                deserializer.deserialize_identifier(FieldVisitor)
53            }
54        }
55
56        struct LeaseInfoVisitor;
57
58        impl<'de> Visitor<'de> for LeaseInfoVisitor {
59            type Value = LeaseInfo;
60
61            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
62                formatter.write_str("struct LeaseInfoVisitor")
63            }
64            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where
65                A: MapAccess<'de> {
66                let mut maybe_eviction_duration = None;
67
68                while let Some(key) = map.next_key()? {
69                    match key {
70                        Field::EvictionDurationInSecs => {
71                            if maybe_eviction_duration.is_some() {
72                                return Err(DeError::duplicate_field(EVICTION_DURATION_IN_SECS));
73                            }
74                            maybe_eviction_duration = Some(map.next_value()?);
75                        }
76                    }
77                }
78                Ok(LeaseInfo{
79                    eviction_duration_in_secs: maybe_eviction_duration
80                })
81            }
82        }
83
84        deserializer.deserialize_struct(LEASE_INFO, FIELDS, LeaseInfoVisitor)
85    }
86}
87
88#[cfg(test)]
89mod test {
90    use super::*;
91    use serde_json;
92
93    #[test]
94    fn test_lease_info_some() {
95        let li = LeaseInfo { eviction_duration_in_secs: Some(9600) };
96        let json = r#"{"evictionDurationInSecs":9600}"#;
97        let result = serde_json::to_string(&li).unwrap();
98        assert_eq!(json, result);
99    }
100
101    #[test]
102    fn test_lease_info_none() {
103        let li = LeaseInfo { eviction_duration_in_secs: None };
104        let json = r#"{"evictionDurationInSecs":90}"#;
105        let result = serde_json::to_string(&li).unwrap();
106        assert_eq!(json, result);
107    }
108
109    #[test]
110    fn test_deserialize_lease_info_some() {
111        let li = LeaseInfo { eviction_duration_in_secs: Some(90) };
112        let json = r#"{"evictionDurationInSecs":90}"#;
113        let result = serde_json::from_str(&json).unwrap();
114        assert_eq!(li, result);
115    }
116
117}