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
use std::fmt;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::de::Visitor;
use crate::error::ParamsError;
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct Params {
pub hostname: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub mac: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ip: Option<String>,
pub now: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Tags>,
}
impl Params {
pub fn builder() -> ParamsBuilder {
ParamsBuilder::new()
}
pub(crate) fn set_now(&mut self, now: i64) -> &mut Self {
self.now = now;
self
}
}
pub struct ParamsBuilder {
hostname: Option<String>,
mac: Option<String>,
ip: Option<String>,
tags: Option<Tags>,
}
impl ParamsBuilder {
pub fn new() -> Self {
Self {
hostname: None,
mac: None,
ip: None,
tags: None,
}
}
pub fn hostname<T: Into<String>>(&mut self, hostname: T) -> &mut Self {
self.hostname = Some(hostname.into());
self
}
pub fn mac<T: Into<String>>(&mut self, mac: T) -> &mut Self {
self.mac = Some(mac.into());
self
}
pub fn ip<T: Into<String>>(&mut self, ip: T) -> &mut Self {
self.hostname = Some(ip.into());
self
}
pub fn tags<T: Into<Tags>>(&mut self, tags: T) -> &mut Self {
self.tags = Some(tags.into());
self
}
pub fn build(&mut self) -> Result<Params, ParamsError> {
Ok(Params {
hostname: self.hostname.clone()
.ok_or(ParamsError::RequiredField("hostname is required in a ParamsBuilder".into()))?,
mac: self.mac.clone(),
ip: self.ip.clone(),
now: 0,
tags: self.tags.clone(),
})
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Tags {
inner: Vec<String>
}
impl Tags {
pub fn new() -> Self {
Self {
inner: Vec::new()
}
}
pub fn parse<T: Into<String>>(tags: T) -> Self {
Self {
inner: tags.into().split_terminator(",").map(|s| s.to_string()).collect()
}
}
pub fn add<T: Into<String>>(&mut self, tag: T) -> &mut Self {
self.inner.push(tag.into());
self
}
}
impl From<String> for Tags {
fn from(input: String) -> Self {
Tags::parse(input)
}
}
impl<'a> From<&'a str> for Tags {
fn from(input: &'a str) -> Self {
Tags::parse(input)
}
}
impl From<Vec<String>> for Tags
{
fn from(input: Vec<String>) -> Self {
let mut tags = Tags::new();
input.into_iter().for_each(|t| { tags.add(t); });
tags
}
}
impl Serialize for Tags {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
serializer.serialize_str(&self.inner.join(","))
}
}
impl<'de> Deserialize<'de> for Tags {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
struct StrVisitor {}
impl<'de> Visitor<'de> for StrVisitor {
type Value = Tags;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("comma separated string, e.g a,b,c")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> {
Ok(Tags {
inner: v.split_terminator(",").map(|s| s.to_string()).collect()
})
}
}
deserializer.deserialize_str(StrVisitor {})
}
}