| •1 |
use crate::addr::{SocketAddr, SocketAddrV4, SocketAddrV6}; |
| 2 |
use core::{fmt, str}; |
| •3 |
use crate::ip::{IpAddr, Ipv4Addr, Ipv6Addr}; |
| 4 |
use serde::de::{Deserialize, Deserializer, EnumAccess, Error, Unexpected, VariantAccess, Visitor}; |
| 5 |
|
| 6 |
macro_rules! variant_identifier { |
| 7 |
( |
| 8 |
$name_kind: ident ( $($variant: ident; $bytes: expr; $index: expr),* ) |
| 9 |
$expecting_message: expr, |
| 10 |
$variants_name: ident |
| 11 |
) => { |
| 12 |
enum $name_kind { |
| 13 |
$( $variant ),* |
| 14 |
} |
| 15 |
|
| 16 |
static $variants_name: &'static [&'static str] = &[ $( stringify!($variant) ),*]; |
| 17 |
|
| 18 |
impl<'de> Deserialize<'de> for $name_kind { |
| 19 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 20 |
where |
| 21 |
D: Deserializer<'de>, |
| 22 |
{ |
| 23 |
struct KindVisitor; |
| 24 |
|
| 25 |
impl<'de> Visitor<'de> for KindVisitor { |
| 26 |
type Value = $name_kind; |
| 27 |
|
| 28 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 29 |
formatter.write_str($expecting_message) |
| 30 |
} |
| 31 |
|
| 32 |
fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E> |
| 33 |
where |
| 34 |
E: Error, |
| 35 |
{ |
| 36 |
match value { |
| 37 |
$( |
| 38 |
$index => Ok($name_kind :: $variant), |
| 39 |
)* |
| 40 |
_ => Err(Error::invalid_value(Unexpected::Unsigned(value as u64), &self),), |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| 45 |
where |
| 46 |
E: Error, |
| 47 |
{ |
| 48 |
match value { |
| 49 |
$( |
| 50 |
stringify!($variant) => Ok($name_kind :: $variant), |
| 51 |
)* |
| 52 |
_ => Err(Error::unknown_variant(value, $variants_name)), |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> |
| 57 |
where |
| 58 |
E: Error, |
| 59 |
{ |
| 60 |
match value { |
| 61 |
$( |
| 62 |
$bytes => Ok($name_kind :: $variant), |
| 63 |
)* |
| 64 |
_ => { |
| 65 |
match str::from_utf8(value) { |
| 66 |
Ok(value) => Err(Error::unknown_variant(value, $variants_name)), |
| 67 |
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(value), &self)), |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
deserializer.deserialize_identifier(KindVisitor) |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
macro_rules! deserialize_enum { |
| 81 |
( |
| 82 |
$name: ident $name_kind: ident ( $($variant: ident; $bytes: expr; $index: expr),* ) |
| 83 |
$expecting_message: expr, |
| 84 |
$deserializer: expr |
| 85 |
) => { |
| 86 |
variant_identifier!{ |
| 87 |
$name_kind ( $($variant; $bytes; $index),* ) |
| 88 |
$expecting_message, |
| 89 |
VARIANTS |
| 90 |
} |
| 91 |
|
| 92 |
struct EnumVisitor; |
| 93 |
impl<'de> Visitor<'de> for EnumVisitor { |
| 94 |
type Value = $name; |
| 95 |
|
| 96 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 97 |
formatter.write_str(concat!("a ", stringify!($name))) |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> |
| 102 |
where |
| 103 |
A: EnumAccess<'de>, |
| 104 |
{ |
| 105 |
match data.variant()? { |
| 106 |
$( |
| 107 |
($name_kind :: $variant, v) => v.newtype_variant().map($name :: $variant), |
| 108 |
)* |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
$deserializer.deserialize_enum(stringify!($name), VARIANTS, EnumVisitor) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
macro_rules! parse_ip_impl { |
| 117 |
($expecting:tt $ty:ty; $size:tt) => { |
| 118 |
impl<'de> Deserialize<'de> for $ty { |
| 119 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 120 |
where |
| 121 |
D: Deserializer<'de>, |
| 122 |
{ |
| 123 |
if deserializer.is_human_readable() { |
| 124 |
struct IpAddrVisitor; |
| 125 |
|
| 126 |
impl<'de> Visitor<'de> for IpAddrVisitor { |
| 127 |
type Value = $ty; |
| 128 |
|
| 129 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 130 |
formatter.write_str($expecting) |
| 131 |
} |
| 132 |
|
| 133 |
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> |
| 134 |
where |
| 135 |
E: Error, |
| 136 |
{ |
| 137 |
s.parse().map_err(Error::custom) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
deserializer.deserialize_str(IpAddrVisitor) |
| 142 |
} else { |
| 143 |
<[u8; $size]>::deserialize(deserializer).map(<$ty>::from) |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
}; |
| 148 |
} |
| 149 |
|
| 150 |
impl<'de> Deserialize<'de> for IpAddr { |
| 151 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 152 |
where |
| 153 |
D: Deserializer<'de>, |
| 154 |
{ |
| 155 |
if deserializer.is_human_readable() { |
| 156 |
struct IpAddrVisitor; |
| 157 |
|
| 158 |
impl<'de> Visitor<'de> for IpAddrVisitor { |
| 159 |
type Value = IpAddr; |
| 160 |
|
| 161 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 162 |
formatter.write_str("IP address") |
| 163 |
} |
| 164 |
|
| 165 |
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> |
| 166 |
where |
| 167 |
E: Error, |
| 168 |
{ |
| 169 |
s.parse().map_err(Error::custom) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
deserializer.deserialize_str(IpAddrVisitor) |
| 174 |
} else { |
| 175 |
deserialize_enum! { |
| 176 |
IpAddr IpAddrKind (V4; b"V4"; 0, V6; b"V6"; 1) |
| 177 |
"`V4` or `V6`", |
| 178 |
deserializer |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
parse_ip_impl!("IPv4 address" Ipv4Addr; 4); |
| 185 |
parse_ip_impl!("IPv6 address" Ipv6Addr; 16); |
| 186 |
|
| 187 |
macro_rules! parse_socket_impl { |
| 188 |
($expecting:tt $ty:ty, $new:expr) => { |
| 189 |
impl<'de> Deserialize<'de> for $ty { |
| 190 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 191 |
where |
| 192 |
D: Deserializer<'de>, |
| 193 |
{ |
| 194 |
if deserializer.is_human_readable() { |
| 195 |
struct SocketAddrVisitor; |
| 196 |
|
| 197 |
impl<'de> Visitor<'de> for SocketAddrVisitor { |
| 198 |
type Value = $ty; |
| 199 |
|
| 200 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 201 |
formatter.write_str($expecting) |
| 202 |
} |
| 203 |
|
| 204 |
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> |
| 205 |
where |
| 206 |
E: Error, |
| 207 |
{ |
| 208 |
s.parse().map_err(Error::custom) |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
deserializer.deserialize_str(SocketAddrVisitor) |
| 213 |
} else { |
| 214 |
<(_, u16)>::deserialize(deserializer).map(|(ip, port)| $new(ip, port)) |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
}; |
| 219 |
} |
| 220 |
|
| 221 |
impl<'de> Deserialize<'de> for SocketAddr { |
| 222 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 223 |
where |
| 224 |
D: Deserializer<'de>, |
| 225 |
{ |
| 226 |
if deserializer.is_human_readable() { |
| 227 |
struct SocketAddrVisitor; |
| 228 |
|
| 229 |
impl<'de> Visitor<'de> for SocketAddrVisitor { |
| 230 |
type Value = SocketAddr; |
| 231 |
|
| 232 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 233 |
formatter.write_str("socket address") |
| 234 |
} |
| 235 |
|
| 236 |
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> |
| 237 |
where |
| 238 |
E: Error, |
| 239 |
{ |
| 240 |
s.parse().map_err(Error::custom) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
deserializer.deserialize_str(SocketAddrVisitor) |
| 245 |
} else { |
| 246 |
deserialize_enum! { |
| 247 |
SocketAddr SocketAddrKind (V4; b"V4"; 0, V6; b"V6"; 1) |
| 248 |
"`V4` or `V6`", |
| 249 |
deserializer |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
parse_socket_impl!("IPv4 socket address" SocketAddrV4, SocketAddrV4::new); |
| 256 |
parse_socket_impl!("IPv6 socket address" SocketAddrV6, |ip, port| SocketAddrV6::new( |
| 257 |
ip, port, 0, 0 |
| 258 |
)); |
| 259 |
|
| 260 |
|