web_url/parse/path_plus/
path_plus.rs

1use crate::parse::path_plus::{parse_fragment, parse_path, parse_query};
2use crate::parse::Error;
3
4/// The parsing data for a web-based URL from the path to the end.
5#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
6pub struct PathPlus {
7    pub path_len: usize,     // length of the path including the '/' (will be 1+)
8    pub query_len: usize,    // length of query including the '?' (will be 0+)
9    pub fragment_len: usize, // length of fragment including the '#' (will be 0+)
10}
11
12/// Parses the `path_plus`.
13///
14/// The path, query, and fragment will be validated.
15pub fn parse_path_plus(path_plus: &str) -> Result<PathPlus, Error> {
16    let (path, after_path) = parse_path(path_plus)?;
17    let (query, after_query) = parse_query(after_path)?;
18    let fragment: Option<&str> = parse_fragment(after_query)?;
19
20    let path_len: usize = path.as_str().len();
21    let query_len: usize = query.map(|q| q.as_str().len()).unwrap_or(0);
22    let fragment_len: usize = fragment.map(|f| f.len()).unwrap_or(0);
23
24    Ok(PathPlus {
25        path_len,
26        query_len,
27        fragment_len,
28    })
29}