eva_common/
common_payloads.rs

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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::events::NodeInfo;
use crate::value::Value;
use crate::OID;
use rand::seq::SliceRandom;
use rand::thread_rng;
use serde::{Deserialize, Deserializer, Serialize};
use std::sync::atomic;
use std::sync::Arc;
use std::time::Duration;
use uuid::Uuid;

pub fn deserialize_uuid<'de, D>(deserializer: D) -> Result<Uuid, D::Error>
where
    D: Deserializer<'de>,
{
    let val: Value = Deserialize::deserialize(deserializer)?;
    Uuid::deserialize(val).map_err(serde::de::Error::custom)
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NodeData {
    svc: Option<String>,
    #[serde(
        deserialize_with = "crate::tools::deserialize_arc_atomic_bool",
        serialize_with = "crate::tools::serialize_atomic_bool"
    )]
    online: Arc<atomic::AtomicBool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    info: Option<NodeInfo>,
    #[serde(
        default,
        serialize_with = "crate::tools::serialize_opt_duration_as_f64",
        deserialize_with = "crate::tools::de_opt_float_as_duration"
    )]
    timeout: Option<Duration>,
}

impl NodeData {
    #[inline]
    pub fn new(
        svc: Option<&str>,
        online: bool,
        info: Option<NodeInfo>,
        timeout: Option<Duration>,
    ) -> Self {
        Self {
            svc: svc.map(ToOwned::to_owned),
            online: Arc::new(atomic::AtomicBool::new(online)),
            info,
            timeout,
        }
    }
    #[inline]
    pub fn svc(&self) -> Option<&str> {
        self.svc.as_deref()
    }
    #[inline]
    pub fn online(&self) -> bool {
        self.online.load(atomic::Ordering::SeqCst)
    }
    #[inline]
    pub fn online_beacon(&self) -> Arc<atomic::AtomicBool> {
        self.online.clone()
    }
    #[inline]
    pub fn info(&self) -> Option<&NodeInfo> {
        self.info.as_ref()
    }
    #[inline]
    pub fn timeout(&self) -> Option<Duration> {
        self.timeout
    }
    #[inline]
    pub fn set_online(&self, online: bool) {
        self.online.store(online, atomic::Ordering::SeqCst);
    }
    #[inline]
    pub fn update_info(&mut self, info: NodeInfo) {
        self.info.replace(info);
    }
    #[inline]
    pub fn update_timeout(&mut self, timeout: Option<Duration>) {
        self.timeout = timeout;
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsId<'a> {
    #[serde(borrow)]
    pub i: &'a str,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsIdOwned {
    pub i: String,
}

pub type IdOrList<'a> = ValueOrList<&'a str>;

pub type IdOrListOwned = ValueOrList<String>;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsIdOrList<'a> {
    #[serde(borrow)]
    pub i: IdOrList<'a>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsIdOrListOwned {
    pub i: IdOrListOwned,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsIdList<'a> {
    #[serde(borrow)]
    pub i: Vec<&'a str>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ParamsIdListOwned {
    pub i: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsOID {
    pub i: OID,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsUuid {
    pub u: Uuid,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsUuidAny {
    #[serde(deserialize_with = "deserialize_uuid")]
    pub u: Uuid,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum ValueOrList<T>
where
    T: Send + Sync + Clone,
{
    Single(T),
    Multiple(Vec<T>),
}

impl<T> Default for ValueOrList<T>
where
    T: Send + Sync + Clone,
{
    #[inline]
    fn default() -> Self {
        ValueOrList::Multiple(Vec::new())
    }
}

impl<T> ValueOrList<T>
where
    T: Send + Sync + Clone + PartialEq,
{
    pub fn is_empty(&self) -> bool {
        match self {
            ValueOrList::Single(_) => false,
            ValueOrList::Multiple(v) => v.is_empty(),
        }
    }
    pub fn len(&self) -> usize {
        match self {
            ValueOrList::Single(_) => 1,
            ValueOrList::Multiple(v) => v.len(),
        }
    }
    pub fn shuffle(&mut self) {
        if let ValueOrList::Multiple(ref mut v) = self {
            v.shuffle(&mut thread_rng());
        }
    }
    #[inline]
    pub fn iter(&self) -> impl Iterator<Item = &T> + '_ {
        self.into_iter()
    }
    pub fn to_vec(&self) -> Vec<T> {
        match self {
            ValueOrList::Single(v) => vec![v.clone()],
            ValueOrList::Multiple(v) => v.clone(),
        }
    }
    pub fn into_vec(self) -> Vec<T> {
        match self {
            ValueOrList::Single(v) => vec![v],
            ValueOrList::Multiple(v) => v,
        }
    }
    pub fn contains(&self, value: &T) -> bool {
        match self {
            ValueOrList::Single(v) => v == value,
            ValueOrList::Multiple(v) => v.contains(value),
        }
    }
}

impl<T: Send + Sync + Clone + 'static> IntoIterator for ValueOrList<T> {
    type Item = T;
    type IntoIter = Box<dyn Iterator<Item = Self::Item> + Send + Sync + 'static>;

    fn into_iter(self) -> Self::IntoIter {
        match self {
            ValueOrList::Single(s) => Box::new(SingleIter(Some(s))),
            ValueOrList::Multiple(vals) => Box::new(vals.into_iter()),
        }
    }
}

impl<'a, T: Send + Sync + Clone> IntoIterator for &'a ValueOrList<T> {
    type Item = &'a T;
    type IntoIter = Box<dyn Iterator<Item = Self::Item> + Send + Sync + 'a>;

    fn into_iter(self) -> Self::IntoIter {
        match self {
            ValueOrList::Single(s) => Box::new(SingleIter(Some(s))),
            ValueOrList::Multiple(vals) => Box::new(vals.iter()),
        }
    }
}

struct SingleIter<T>(Option<T>);

impl<T> Iterator for SingleIter<T> {
    type Item = T;
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.0.take()
    }
}