iri_string/components/
authority.rs1use crate::parser::trusted as trusted_parser;
4use crate::spec::Spec;
5use crate::types::RiReferenceStr;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub struct AuthorityComponents<'a> {
15 pub(crate) authority: &'a str,
17 pub(crate) host_start: usize,
19 pub(crate) host_end: usize,
21}
22
23impl<'a> AuthorityComponents<'a> {
24 pub fn from_iri<S: Spec>(iri: &'a RiReferenceStr<S>) -> Option<Self> {
26 iri.authority_str()
27 .map(trusted_parser::authority::decompose_authority)
28 }
29
30 #[cfg(feature = "alloc")]
38 #[must_use]
39 pub(crate) fn from_iri_get_offset(iri: &'a str) -> Option<(Self, usize)> {
40 let (authority_str, offset) = trusted_parser::extract_authority_and_offset(iri)?;
41 let components = trusted_parser::authority::decompose_authority(authority_str);
42 Some((components, offset))
43 }
44
45 #[must_use]
47 pub fn userinfo(&self) -> Option<&'a str> {
48 let userinfo_at = self.host_start.checked_sub(1)?;
49 debug_assert_eq!(self.authority.as_bytes()[userinfo_at], b'@');
50 Some(&self.authority[..userinfo_at])
51 }
52
53 #[inline]
55 #[must_use]
56 pub fn host(&self) -> &'a str {
57 &self.authority[self.host_start..self.host_end]
59 }
60
61 #[inline]
63 #[must_use]
64 pub fn reg_name(&self) -> Option<&'a str> {
65 let host = self.host();
66 if trusted_parser::authority::is_host_reg_name(host) {
67 Some(host)
68 } else {
69 None
70 }
71 }
72
73 #[must_use]
75 pub fn port(&self) -> Option<&'a str> {
76 if self.host_end == self.authority.len() {
77 return None;
78 }
79 let port_colon = self.host_end;
80 debug_assert_eq!(self.authority.as_bytes()[port_colon], b':');
81 Some(&self.authority[(port_colon + 1)..])
82 }
83}
84
85#[cfg(test)]
86#[cfg(feature = "alloc")]
87mod tests {
88 use super::*;
89
90 #[cfg(all(feature = "alloc", not(feature = "std")))]
91 use alloc::string::String;
92
93 use crate::types::IriReferenceStr;
94
95 const USERINFO: &[&str] = &["", "user:password", "user"];
96
97 const PORT: &[&str] = &[
98 "",
99 "0",
100 "0000",
101 "80",
102 "1234567890123456789012345678901234567890",
103 ];
104
105 const HOST: &[&str] = &[
106 "",
107 "localhost",
108 "example.com",
109 "192.0.2.0",
110 "[2001:db8::1]",
111 "[2001:0db8:0:0:0:0:0:1]",
112 "[2001:0db8::192.0.2.255]",
113 "[v9999.this-is-futuristic-ip-address]",
114 ];
115
116 fn compose_to_relative_iri(userinfo: Option<&str>, host: &str, port: Option<&str>) -> String {
117 let mut buf = String::from("//");
118 if let Some(userinfo) = userinfo {
119 buf.push_str(userinfo);
120 buf.push('@');
121 }
122 buf.push_str(host);
123 if let Some(port) = port {
124 buf.push(':');
125 buf.push_str(port);
126 }
127 buf
128 }
129
130 #[test]
131 fn test_decompose_authority() {
132 for host in HOST.iter().copied() {
133 for userinfo in USERINFO.iter().map(|s| Some(*s)).chain(None) {
134 for port in PORT.iter().map(|s| Some(*s)).chain(None) {
135 let authority = compose_to_relative_iri(userinfo, host, port);
136 let authority =
137 IriReferenceStr::new(&authority).expect("test case should be valid");
138 let components = AuthorityComponents::from_iri(authority)
139 .expect("relative path composed for this test should contain authority");
140
141 assert_eq!(components.host(), host);
142 assert_eq!(components.userinfo(), userinfo);
143 assert_eq!(components.port(), port);
144 }
145 }
146 }
147 }
148}