1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::core::Parser;
use crate::utils::Utils;

impl Parser {
    /// Extract the path as a vector from the url.
    ///
    /// # Example
    /// ```rust
    /// use url_parse::core::Parser;
    /// let input = "https://www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
    /// let result = Parser::new(None).path(input).unwrap();
    /// let expected = vec!["blog", "article", "search"];
    /// assert_eq!(result, expected);
    /// ```
    pub fn path<'a>(&self, input: &'a str) -> Option<Vec<&'a str>> {
        let input = Utils::substring_from_path_begin(self, input).unwrap_or("");
        let input = Utils::substring_after_port(self, input);
        let input = match input.chars().next() {
            Some('/') => &input[1..],
            _ => input,
        };
        let position_questionmark = input.find('?');
        let path_string = match position_questionmark {
            Some(v) => &input[..v],
            None => input,
        };
        return Some(path_string.split('/').collect());
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_path_works_when_partial_url() {
        let input = "https://www.example.co.uk/blog/article/search?docid=720&hl=en#dayone";
        let result = Parser::new(None).path(input).unwrap();
        let expected = vec!["blog", "article", "search"];
        assert_eq!(result, expected);
    }

    #[test]
    fn test_path_works_when_partial_url_starts_with_slash() {
        let input = "https://www.example.co.uk/blog/article/search?docid=720&hl=en#dayone";
        let result = Parser::new(None).path(input).unwrap();
        let expected = vec!["blog", "article", "search"];
        assert_eq!(result, expected);
    }

    #[test]
    fn test_path_works_when_typical() {
        let input = "https://www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
        let result = Parser::new(None).path(input).unwrap();
        let expected = vec!["blog", "article", "search"];
        assert_eq!(result, expected);
    }

    #[test]
    fn test_path_works_when_no_port() {
        let input = "https://www.example.co.uk/blog/article/search?docid=720&hl=en#dayone";
        let result = Parser::new(None).path(input).unwrap();
        let expected = vec!["blog", "article", "search"];
        assert_eq!(result, expected);
    }
}