@@ -6,43 +6,43 @@ use snafu::Snafu;
6
6
7
7
use crate :: validation;
8
8
9
- /// A validated hostname type conforming to RFC 1123, e.g. not an IPv6 address.
9
+ /// A validated domain name type conforming to RFC 1123, e.g. an IPv4, but not an IPv6 address.
10
10
#[ derive(
11
11
Serialize , Deserialize , Clone , Debug , Eq , PartialEq , Hash , Ord , PartialOrd , JsonSchema ,
12
12
) ]
13
13
#[ serde( try_from = "String" , into = "String" ) ]
14
- pub struct Hostname ( #[ validate( regex( path = "validation::RFC_1123_SUBDOMAIN_REGEX" ) ) ] String ) ;
14
+ pub struct DomainName ( #[ validate( regex( path = "validation::RFC_1123_SUBDOMAIN_REGEX" ) ) ] String ) ;
15
15
16
- impl FromStr for Hostname {
16
+ impl FromStr for DomainName {
17
17
type Err = validation:: Errors ;
18
18
19
19
fn from_str ( value : & str ) -> Result < Self , Self :: Err > {
20
20
validation:: is_rfc_1123_subdomain ( value) ?;
21
- Ok ( Hostname ( value. to_owned ( ) ) )
21
+ Ok ( DomainName ( value. to_owned ( ) ) )
22
22
}
23
23
}
24
24
25
- impl TryFrom < String > for Hostname {
25
+ impl TryFrom < String > for DomainName {
26
26
type Error = validation:: Errors ;
27
27
28
28
fn try_from ( value : String ) -> Result < Self , Self :: Error > {
29
29
value. parse ( )
30
30
}
31
31
}
32
32
33
- impl From < Hostname > for String {
34
- fn from ( value : Hostname ) -> Self {
33
+ impl From < DomainName > for String {
34
+ fn from ( value : DomainName ) -> Self {
35
35
value. 0
36
36
}
37
37
}
38
38
39
- impl Display for Hostname {
39
+ impl Display for DomainName {
40
40
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
41
41
f. write_str ( & self . 0 )
42
42
}
43
43
}
44
44
45
- impl Deref for Hostname {
45
+ impl Deref for DomainName {
46
46
type Target = str ;
47
47
48
48
fn deref ( & self ) -> & Self :: Target {
@@ -51,82 +51,82 @@ impl Deref for Hostname {
51
51
}
52
52
53
53
#[ derive( Debug , Snafu ) ]
54
- pub enum HostParseError {
54
+ pub enum HostNameParseError {
55
55
#[ snafu( display(
56
- "the given host '{host }' is not a valid host , which needs to be either a hostname or IP address"
56
+ "the given hostname '{hostname }' is not a valid hostname , which needs to be either a domain name or IP address"
57
57
) ) ]
58
- InvalidHost { host : String } ,
58
+ InvalidHostname { hostname : String } ,
59
59
}
60
60
61
- /// A validated host (either a [`Hostname `] or IP address) type.
61
+ /// A validated hostname (either a [`DomainName `] or IP address) type.
62
62
#[ derive( Serialize , Deserialize , Clone , Debug , Eq , PartialEq , Hash , Ord , PartialOrd ) ]
63
63
#[ serde( try_from = "String" , into = "String" ) ]
64
- pub enum Host {
64
+ pub enum HostName {
65
65
IpAddress ( IpAddr ) ,
66
- Hostname ( Hostname ) ,
66
+ DomainName ( DomainName ) ,
67
67
}
68
68
69
- impl JsonSchema for Host {
69
+ impl JsonSchema for HostName {
70
70
fn schema_name ( ) -> String {
71
- "Host " . to_owned ( )
71
+ "HostName " . to_owned ( )
72
72
}
73
73
74
74
fn json_schema ( gen : & mut schemars:: gen:: SchemaGenerator ) -> schemars:: schema:: Schema {
75
75
String :: json_schema ( gen)
76
76
}
77
77
}
78
78
79
- impl FromStr for Host {
80
- type Err = HostParseError ;
79
+ impl FromStr for HostName {
80
+ type Err = HostNameParseError ;
81
81
82
82
fn from_str ( value : & str ) -> Result < Self , Self :: Err > {
83
83
if let Ok ( ip) = value. parse :: < IpAddr > ( ) {
84
- return Ok ( Host :: IpAddress ( ip) ) ;
84
+ return Ok ( HostName :: IpAddress ( ip) ) ;
85
85
}
86
86
87
- if let Ok ( hostname ) = value. parse ( ) {
88
- return Ok ( Host :: Hostname ( hostname ) ) ;
87
+ if let Ok ( domain_name ) = value. parse ( ) {
88
+ return Ok ( HostName :: DomainName ( domain_name ) ) ;
89
89
} ;
90
90
91
- InvalidHostSnafu {
92
- host : value. to_owned ( ) ,
91
+ InvalidHostnameSnafu {
92
+ hostname : value. to_owned ( ) ,
93
93
}
94
94
. fail ( )
95
95
}
96
96
}
97
97
98
- impl TryFrom < String > for Host {
99
- type Error = HostParseError ;
98
+ impl TryFrom < String > for HostName {
99
+ type Error = HostNameParseError ;
100
100
101
101
fn try_from ( value : String ) -> Result < Self , Self :: Error > {
102
102
value. parse ( )
103
103
}
104
104
}
105
105
106
- impl From < Host > for String {
107
- fn from ( value : Host ) -> Self {
106
+ impl From < HostName > for String {
107
+ fn from ( value : HostName ) -> Self {
108
108
value. to_string ( )
109
109
}
110
110
}
111
111
112
- impl Display for Host {
112
+ impl Display for HostName {
113
113
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
114
114
match self {
115
- Host :: IpAddress ( ip) => write ! ( f, "{ip}" ) ,
116
- Host :: Hostname ( hostname ) => write ! ( f, "{hostname }" ) ,
115
+ HostName :: IpAddress ( ip) => write ! ( f, "{ip}" ) ,
116
+ HostName :: DomainName ( domain_name ) => write ! ( f, "{domain_name }" ) ,
117
117
}
118
118
}
119
119
}
120
120
121
- impl Host {
121
+ impl HostName {
122
122
/// Formats the host in such a way that it can be used in URLs.
123
123
pub fn as_url_host ( & self ) -> String {
124
124
match self {
125
- Host :: IpAddress ( ip) => match ip {
125
+ HostName :: IpAddress ( ip) => match ip {
126
126
IpAddr :: V4 ( ip) => ip. to_string ( ) ,
127
127
IpAddr :: V6 ( ip) => format ! ( "[{ip}]" ) ,
128
128
} ,
129
- Host :: Hostname ( hostname ) => hostname . to_string ( ) ,
129
+ HostName :: DomainName ( domain_name ) => domain_name . to_string ( ) ,
130
130
}
131
131
}
132
132
}
@@ -176,37 +176,38 @@ mod tests {
176
176
#[ rstest]
177
177
#[ case( "foo" ) ]
178
178
#[ case( "foo.bar" ) ]
179
- // Well this is also a valid hostname I guess
179
+ // This is also a valid domain name
180
180
#[ case( "1.2.3.4" ) ]
181
- fn test_host_and_hostname_parsing_success ( #[ case] hostname : String ) {
182
- let parsed_hostname: Hostname = hostname. parse ( ) . expect ( "hostname can not be parsed" ) ;
183
- // Every hostname is also a valid host
184
- let parsed_host: Host = hostname. parse ( ) . expect ( "host can not be parsed" ) ;
181
+ fn test_domain_name_and_host_name_parsing_success ( #[ case] domain_name : String ) {
182
+ let parsed_domain_name: DomainName =
183
+ domain_name. parse ( ) . expect ( "domain name can not be parsed" ) ;
184
+ // Every domain name is also a valid host name
185
+ let parsed_host_name: HostName = domain_name. parse ( ) . expect ( "host name can not be parsed" ) ;
185
186
186
187
// Also test the round-trip
187
- assert_eq ! ( parsed_hostname . to_string( ) , hostname ) ;
188
- assert_eq ! ( parsed_host . to_string( ) , hostname ) ;
188
+ assert_eq ! ( parsed_domain_name . to_string( ) , domain_name ) ;
189
+ assert_eq ! ( parsed_host_name . to_string( ) , domain_name ) ;
189
190
}
190
191
191
192
#[ rstest]
192
193
#[ case( "" ) ]
193
194
#[ case( "foo.bar:1234" ) ]
194
195
#[ case( "fe80::1" ) ]
195
- fn test_hostname_parsing_invalid_input ( #[ case] hostname : & str ) {
196
- assert ! ( hostname . parse:: <Hostname >( ) . is_err( ) ) ;
196
+ fn test_domain_name_parsing_invalid_input ( #[ case] domain_name : & str ) {
197
+ assert ! ( domain_name . parse:: <DomainName >( ) . is_err( ) ) ;
197
198
}
198
199
199
200
#[ rstest]
200
201
#[ case( "foo" , "foo" ) ]
201
202
#[ case( "foo.bar" , "foo.bar" ) ]
202
203
#[ case( "1.2.3.4" , "1.2.3.4" ) ]
203
204
#[ case( "fe80::1" , "[fe80::1]" ) ]
204
- fn test_host_parsing_success ( #[ case] host : & str , #[ case] expected_url_host : & str ) {
205
- let parsed_host : Host = host. parse ( ) . expect ( "host can not be parsed" ) ;
205
+ fn test_host_name_parsing_success ( #[ case] host : & str , #[ case] expected_url_host : & str ) {
206
+ let parsed_host_name : HostName = host. parse ( ) . expect ( "host can not be parsed" ) ;
206
207
207
208
// Also test the round-trip
208
- assert_eq ! ( parsed_host . to_string( ) , host) ;
209
+ assert_eq ! ( parsed_host_name . to_string( ) , host) ;
209
210
210
- assert_eq ! ( parsed_host . as_url_host( ) , expected_url_host) ;
211
+ assert_eq ! ( parsed_host_name . as_url_host( ) , expected_url_host) ;
211
212
}
212
213
}
0 commit comments