iref_core/uri/authority/
host.rs

1use pct_str::{PctStr, PctString};
2use std::{
3	cmp,
4	hash::{self, Hash},
5	ops,
6};
7
8use static_regular_grammar::RegularGrammar;
9
10use crate::common::HostImpl;
11
12/// IRI authority host.
13#[derive(RegularGrammar)]
14#[grammar(
15	file = "src/uri/grammar.abnf",
16	entry_point = "host",
17	name = "URI host",
18	ascii,
19	no_deref,
20	cache = "automata/uri/host.aut.cbor"
21)]
22#[grammar(sized(HostBuf, derive(Debug, Display, PartialEq, Eq, PartialOrd, Ord, Hash)))]
23#[cfg_attr(feature = "serde", grammar(serde))]
24#[cfg_attr(feature = "ignore-grammars", grammar(disable))]
25pub struct Host([u8]);
26
27impl HostImpl for Host {
28	unsafe fn new_unchecked(bytes: &[u8]) -> &Self {
29		Self::new_unchecked(bytes)
30	}
31
32	fn as_bytes(&self) -> &[u8] {
33		&self.0
34	}
35}
36
37impl Host {
38	/// Returns the host as a percent-encoded string slice.
39	#[inline]
40	pub fn as_pct_str(&self) -> &PctStr {
41		unsafe { PctStr::new_unchecked(self.as_str()) }
42	}
43}
44
45impl ops::Deref for Host {
46	type Target = PctStr;
47
48	fn deref(&self) -> &Self::Target {
49		self.as_pct_str()
50	}
51}
52
53impl cmp::PartialEq for Host {
54	#[inline]
55	fn eq(&self, other: &Host) -> bool {
56		self.as_pct_str() == other.as_pct_str()
57	}
58}
59
60impl Eq for Host {}
61
62impl<'a> PartialEq<&'a str> for Host {
63	#[inline]
64	fn eq(&self, other: &&'a str) -> bool {
65		self.as_str() == *other
66	}
67}
68
69impl PartialOrd for Host {
70	#[inline]
71	fn partial_cmp(&self, other: &Host) -> Option<cmp::Ordering> {
72		Some(self.cmp(other))
73	}
74}
75
76impl Ord for Host {
77	#[inline]
78	fn cmp(&self, other: &Host) -> cmp::Ordering {
79		self.as_pct_str().cmp(other.as_pct_str())
80	}
81}
82
83impl Hash for Host {
84	#[inline]
85	fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
86		self.as_pct_str().hash(hasher)
87	}
88}
89
90impl HostBuf {
91	pub fn into_pct_string(self) -> PctString {
92		unsafe { PctString::new_unchecked(self.0) }
93	}
94}