iref 4.4.0

Uniform & Internationalized Resource Identifiers (URIs/IRIs), borrowed and owned.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#[cfg(feature = "std")]
pub use crate::PortBuf;
pub use crate::{InvalidPort, Port};

mod host;
#[cfg(feature = "std")]
mod r#mut;
mod userinfo;

pub use host::*;
#[cfg(feature = "std")]
pub use r#mut::*;
pub use userinfo::*;

/// URI authority component.
///
/// The authority component of a URI contains the host, and optionally
/// a userinfo prefix and a port suffix: `[userinfo@]host[:port]`.
///
/// # Example
///
/// ```rust
/// use iref::uri::Authority;
///
/// let authority = Authority::new("user:pass@example.org:8080").unwrap();
///
/// assert_eq!(authority.user_info().unwrap(), "user:pass");
/// assert_eq!(authority.host(), "example.org");
/// assert_eq!(authority.port().unwrap(), "8080");
/// ```
#[derive(static_automata::Validate, str_newtype::StrNewType)]
#[automaton(crate::uri::grammar::Authority)]
#[newtype(ord([u8], &[u8], str, &str))]
#[cfg_attr(
	feature = "std",
	newtype(ord(Vec<u8>, String), owned(AuthorityBuf, derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash)))
)]
#[cfg_attr(feature = "serde", newtype(serde))]
pub struct Authority(str);

impl Default for &Authority {
	fn default() -> Self {
		Authority::EMPTY
	}
}

impl Authority {
	/// The empty authority.
	pub const EMPTY: &Self = unsafe { Self::new_unchecked("") };

	/// Returns all the parts of this authority.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::Authority;
	///
	/// let authority = Authority::new("user@example.org:443").unwrap();
	/// let parts = authority.parts();
	///
	/// assert_eq!(parts.user_info.unwrap(), "user");
	/// assert_eq!(parts.host, "example.org");
	/// assert_eq!(parts.port.unwrap(), "443");
	/// ```
	pub fn parts(&self) -> AuthorityParts<'_> {
		let bytes = self.as_bytes();

		let (user_info, host) = match crate::common::parse::user_info_or_host(bytes, 0) {
			(crate::common::parse::UserInfoOrHost::UserInfo, user_info_end) => {
				let host_start = user_info_end + 1;
				let host_end = crate::common::parse::host(bytes, host_start);
				(Some(0..user_info_end), host_start..host_end)
			}
			(crate::common::parse::UserInfoOrHost::Host, host_end) => (None, 0..host_end),
		};

		let (has_port, port_end) = crate::common::parse::port(bytes, host.end);
		let port = has_port.then_some((host.end + 1)..port_end);

		AuthorityParts {
			user_info: user_info.map(|r| unsafe { UserInfo::new_unchecked(&self.0[r]) }),
			host: unsafe { Host::new_unchecked(&self.0[host]) },
			port: port.map(|r| unsafe { Port::new_unchecked_from_bytes(&self.0.as_bytes()[r]) }),
		}
	}

	/// Returns a reference to the user information, if any.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::Authority;
	///
	/// let with_userinfo = Authority::new("user:pass@example.org").unwrap();
	/// assert_eq!(with_userinfo.user_info().unwrap(), "user:pass");
	///
	/// let without = Authority::new("example.org").unwrap();
	/// assert!(without.user_info().is_none());
	/// ```
	pub fn user_info(&self) -> Option<&UserInfo> {
		let bytes = self.as_bytes();
		crate::common::parse::find_user_info(bytes, 0)
			.map(|range| unsafe { UserInfo::new_unchecked_from_bytes(&bytes[range]) })
	}

	/// Returns a reference to the host.
	///
	/// The host is always present in an authority.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::Authority;
	///
	/// let authority = Authority::new("example.org:8080").unwrap();
	/// assert_eq!(authority.host(), "example.org");
	/// ```
	pub fn host(&self) -> &Host {
		let bytes = self.as_bytes();
		let range = crate::common::parse::find_host(bytes, 0);
		unsafe { Host::new_unchecked_from_bytes(&bytes[range]) }
	}

	/// Returns a reference to the port, if any.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::Authority;
	///
	/// let with_port = Authority::new("example.org:8080").unwrap();
	/// assert_eq!(with_port.port().unwrap(), "8080");
	///
	/// let without = Authority::new("example.org").unwrap();
	/// assert!(without.port().is_none());
	/// ```
	pub fn port(&self) -> Option<&Port> {
		let bytes = self.as_bytes();
		crate::common::parse::find_port(bytes, 0)
			.map(|range| unsafe { Port::new_unchecked_from_bytes(&bytes[range]) })
	}
}

impl core::cmp::PartialEq for Authority {
	#[inline]
	fn eq(&self, other: &Authority) -> bool {
		self.parts() == other.parts()
	}
}

impl Eq for Authority {}

impl PartialOrd for Authority {
	#[inline]
	fn partial_cmp(&self, other: &Authority) -> Option<core::cmp::Ordering> {
		Some(self.cmp(other))
	}
}

impl Ord for Authority {
	#[inline]
	fn cmp(&self, other: &Authority) -> core::cmp::Ordering {
		self.parts().cmp(&other.parts())
	}
}

impl core::hash::Hash for Authority {
	#[inline]
	fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) {
		self.parts().hash(hasher)
	}
}

/// Individual components of a URI authority.
///
/// Contains references to the userinfo, host, and port components.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AuthorityParts<'a> {
	/// User information component, if present (e.g., `user:pass`).
	pub user_info: Option<&'a UserInfo>,

	/// Host component (always present).
	pub host: &'a Host,

	/// Port component, if present (e.g., `8080`).
	pub port: Option<&'a Port>,
}

#[cfg(feature = "std")]
impl AuthorityBuf {
	/// Returns a mutable reference to this authority.
	pub fn as_authority_mut(&mut self) -> AuthorityMut<'_> {
		let len = self.0.len();
		unsafe { AuthorityMut::new_unchecked(self.0.as_mut_vec(), 0..len) }
	}

	/// Replaces the entire authority value.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::{Authority, AuthorityBuf};
	///
	/// let mut authority = AuthorityBuf::new("example.org".to_string()).unwrap();
	/// authority.replace(Authority::new("other.com:8080").unwrap());
	///
	/// assert_eq!(authority, "other.com:8080");
	/// ```
	#[inline]
	pub fn replace(&mut self, other: &Authority) -> &mut Self {
		self.as_authority_mut().replace(other);
		self
	}

	/// Tries to replace the entire authority value from a string.
	///
	/// Returns an error if the string is not a valid authority.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::AuthorityBuf;
	///
	/// let mut authority = AuthorityBuf::new("example.org".to_string()).unwrap();
	/// assert!(authority.try_replace("other.com:8080").is_ok());
	/// assert_eq!(authority, "other.com:8080");
	///
	/// assert!(authority.try_replace("not valid\0").is_err());
	/// ```
	#[inline]
	pub fn try_replace<'s>(
		&mut self,
		other: &'s str,
	) -> Result<&mut Self, InvalidAuthority<&'s str>> {
		self.as_authority_mut().try_replace(other)?;
		Ok(self)
	}

	/// Sets the user info component.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::{AuthorityBuf, UserInfo};
	///
	/// let mut authority = AuthorityBuf::new("example.org".to_string()).unwrap();
	/// authority.set_user_info(Some(UserInfo::new("user:pass").unwrap()));
	///
	/// assert_eq!(authority, "user:pass@example.org");
	/// ```
	#[inline]
	pub fn set_user_info(&mut self, user_info: Option<&UserInfo>) -> &mut Self {
		self.as_authority_mut().set_user_info(user_info);
		self
	}

	/// Tries to set the user info from a string.
	///
	/// Returns an error if the string is not valid user info.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::AuthorityBuf;
	///
	/// let mut authority = AuthorityBuf::new("example.org".to_string()).unwrap();
	/// assert!(authority.try_set_user_info(Some("user:pass")).is_ok());
	/// assert_eq!(authority, "user:pass@example.org");
	///
	/// assert!(authority.try_set_user_info(Some("invalid@user")).is_err());
	/// ```
	#[inline]
	pub fn try_set_user_info<'s>(
		&mut self,
		user_info: Option<&'s str>,
	) -> Result<&mut Self, InvalidUserInfo<&'s str>> {
		self.as_authority_mut().try_set_user_info(user_info)?;
		Ok(self)
	}

	/// Sets the host component.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::{AuthorityBuf, Host};
	///
	/// let mut authority = AuthorityBuf::new("example.org:8080".to_string()).unwrap();
	/// authority.set_host(Host::new("other.com").unwrap());
	///
	/// assert_eq!(authority, "other.com:8080");
	/// ```
	#[inline]
	pub fn set_host(&mut self, host: &Host) -> &mut Self {
		self.as_authority_mut().set_host(host);
		self
	}

	/// Tries to set the host from a string.
	///
	/// Returns an error if the string is not a valid host.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::AuthorityBuf;
	///
	/// let mut authority = AuthorityBuf::new("example.org:8080".to_string()).unwrap();
	/// assert!(authority.try_set_host("other.com").is_ok());
	/// assert_eq!(authority, "other.com:8080");
	///
	/// assert!(authority.try_set_host("invalid/host").is_err());
	/// ```
	#[inline]
	pub fn try_set_host<'s>(&mut self, host: &'s str) -> Result<&mut Self, InvalidHost<&'s str>> {
		self.as_authority_mut().try_set_host(host)?;
		Ok(self)
	}

	/// Sets the port component.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::{AuthorityBuf, Port};
	///
	/// let mut authority = AuthorityBuf::new("example.org".to_string()).unwrap();
	/// authority.set_port(Some(Port::new(b"443").unwrap()));
	///
	/// assert_eq!(authority, "example.org:443");
	/// ```
	#[inline]
	pub fn set_port(&mut self, port: Option<&Port>) -> &mut Self {
		self.as_authority_mut().set_port(port);
		self
	}

	/// Sets the port from a `u32` value.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::AuthorityBuf;
	///
	/// let mut authority = AuthorityBuf::new("example.org".to_string()).unwrap();
	/// authority.set_port_u32(Some(8080));
	///
	/// assert_eq!(authority, "example.org:8080");
	/// ```
	#[inline]
	pub fn set_port_u32(&mut self, port: Option<u32>) -> &mut Self {
		self.as_authority_mut().set_port_u32(port);
		self
	}

	/// Tries to set the port from a string.
	///
	/// Returns an error if the string is not a valid port.
	///
	/// # Example
	///
	/// ```rust
	/// use iref::uri::AuthorityBuf;
	///
	/// let mut authority = AuthorityBuf::new("example.org".to_string()).unwrap();
	/// assert!(authority.try_set_port(Some("8080")).is_ok());
	/// assert_eq!(authority, "example.org:8080");
	///
	/// assert!(authority.try_set_port(Some("not_a_port")).is_err());
	/// ```
	#[inline]
	pub fn try_set_port<'s>(
		&mut self,
		port: Option<&'s str>,
	) -> Result<&mut Self, InvalidPort<&'s str>> {
		self.as_authority_mut().try_set_port(port)?;
		Ok(self)
	}
}

/// Parses a URI authority [`Authority`] at compile time.
#[macro_export]
macro_rules! authority {
	($value:literal) => {
		match $crate::uri::Authority::from_str($value) {
			Ok(value) => value,
			Err(_) => panic!("invalid URI authority"),
		}
	};
}

#[cfg(test)]
mod tests {
	use crate::Uri;

	#[test]
	fn explicit_empty_with_authority_alike_path() {
		let uri = Uri::new("scheme:////").unwrap();
		let authority = uri.authority();

		assert!(authority.unwrap().is_empty());
	}
}