file_storage/path/construct/
parse.rs

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