zvariant/type/
net.rs

1use crate::impl_type_with_repr;
2use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
3
4#[cfg(feature = "url")]
5impl_type_with_repr! {
6    url::Url => &str {
7        url_ {
8            samples = [url::Url::parse("https://example.com").unwrap()],
9            repr(url) = &url.to_string(),
10        }
11    }
12}
13
14impl_type_with_repr! {
15    Ipv4Addr => [u8; 4] {
16        ipv4_addr {
17            samples = [Ipv4Addr::LOCALHOST],
18            repr(addr) = addr.octets(),
19        }
20    }
21}
22
23impl_type_with_repr! {
24    Ipv6Addr => [u8; 16] {
25        ipv6_addr {
26            samples = [Ipv6Addr::LOCALHOST],
27            repr(addr) = addr.octets(),
28        }
29    }
30}
31
32impl_type_with_repr! {
33    IpAddr => (u32, &[u8]) {
34        ip_addr {
35            samples = [IpAddr::V4(Ipv4Addr::LOCALHOST), IpAddr::V6(Ipv6Addr::LOCALHOST)],
36            repr(addr) = match addr {
37                IpAddr::V4(v4) => (0, &v4.octets()),
38                IpAddr::V6(v6) => (1, &v6.octets()),
39            },
40        }
41    }
42}
43
44impl_type_with_repr! {
45    SocketAddrV4 => (Ipv4Addr, u16) {
46        socket_addr_v4 {
47            samples = [SocketAddrV4::new(Ipv4Addr::LOCALHOST, 8080)],
48            repr(addr) = (*addr.ip(), addr.port()),
49        }
50    }
51}
52
53impl_type_with_repr! {
54    SocketAddrV6 => (Ipv6Addr, u16) {
55        socket_addr_v6 {
56            samples = [SocketAddrV6::new(Ipv6Addr::LOCALHOST, 8080, 0, 0)],
57            // https://github.com/serde-rs/serde/blob/9b868ef831c95f50dd4bde51a7eb52e3b9ee265a/serde/src/ser/impls.rs#L966
58            repr(addr) = (*addr.ip(), addr.port()),
59        }
60    }
61}
62
63// TODO(bash): Implement DynamicType for SocketAddr