web_url/parse/
try_from_string.rs

1use crate::parse::{parse_path_plus, parse_pre_path, Error, PathPlus, PrePath};
2use crate::WebUrl;
3
4impl TryFrom<String> for WebUrl {
5    type Error = (Error, String);
6
7    fn try_from(s: String) -> Result<Self, Self::Error> {
8        let pre_path: PrePath = match parse_pre_path(s.as_str()) {
9            Ok(pre_path) => pre_path,
10            Err(error) => return Err((error, s)),
11        };
12
13        if pre_path.len() == s.len() {
14            let mut url: String = s;
15            url.push('/');
16
17            let path_plus: PathPlus = match parse_path_plus("/") {
18                Ok(path_plus) => path_plus,
19                Err(error) => return Err((error, url)),
20            };
21            unsafe { crate::parse::finalize::finalize_web_url(url, pre_path, path_plus) }
22        } else {
23            let url: String = s;
24            let path_plus: PathPlus = match parse_path_plus(&url.as_str()[pre_path.len()..]) {
25                Ok(path_plus) => path_plus,
26                Err(error) => return Err((error, url)),
27            };
28            unsafe { crate::parse::finalize::finalize_web_url(url, pre_path, path_plus) }
29        }
30    }
31}