rust_eureka/response/
action_type.rs1use std::fmt;
2use std::convert::From;
3use std::str::FromStr;
4use std::error::Error;
5use serde::ser::{Serialize, Serializer};
6use serde::de::{Deserialize, Deserializer, Visitor, Error as DeError};
7
8const ADDED: &'static str = "ADDED";
9const DELETED: &'static str = "DELETED";
10const MODIFIED: &'static str = "MODIFIED";
11
12#[derive(Debug, PartialEq)]
13pub enum ActionType {
14 Added,
15 Deleted,
16 Modified
17}
18
19impl ActionType {
20 fn values() -> Vec<ActionType> {
21 use self::ActionType::*;
22 vec![Added, Deleted, Modified]
23 }
24}
25
26#[derive(Debug)]
27pub struct InvalidActionTypeError {
28 invalid_value: String
29}
30
31impl InvalidActionTypeError {
32 pub fn new(invalid_nm: &str) -> Self {
33 InvalidActionTypeError {
34 invalid_value: invalid_nm.to_owned()
35 }
36 }
37}
38
39
40impl fmt::Display for InvalidActionTypeError {
41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42 write!(f, "InvalidActionTypeError({})", self.invalid_value)
43 }
44}
45
46impl Error for InvalidActionTypeError {
47 fn description(&self) -> &str {
48 "Not a valid ActionType"
49 }
50}
51
52impl FromStr for ActionType {
53 type Err = InvalidActionTypeError;
54
55 fn from_str(s: &str) -> Result<Self, Self::Err> {
56 match s {
57 ADDED => Ok(ActionType::Added),
58 DELETED => Ok(ActionType::Deleted),
59 MODIFIED => Ok(ActionType::Modified),
60 _ => Err(InvalidActionTypeError::new(s))
61 }
62 }
63}
64
65impl From<ActionType> for String {
66 fn from(s: ActionType) -> Self {
67 match s {
68 ActionType::Added => ADDED.to_string(),
69 ActionType::Deleted => DELETED.to_string(),
70 ActionType::Modified => MODIFIED.to_string(),
71 }
72 }
73}
74
75impl<'a> From<&'a ActionType> for String {
76 fn from(s: &ActionType) -> Self {
77 match s {
78 &ActionType::Added => ADDED.to_string(),
79 &ActionType::Deleted => DELETED.to_string(),
80 &ActionType::Modified => MODIFIED.to_string(),
81 }
82 }
83}
84
85impl Serialize for ActionType {
86 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
87 S: Serializer {
88 serializer.serialize_str(String::from(self).as_ref())
89 }
90}
91
92impl<'de> Deserialize<'de> for ActionType {
93 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
94 D: Deserializer<'de> {
95 struct ActionTypeVisitor;
96
97 impl<'de> Visitor<'de> for ActionTypeVisitor {
98 type Value = ActionType;
99
100 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
101 let values = ActionType::values()
102 .iter()
103 .fold(String::new(), |mut acc, v| {
104 acc.push_str(String::from(v).as_ref());
105 acc
106 });
107
108 formatter.write_fmt(format_args!("Expecting {}", values))
109 }
110
111 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where
112 E: DeError {
113
114 ActionType::from_str(v)
115 .map_err(|err| E::custom(format!("{}", err)))
116 }
117 }
118
119 deserializer.deserialize_str(ActionTypeVisitor)
120 }
121}
122
123#[cfg(test)]
124mod test {
125 use super::*;
126
127 #[test]
128 fn test_from_str() {
129 assert_eq!(ActionType::Added, ActionType::from_str(ADDED).unwrap());
130 assert_eq!(ActionType::Deleted, ActionType::from_str(DELETED).unwrap());
131 assert_eq!(ActionType::Modified, ActionType::from_str(MODIFIED).unwrap());
132 }
133
134 #[test]
135 #[should_panic]
136 fn test_from_str_invalid() {
137 ActionType::from_str("sfd2ef").unwrap();
138 }
139
140 #[test]
141 fn test_to_string() {
142 assert_eq!(ADDED.to_owned(), String::from(ActionType::Added));
143 assert_eq!(DELETED.to_owned(), String::from(ActionType::Deleted));
144 assert_eq!(MODIFIED.to_owned(), String::from(ActionType::Modified));
145 }
146}
147