amadeus_core/
misc_serde.rs1use serde::{Deserialize, Deserializer, Serialize, Serializer};
2use std::{io, sync::Arc};
3
4pub struct Serde<T>(T);
5
6impl Serialize for Serde<&io::ErrorKind> {
7 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
8 where
9 S: Serializer,
10 {
11 usize::serialize(
12 &match self.0 {
13 io::ErrorKind::NotFound => 0,
14 io::ErrorKind::PermissionDenied => 1,
15 io::ErrorKind::ConnectionRefused => 2,
16 io::ErrorKind::ConnectionReset => 3,
17 io::ErrorKind::ConnectionAborted => 4,
18 io::ErrorKind::NotConnected => 5,
19 io::ErrorKind::AddrInUse => 6,
20 io::ErrorKind::AddrNotAvailable => 7,
21 io::ErrorKind::BrokenPipe => 8,
22 io::ErrorKind::AlreadyExists => 9,
23 io::ErrorKind::WouldBlock => 10,
24 io::ErrorKind::InvalidInput => 11,
25 io::ErrorKind::InvalidData => 12,
26 io::ErrorKind::TimedOut => 13,
27 io::ErrorKind::WriteZero => 14,
28 io::ErrorKind::Interrupted => 15,
29 io::ErrorKind::UnexpectedEof => 17,
30 _ => 16,
31 },
32 serializer,
33 )
34 }
35}
36impl<'de> Deserialize<'de> for Serde<io::ErrorKind> {
37 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
38 where
39 D: Deserializer<'de>,
40 {
41 usize::deserialize(deserializer)
42 .map(|kind| match kind {
43 0 => io::ErrorKind::NotFound,
44 1 => io::ErrorKind::PermissionDenied,
45 2 => io::ErrorKind::ConnectionRefused,
46 3 => io::ErrorKind::ConnectionReset,
47 4 => io::ErrorKind::ConnectionAborted,
48 5 => io::ErrorKind::NotConnected,
49 6 => io::ErrorKind::AddrInUse,
50 7 => io::ErrorKind::AddrNotAvailable,
51 8 => io::ErrorKind::BrokenPipe,
52 9 => io::ErrorKind::AlreadyExists,
53 10 => io::ErrorKind::WouldBlock,
54 11 => io::ErrorKind::InvalidInput,
55 12 => io::ErrorKind::InvalidData,
56 13 => io::ErrorKind::TimedOut,
57 14 => io::ErrorKind::WriteZero,
58 15 => io::ErrorKind::Interrupted,
59 17 => io::ErrorKind::UnexpectedEof,
60 _ => io::ErrorKind::Other,
61 })
62 .map(Self)
63 }
64}
65
66impl Serialize for Serde<&Arc<io::Error>> {
67 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
68 where
69 S: Serializer,
70 {
71 <(Serde<&io::ErrorKind>, String)>::serialize(
72 &(Serde(&self.0.kind()), self.0.to_string()),
73 serializer,
74 )
75 }
76}
77impl<'de> Deserialize<'de> for Serde<Arc<io::Error>> {
78 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
79 where
80 D: Deserializer<'de>,
81 {
82 <(Serde<io::ErrorKind>, String)>::deserialize(deserializer)
83 .map(|(kind, message)| Arc::new(io::Error::new(kind.0, message)))
84 .map(Self)
85 }
86}
87
88impl Serialize for Serde<&io::Error> {
89 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
90 where
91 S: Serializer,
92 {
93 <(Serde<&io::ErrorKind>, String)>::serialize(
94 &(Serde(&self.0.kind()), self.0.to_string()),
95 serializer,
96 )
97 }
98}
99impl<'de> Deserialize<'de> for Serde<io::Error> {
100 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
101 where
102 D: Deserializer<'de>,
103 {
104 <(Serde<io::ErrorKind>, String)>::deserialize(deserializer)
105 .map(|(kind, message)| io::Error::new(kind.0, message))
106 .map(Self)
107 }
108}
109
110pub fn serialize<T, S>(t: &T, serializer: S) -> Result<S::Ok, S::Error>
111where
112 for<'a> Serde<&'a T>: Serialize,
113 S: Serializer,
114{
115 Serde(t).serialize(serializer)
116}
117pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
118where
119 Serde<T>: Deserialize<'de>,
120 D: Deserializer<'de>,
121{
122 Serde::<T>::deserialize(deserializer).map(|x| x.0)
123}