annotate src/resolv_addr.rs @ 28:cfef4593e207

Run `cargo fmt`.
author Paul Fisher <paul@pfish.zone>
date Sat, 19 Apr 2025 01:33:50 -0400
parents 66c0e10c89fc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
1 use std::fmt;
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
2 use thiserror::Error;
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
3
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
4 #[derive(Debug, PartialEq)]
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
5 pub(crate) struct ResolvAddr(String);
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
6
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
7 impl ResolvAddr {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
8 pub(crate) fn as_str(&self) -> &str {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
9 &self.0
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
10 }
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
11
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
12 pub(crate) fn try_from_generic<T: std::ops::Deref<Target = str> + Into<String>>(
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
13 string: T,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
14 ) -> Result<Self, ResolvAddrError> {
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
15 // can't use a combinator due to borrowing
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
16 let colon = match string.rfind(':') {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
17 Some(colon) => colon,
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
18 None => return Err(ResolvAddrError::MissingPort(string.into())),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
19 };
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
20
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
21 let (hostname, port) = string.split_at(colon);
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
22
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
23 if let Err(error) = port[1..].parse::<u16>() {
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
24 return Err(ResolvAddrError::InvalidPort {
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
25 string: string.into(),
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
26 error,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
27 });
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
28 }
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
29
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
30 let len = hostname.len();
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
31 if len > 253 {
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
32 return Err(ResolvAddrError::TooLong {
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
33 string: string.into(),
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
34 len,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
35 });
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
36 }
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
37
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
38 let mut label_start = 0usize;
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
39
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
40 for (i, c) in hostname.chars().enumerate() {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
41 match c {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
42 '.' => {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
43 if i - label_start == 0 {
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
44 return Err(ResolvAddrError::EmptyLabel {
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
45 string: string.into(),
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
46 label_start,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
47 });
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
48 }
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
49
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
50 label_start = i + 1;
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
51 }
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
52 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' => (),
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
53 _ => {
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
54 return Err(ResolvAddrError::InvalidCharacter {
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
55 string: string.into(),
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
56 c,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
57 pos: i,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
58 })
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
59 }
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
60 }
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
61
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
62 if i - label_start > 63 {
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
63 return Err(ResolvAddrError::LongLabel {
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
64 string: string.into(),
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
65 label_start,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
66 label_end: i,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
67 });
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
68 }
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
69 }
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
70
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
71 Ok(ResolvAddr(string.into()))
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
72 }
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
73 }
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
74
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
75 impl fmt::Display for ResolvAddr {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
76 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
77 fmt::Display::fmt(&self.0, f)
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
78 }
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
79 }
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
80
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
81 #[derive(Debug, Error)]
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
82 pub(crate) enum ResolvAddrError {
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
83 #[error("hostname {string} has {len} character which exceeds the limit of 253")]
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
84 TooLong { string: String, len: usize },
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
85 #[error("invalid character {c} in hostname {string} at position {pos}")]
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
86 InvalidCharacter { string: String, pos: usize, c: char },
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
87 #[error("hostname {string} contains a label {} at position {label_start} which is {} characters long - more than the limit 63", &string[(*label_start)..(*label_end)], label_end - label_start)]
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
88 LongLabel {
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
89 string: String,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
90 label_start: usize,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
91 label_end: usize,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
92 },
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
93 #[error("hostname {string} contains an empty label at position {label_start}")]
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
94 EmptyLabel { string: String, label_start: usize },
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
95 #[error("the address {0} is missing a port")]
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
96 MissingPort(String),
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
97 #[error("failed to parse port numer in the address {string}")]
28
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
98 InvalidPort {
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
99 string: String,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
100 error: std::num::ParseIntError,
cfef4593e207 Run `cargo fmt`.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
101 },
4
66c0e10c89fc Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff changeset
102 }