Skip to main content

web_url/url/
path.rs

1use crate::{Path, WebUrl};
2
3impl WebUrl {
4    //! Path
5
6    /// Gets the path.
7    pub fn path(&self) -> Path<'_> {
8        unsafe { Path::new(self.path_str()) }
9    }
10
11    /// Gets the path string.
12    ///
13    /// This will be a valid path starting with a '/'.
14    fn path_str(&self) -> &str {
15        let start: usize = self.port_end as usize;
16        let end: usize = self.path_end as usize;
17        &self.url[start..end]
18    }
19}
20
21#[cfg(test)]
22mod tests {
23    use crate::WebUrl;
24    use std::error::Error;
25    use std::str::FromStr;
26
27    #[test]
28    fn path_explicit() -> Result<(), Box<dyn Error>> {
29        let url = WebUrl::from_str("https://example.com/the/path")?;
30        assert_eq!(url.path().as_str(), "/the/path");
31        Ok(())
32    }
33
34    #[test]
35    fn path_default() -> Result<(), Box<dyn Error>> {
36        let url = WebUrl::from_str("https://example.com")?;
37        assert_eq!(url.path().as_str(), "/");
38        Ok(())
39    }
40}