http_type/http_url/
impl.rs

1use crate::*;
2
3impl HttpUrlComponents {
4    /// Parses a URL string into its components.
5    ///
6    /// Extracts protocol, host, port, path, query and fragment from the URL string.
7    ///
8    /// # Arguments
9    ///
10    /// - `AsRef<str>` - The URL string to parse.
11    ///
12    /// # Returns
13    ///
14    /// - `Result<HttpUrlComponents, HttpUrlError>` - Either the parsed components or an error.
15    pub fn parse<U>(url_str: U) -> Result<Self, HttpUrlError>
16    where
17        U: AsRef<str>,
18    {
19        let parsed_url: UrlParser =
20            UrlParser::parse(url_str.as_ref()).map_err(|_| HttpUrlError::InvalidUrl)?;
21        let res: Self = Self {
22            protocol: parsed_url
23                .scheme()
24                .to_string()
25                .parse::<Protocol>()
26                .unwrap_or_default(),
27            host: parsed_url.host_str().map(|h| h.to_string()),
28            port: parsed_url.port(),
29            path: Some(parsed_url.path().to_string()),
30            query: parsed_url.query().map(|q| q.to_string()),
31            fragment: parsed_url.fragment().map(|f| f.to_string()),
32        };
33        Ok(res)
34    }
35}