1use core::fmt::Display;
2use core::str::FromStr;
3
4#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
5#[repr(transparent)]
6pub struct Ipv6([u8; 16]); impl Ipv6 {
9 pub const fn new(value: [u16; 8]) -> Self {
10 let mut bytes = [0; 16];
11 let mut i = 0;
12 while i < 8 {
13 let segment = value[i].to_be_bytes();
14 bytes[i * 2] = segment[0];
15 bytes[i * 2 + 1] = segment[1];
16 i += 1;
17 }
18 Self(bytes)
19 }
20
21 pub const fn into_inner(self) -> [u8; 16] {
22 self.0
23 }
24
25 pub const fn from_inner(value: [u8; 16]) -> Self {
26 Self(value)
27 }
28
29 pub const fn into_smoltcp(self) -> core::net::Ipv6Addr {
30 core::net::Ipv6Addr::from_octets(self.0)
31 }
32
33 pub const fn from_smoltcp(value: &core::net::Ipv6Addr) -> Self {
34 Self(value.octets())
35 }
36}
37
38impl TryFrom<&str> for Ipv6 {
39 type Error = ();
40
41 fn try_from(value: &str) -> Result<Self, Self::Error> {
42 let address = core::net::Ipv6Addr::from_str(value).map_err(|_| ())?;
46 Ok(Self(address.octets()))
47 }
48}
49
50impl Display for Ipv6 {
51 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
55 Display::fmt(&self.into_smoltcp(), f)
56 }
57}
58
59#[cfg(test)]
60mod tests {
61 use alloc::string::ToString;
62
63 use super::*;
64
65 #[test]
66 fn test_ipv6_display() {
67 let ip = Ipv6::new([0; 8]);
68
69 assert_eq!(ip.to_string(), "::");
70 }
71
72 #[test]
73 fn display_compresses_the_longest_zero_run() {
74 let ip = Ipv6::new([0x2606, 0x4700, 0x10, 0, 0, 0, 0x6814, 0x179a]);
75
76 assert_eq!(ip.to_string(), "2606:4700:10::6814:179a");
77 assert_eq!(Ipv6::try_from(ip.to_string().as_str()).unwrap(), ip);
78 }
79
80 #[test]
81 fn display_compresses_the_first_run_on_a_tie() {
82 let ip = Ipv6::new([0, 0, 1, 0, 0, 2, 3, 4]);
83
84 assert_eq!(ip.to_string(), "::1:0:0:2:3:4");
85 }
86
87 #[test]
88 fn display_uses_lowercase_hexadecimal() {
89 let ip = Ipv6::new([0xabcd, 0xef01, 0, 0, 0, 0, 0, 1]);
90
91 assert_eq!(ip.to_string(), "abcd:ef01::1");
92 }
93
94 #[test]
95 fn display_prints_ipv4_mapped_addresses_in_mixed_notation() {
96 let ip = Ipv6::try_from("::ffff:192.0.2.1").unwrap();
97
98 assert_eq!(ip.to_string(), "::ffff:192.0.2.1");
99 }
100
101 #[test]
102 fn test_ipv6_try_from() {
103 let ip = Ipv6::try_from("0:0:0:0:0:0:0:0").unwrap();
104
105 assert_eq!(ip.0, [0; 16]);
106
107 Ipv6::try_from("0:0:0:0:0:0:0:0:0").unwrap_err();
108
109 Ipv6::try_from("0:0:0:0:0:0:0").unwrap_err();
110
111 let ip = Ipv6::try_from("1234:5678:9abc:def0:1234:5678:9abc:def0").unwrap();
112
113 assert_eq!(
114 ip.0,
115 [
116 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc,
117 0xde, 0xf0
118 ]
119 );
120 }
121
122 #[test]
123 fn test_ipv6_try_from_compressed_form() {
124 let ip = Ipv6::try_from("2001:db8::1").unwrap();
125
126 assert_eq!(ip, Ipv6::new([0x2001, 0x0db8, 0, 0, 0, 0, 0, 1]));
127
128 assert_eq!(
129 Ipv6::try_from("::1").unwrap(),
130 Ipv6::new([0, 0, 0, 0, 0, 0, 0, 1])
131 );
132
133 assert_eq!(Ipv6::try_from("::").unwrap(), Ipv6::new([0; 8]));
134
135 assert_eq!(
136 Ipv6::try_from("2001:db8::1:0:0:1").unwrap(),
137 Ipv6::try_from("2001:db8:0:0:1:0:0:1").unwrap()
138 );
139 }
140
141 #[test]
142 fn test_ipv6_try_from_ipv4_mapped() {
143 let ip = Ipv6::try_from("::ffff:192.0.2.1").unwrap();
144
145 assert_eq!(ip, Ipv6::new([0, 0, 0, 0, 0, 0xffff, 0xc000, 0x0201]));
146 assert_eq!(Ipv6::try_from(ip.to_string().as_str()).unwrap(), ip);
147 }
148
149 #[test]
150 fn test_ipv6_try_from_invalid() {
151 for input in [
152 "",
153 "0:0:0:0:0:0:0:0:0",
154 "0:0:0:0:0:0:0",
155 "::1::",
156 "2001:db8:::1",
157 "12345::",
158 "gg::1",
159 "192.0.2.1",
160 "-1::",
161 " 2001:db8::1",
162 ] {
163 Ipv6::try_from(input).unwrap_err();
164 }
165 }
166
167 #[test]
168 fn display_round_trips_through_try_from() {
169 for input in [
170 "::",
171 "::1",
172 "2001:db8::1",
173 "2606:4700:10::6814:179a",
174 "::ffff:192.0.2.1",
175 "1234:5678:9abc:def0:1234:5678:9abc:def0",
176 ] {
177 let ip = Ipv6::try_from(input).unwrap();
178
179 assert_eq!(Ipv6::try_from(ip.to_string().as_str()).unwrap(), ip);
180 }
181 }
182}