1use std::fmt::{self, Formatter};
12use std::str::FromStr;
13
14use serde::{de, ser};
15use strason::Json;
16
17#[derive(Debug, Deserialize, Serialize)]
19pub struct NetworkInfo {
20 pub version: i64,
22 pub subversion: String,
24 pub protocolversion: i64,
26 pub localservices: Option<String>,
28 pub localrelay: bool,
30 pub timeoffset: i64,
32 pub networkactive: Option<bool>,
34 pub connections: Option<i64>,
36 pub networks: Vec<Network>,
38 pub relayfee: Json,
40 pub incrementalfee: Json,
43 pub localaddresses: Vec<LocalAddress>,
45 pub warnings: String,
47}
48
49#[derive(Debug, Clone, Copy, Eq, PartialEq)]
51pub enum NetworkName {
52 Ipv4,
54 Ipv6,
56 Onion,
58}
59
60impl FromStr for NetworkName {
61 type Err = ();
62
63 fn from_str(s: &str) -> Result<Self, Self::Err> {
64 match s {
65 "ipv4" => Ok(NetworkName::Ipv4),
66 "ipv6" => Ok(NetworkName::Ipv6),
67 "onion" => Ok(NetworkName::Onion),
68 _ => Err(()),
69 }
70 }
71}
72
73impl<'de> de::Deserialize<'de> for NetworkName {
74 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
75 where
76 D: de::Deserializer<'de>,
77 {
78 struct Visitor;
79
80 impl<'de> de::Visitor<'de> for Visitor {
81 type Value = NetworkName;
82
83 fn expecting(&self, fmt: &mut Formatter) -> fmt::Result {
84 write!(fmt, "network name")
85 }
86
87 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
88 where
89 E: de::Error,
90 {
91 NetworkName::from_str(v)
92 .map_err(|_e| de::Error::custom("invalid string"))
93 }
94
95 fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
96 where
97 E: de::Error,
98 {
99 NetworkName::from_str(v)
100 .map_err(|_e| de::Error::custom("invalid string"))
101 }
102
103 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
104 where
105 E: de::Error,
106 {
107 NetworkName::from_str(&*v)
108 .map_err(|_e| de::Error::custom("invalid string"))
109 }
110 }
111
112 deserializer.deserialize_str(Visitor)
113 }
114}
115
116impl ser::Serialize for NetworkName {
117 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
118 where
119 S: ser::Serializer,
120 {
121 let s = match *self {
122 NetworkName::Ipv4 => "ipv4",
123 NetworkName::Ipv6 => "ipv6",
124 NetworkName::Onion => "onion",
125 };
126
127 serializer.serialize_str(s)
128 }
129}
130
131#[derive(Debug, Deserialize, Serialize)]
133pub struct Network {
134 pub name: NetworkName,
136 pub limited: bool,
138 pub reachable: bool,
140 pub proxy: String,
142 pub proxy_randomize_credentials: bool,
144}
145
146#[derive(Debug, Clone, Deserialize, Serialize)]
147pub struct LocalAddress {
148 pub address: String,
150 pub port: u16,
152 pub score: i64,
154}
155
156#[derive(Debug, Clone, Deserialize, Serialize)]
158pub struct PeerInfo {
159 pub id: u64,
161 pub addr: String,
164 pub addrbind: String,
167 pub addrlocal: String,
170 pub services: String,
173 pub relaytxes: bool,
175 pub lastsend: u64,
177 pub lastrecv: u64,
179 pub bytessent: u64,
181 pub bytesrecv: u64,
183 pub conntime: u64,
185 pub timeoffset: u64,
187 pub pingtime: u64,
189 pub minping: u64,
191 pub pingwait: u64,
193 pub version: u64,
195 pub subver: String,
197 pub inbound: bool,
199 pub addnode: bool,
202 pub startingheight: u64,
204 pub banscore: i64,
206 pub synced_headers: u64,
208 pub synced_blocks: u64,
210 pub inflight: Vec<u64>,
212 pub whitelisted: bool,
214 pub bytessent_per_msg: Json,
217 pub bytesrecv_per_msg: Json,
220}
221
222#[derive(Debug, Clone, Copy, Eq, PartialEq)]
224pub enum AddNode {
225 Add,
227 Remove,
229 OneTry,
231}
232
233impl FromStr for AddNode {
234 type Err = ();
235
236 fn from_str(s: &str) -> Result<Self, Self::Err> {
237 match s {
238 "add" => Ok(AddNode::Add),
239 "remove" => Ok(AddNode::Remove),
240 "onetry" => Ok(AddNode::OneTry),
241 _ => Err(()),
242 }
243 }
244}
245
246impl<'de> de::Deserialize<'de> for AddNode {
247 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
248 where
249 D: de::Deserializer<'de>,
250 {
251 struct Visitor;
252
253 impl<'de> de::Visitor<'de> for Visitor {
254 type Value = AddNode;
255
256 fn expecting(&self, fmt: &mut Formatter) -> fmt::Result {
257 write!(fmt, "network name")
258 }
259
260 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
261 where
262 E: de::Error,
263 {
264 AddNode::from_str(v)
265 .map_err(|_e| de::Error::custom("invalid string"))
266 }
267
268 fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
269 where
270 E: de::Error,
271 {
272 AddNode::from_str(v)
273 .map_err(|_e| de::Error::custom("invalid string"))
274 }
275
276 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
277 where
278 E: de::Error,
279 {
280 AddNode::from_str(&*v)
281 .map_err(|_e| de::Error::custom("invalid string"))
282 }
283 }
284
285 deserializer.deserialize_str(Visitor)
286 }
287}
288
289impl ser::Serialize for AddNode {
290 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
291 where
292 S: ser::Serializer,
293 {
294 let s = match *self {
295 AddNode::Add => "add",
296 AddNode::Remove => "remove",
297 AddNode::OneTry => "onetry",
298 };
299
300 serializer.serialize_str(s)
301 }
302}