Skip to main content

iri_string/components/
authority.rs

1//! Subcomponents of authority.
2
3use crate::parser::trusted as trusted_parser;
4use crate::spec::Spec;
5use crate::types::RiReferenceStr;
6
7/// Subcomponents of authority.
8///
9/// This is a return type of the `authority_components` method of the string
10/// types (for example [`RiStr::authority_components`].
11///
12/// [`RiStr::authority_components`]: `crate::types::RiStr::authority_components`
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub struct AuthorityComponents<'a> {
15    /// Authority string, excluding the leading `//`.
16    pub(crate) authority: &'a str,
17    /// Start position of the `host`.
18    pub(crate) host_start: usize,
19    /// End position of the `host`.
20    pub(crate) host_end: usize,
21}
22
23impl<'a> AuthorityComponents<'a> {
24    /// Creates a new `AuthorityComponents` from the IRI.
25    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    /// Creates a new `AuthorityComponents` from the IRI, and return it with authority offset.
31    ///
32    /// # Precondition
33    ///
34    /// The parameter `iri` must be a valid IRI reference. If the condition is
35    /// not met, this function may panic or return a wrong result (but won't
36    /// cause undefined behavior).
37    #[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    /// Returns the `userinfo` part, excluding the following `@`.
46    #[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    /// Returns the `host` part.
54    #[inline]
55    #[must_use]
56    pub fn host(&self) -> &'a str {
57        // NOTE: RFC 6874 support may need the internal logic to change.
58        &self.authority[self.host_start..self.host_end]
59    }
60
61    /// Returns the `host` part if it matches `reg-name` (i.e., looks like a domain name).
62    #[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    /// Returns the `port` part, excluding the leading `:`.
74    #[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}