web_url/parse/
try_from_str.rs

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