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
use crate::core::Parser;

impl Parser {
    /// Extract the query 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).query(input).unwrap();
    /// assert_eq!(result, "docid=720&hl=en");
    /// ```
    pub fn query<'a>(&self, input: &'a str) -> Option<&'a str> {
        let position_questionmark = input.find('?');
        let position_pound = input.find('#');
        if let Some(v) = position_questionmark {
            let end = match position_pound {
                Some(v) => v,
                None => input.len(),
            };
            let after = &input[v + 1..end];
            return Some(after);
        }
        None
    }
}

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

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

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