file_storage/path/construct/parse.rs
1use crate::{Error, StoragePath};
2use std::io;
3
4impl StoragePath {
5 //! Parse
6
7 /// Parses the `path`.
8 pub fn parse<S>(path: S) -> Result<Self, io::Error>
9 where
10 S: AsRef<str> + Into<String>,
11 {
12 let s: &str = path.as_ref();
13
14 if StoragePath::is_unix_path_str(s) {
15 return Ok(unsafe { StoragePath::new(path, 1, '/') });
16 } else if StoragePath::is_windows_path_str(s) {
17 // todo -- refactor windows paths
18 // Windows paths are rather poorly designed. I am not sure if I want to normalize the
19 // path case and file-separators here or keep it as is and handle de-duplication and
20 // other logic in other places. I am also not sure if I should have equality be
21 // case-insensitive. I don't use this 🗑🔥 of an OS so I will address this later.
22 // todo -- this logic is very interdependent and should be refactored as well
23 let file_separator: char = s.as_bytes()[2] as char;
24 return Ok(unsafe { StoragePath::new(path, 3, file_separator) });
25 }
26
27 #[cfg(feature = "r2")]
28 if let Some(base_len) = crate::R2Path::base_len(s) {
29 return Ok(unsafe { StoragePath::new(path.into(), base_len, '/') });
30 }
31
32 Err(Error::unknown_file_system(s))
33 }
34}