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    /// - `&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(url_str: &str) -> Result<Self, HttpUrlError> {
16        let parsed_url: UrlParser =
17            UrlParser::parse(url_str).map_err(|_| HttpUrlError::InvalidUrl)?;
18        let res: Self = Self {
19            protocol: parsed_url
20                .scheme()
21                .to_string()
22                .parse::<Protocol>()
23                .unwrap_or_default(),
24            host: parsed_url.host_str().map(|h| h.to_string()),
25            port: parsed_url.port(),
26            path: Some(parsed_url.path().to_string()),
27            query: parsed_url.query().map(|q| q.to_string()),
28            fragment: parsed_url.fragment().map(|f| f.to_string()),
29        };
30        Ok(res)
31    }
32}