1use crate::events::NodeInfo;
2use crate::value::Value;
3use crate::OID;
4use rand::seq::SliceRandom;
5use rand::thread_rng;
6use serde::{Deserialize, Deserializer, Serialize};
7use std::sync::atomic;
8use std::sync::Arc;
9use std::time::Duration;
10use uuid::Uuid;
11
12pub fn deserialize_uuid<'de, D>(deserializer: D) -> Result<Uuid, D::Error>
13where
14 D: Deserializer<'de>,
15{
16 let val: Value = Deserialize::deserialize(deserializer)?;
17 Uuid::deserialize(val).map_err(serde::de::Error::custom)
18}
19
20#[derive(Serialize, Deserialize, Debug, Clone)]
21pub struct NodeData {
22 svc: Option<String>,
23 #[serde(
24 deserialize_with = "crate::tools::deserialize_arc_atomic_bool",
25 serialize_with = "crate::tools::serialize_atomic_bool"
26 )]
27 online: Arc<atomic::AtomicBool>,
28 #[serde(skip_serializing_if = "Option::is_none")]
29 info: Option<NodeInfo>,
30 #[serde(
31 default,
32 serialize_with = "crate::tools::serialize_opt_duration_as_f64",
33 deserialize_with = "crate::tools::de_opt_float_as_duration"
34 )]
35 timeout: Option<Duration>,
36}
37
38impl NodeData {
39 #[inline]
40 pub fn new(
41 svc: Option<&str>,
42 online: bool,
43 info: Option<NodeInfo>,
44 timeout: Option<Duration>,
45 ) -> Self {
46 Self {
47 svc: svc.map(ToOwned::to_owned),
48 online: Arc::new(atomic::AtomicBool::new(online)),
49 info,
50 timeout,
51 }
52 }
53 #[inline]
54 pub fn svc(&self) -> Option<&str> {
55 self.svc.as_deref()
56 }
57 #[inline]
58 pub fn online(&self) -> bool {
59 self.online.load(atomic::Ordering::SeqCst)
60 }
61 #[inline]
62 pub fn online_beacon(&self) -> Arc<atomic::AtomicBool> {
63 self.online.clone()
64 }
65 #[inline]
66 pub fn info(&self) -> Option<&NodeInfo> {
67 self.info.as_ref()
68 }
69 #[inline]
70 pub fn timeout(&self) -> Option<Duration> {
71 self.timeout
72 }
73 #[inline]
74 pub fn set_online(&self, online: bool) {
75 self.online.store(online, atomic::Ordering::SeqCst);
76 }
77 #[inline]
78 pub fn update_info(&mut self, info: NodeInfo) {
79 self.info.replace(info);
80 }
81 #[inline]
82 pub fn update_timeout(&mut self, timeout: Option<Duration>) {
83 self.timeout = timeout;
84 }
85}
86
87#[derive(Serialize, Deserialize, Debug, Clone)]
88pub struct ParamsId<'a> {
89 #[serde(borrow)]
90 pub i: &'a str,
91}
92
93#[derive(Serialize, Deserialize, Debug, Clone)]
94pub struct ParamsIdOwned {
95 pub i: String,
96}
97
98pub type IdOrList<'a> = ValueOrList<&'a str>;
99
100pub type IdOrListOwned = ValueOrList<String>;
101
102#[derive(Serialize, Deserialize, Debug, Clone)]
103pub struct ParamsIdOrList<'a> {
104 #[serde(borrow)]
105 pub i: IdOrList<'a>,
106}
107
108#[derive(Serialize, Deserialize, Debug, Clone)]
109pub struct ParamsIdOrListOwned {
110 pub i: IdOrListOwned,
111}
112
113#[derive(Serialize, Deserialize, Debug, Clone)]
114pub struct ParamsIdList<'a> {
115 #[serde(borrow)]
116 pub i: Vec<&'a str>,
117}
118
119#[derive(Serialize, Deserialize, Debug, Clone, Default)]
120pub struct ParamsIdListOwned {
121 pub i: Vec<String>,
122}
123
124#[derive(Serialize, Deserialize, Debug, Clone)]
125pub struct ParamsOID {
126 pub i: OID,
127}
128
129#[derive(Serialize, Deserialize, Debug, Clone)]
130pub struct ParamsUuid {
131 pub u: Uuid,
132}
133
134#[derive(Serialize, Deserialize, Debug, Clone)]
135pub struct ParamsUuidAny {
136 #[serde(deserialize_with = "deserialize_uuid")]
137 pub u: Uuid,
138}
139
140#[derive(Serialize, Deserialize, Debug, Clone)]
141#[serde(untagged)]
142pub enum ValueOrList<T>
143where
144 T: Send + Sync + Clone,
145{
146 Single(T),
147 Multiple(Vec<T>),
148}
149
150impl<T> Default for ValueOrList<T>
151where
152 T: Send + Sync + Clone,
153{
154 #[inline]
155 fn default() -> Self {
156 ValueOrList::Multiple(Vec::new())
157 }
158}
159
160impl<T> ValueOrList<T>
161where
162 T: Send + Sync + Clone + PartialEq,
163{
164 pub fn is_empty(&self) -> bool {
165 match self {
166 ValueOrList::Single(_) => false,
167 ValueOrList::Multiple(v) => v.is_empty(),
168 }
169 }
170 pub fn len(&self) -> usize {
171 match self {
172 ValueOrList::Single(_) => 1,
173 ValueOrList::Multiple(v) => v.len(),
174 }
175 }
176 pub fn shuffle(&mut self) {
177 if let ValueOrList::Multiple(ref mut v) = self {
178 v.shuffle(&mut thread_rng());
179 }
180 }
181 #[inline]
182 pub fn iter(&self) -> impl Iterator<Item = &T> + '_ {
183 self.into_iter()
184 }
185 pub fn to_vec(&self) -> Vec<T> {
186 match self {
187 ValueOrList::Single(v) => vec![v.clone()],
188 ValueOrList::Multiple(v) => v.clone(),
189 }
190 }
191 pub fn into_vec(self) -> Vec<T> {
192 match self {
193 ValueOrList::Single(v) => vec![v],
194 ValueOrList::Multiple(v) => v,
195 }
196 }
197 pub fn contains(&self, value: &T) -> bool {
198 match self {
199 ValueOrList::Single(v) => v == value,
200 ValueOrList::Multiple(v) => v.contains(value),
201 }
202 }
203}
204
205impl<T: Send + Sync + Clone + 'static> IntoIterator for ValueOrList<T> {
206 type Item = T;
207 type IntoIter = Box<dyn Iterator<Item = Self::Item> + Send + Sync + 'static>;
208
209 fn into_iter(self) -> Self::IntoIter {
210 match self {
211 ValueOrList::Single(s) => Box::new(SingleIter(Some(s))),
212 ValueOrList::Multiple(vals) => Box::new(vals.into_iter()),
213 }
214 }
215}
216
217impl<'a, T: Send + Sync + Clone> IntoIterator for &'a ValueOrList<T> {
218 type Item = &'a T;
219 type IntoIter = Box<dyn Iterator<Item = Self::Item> + Send + Sync + 'a>;
220
221 fn into_iter(self) -> Self::IntoIter {
222 match self {
223 ValueOrList::Single(s) => Box::new(SingleIter(Some(s))),
224 ValueOrList::Multiple(vals) => Box::new(vals.iter()),
225 }
226 }
227}
228
229struct SingleIter<T>(Option<T>);
230
231impl<T> Iterator for SingleIter<T> {
232 type Item = T;
233 #[inline]
234 fn next(&mut self) -> Option<Self::Item> {
235 self.0.take()
236 }
237}