file_storage/path/construct/
parse.rs1use crate::{Error, StoragePath};
2use std::io;
3
4impl StoragePath {
5 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 return Ok(Self::parse_windows(path.into()));
18 }
19
20 #[cfg(feature = "r2")]
21 if let Some(base_len) = crate::R2Path::base_len(s) {
22 return Ok(unsafe { StoragePath::new(path, base_len, '/') });
23 }
24
25 Err(Error::unknown_file_system(s))
26 }
27
28 fn parse_windows(mut path: String) -> Self {
30 unsafe {
32 for byte in path.as_bytes_mut() {
33 if *byte == b'/' {
34 *byte = b'\\';
35 }
36 }
37 }
38 let base_len: usize = Self::windows_base_len(&path);
39 unsafe { StoragePath::new(path, base_len, '\\') }
40 }
41
42 fn windows_base_len(path: &str) -> usize {
46 let bytes: &[u8] = path.as_bytes();
47
48 if bytes.len() >= 3
50 && bytes[0].is_ascii_alphabetic()
51 && bytes[1] == b':'
52 && bytes[2] == b'\\'
53 {
54 return 3;
55 }
56
57 if bytes.len() >= 3 && bytes[0] == b'\\' && bytes[1] == b'\\' {
59 if let Some(server_end) = path[2..].find('\\') {
60 let share_start: usize = 2 + server_end + 1;
61 if share_start < path.len() {
62 if let Some(share_end) = path[share_start..].find('\\') {
63 return share_start + share_end + 1;
64 }
65 }
66 }
67 }
68
69 path.len()
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use crate::StoragePath;
77
78 #[test]
79 fn parse_drive_letter() {
80 let path: StoragePath = StoragePath::parse("C:\\folder\\file.txt").unwrap();
81 assert_eq!(path.path(), "C:\\folder\\file.txt");
82 assert_eq!(path.base_path(), "C:\\");
83 assert_eq!(path.relative_path(), "folder\\file.txt");
84 assert_eq!(path.file_separator(), '\\');
85 assert!(path.is_file());
86 }
87
88 #[test]
89 fn parse_drive_letter_normalizes_separators() {
90 let path: StoragePath = StoragePath::parse("C:/folder/file.txt").unwrap();
91 assert_eq!(path.path(), "C:\\folder\\file.txt");
92 assert_eq!(path.base_path(), "C:\\");
93 assert_eq!(path.file_separator(), '\\');
94 }
95
96 #[test]
97 fn parse_drive_letter_mixed_separators() {
98 let path: StoragePath = StoragePath::parse("C:\\folder/file.txt").unwrap();
99 assert_eq!(path.path(), "C:\\folder\\file.txt");
100 }
101
102 #[test]
103 fn parse_drive_letter_folder() {
104 let path: StoragePath = StoragePath::parse("C:\\folder\\").unwrap();
105 assert!(path.is_folder());
106 }
107
108 #[test]
109 fn parse_drive_letter_root() {
110 let path: StoragePath = StoragePath::parse("C:\\").unwrap();
111 assert_eq!(path.base_path(), "C:\\");
112 assert_eq!(path.relative_path(), "");
113 assert!(path.is_folder());
114 }
115
116 #[test]
117 fn parse_unc_path() {
118 let path: StoragePath = StoragePath::parse("\\\\server\\share\\file.txt").unwrap();
119 assert_eq!(path.base_path(), "\\\\server\\share\\");
120 assert_eq!(path.relative_path(), "file.txt");
121 assert_eq!(path.file_separator(), '\\');
122 assert!(path.is_file());
123 }
124
125 #[test]
126 fn parse_unc_folder() {
127 let path: StoragePath = StoragePath::parse("\\\\server\\share\\folder\\").unwrap();
128 assert_eq!(path.base_path(), "\\\\server\\share\\");
129 assert_eq!(path.relative_path(), "folder\\");
130 assert!(path.is_folder());
131 }
132
133 #[test]
134 fn parse_unc_root() {
135 let path: StoragePath = StoragePath::parse("\\\\server\\share\\").unwrap();
136 assert_eq!(path.base_path(), "\\\\server\\share\\");
137 assert_eq!(path.relative_path(), "");
138 assert!(path.is_folder());
139 }
140
141 #[test]
142 fn parse_unc_incomplete_no_share() {
143 let path: StoragePath = StoragePath::parse("\\\\server").unwrap();
144 assert_eq!(path.base_path(), "\\\\server");
145 assert_eq!(path.relative_path(), "");
146 assert!(path.is_folder());
147 }
148
149 #[test]
150 fn parse_unc_incomplete_no_share_separator() {
151 let path: StoragePath = StoragePath::parse("\\\\server\\share").unwrap();
152 assert_eq!(path.base_path(), "\\\\server\\share");
153 assert_eq!(path.relative_path(), "");
154 assert!(path.is_folder());
155 }
156
157 #[test]
158 fn parse_unknown_file_system() {
159 assert!(StoragePath::parse("not_a_path").is_err());
160 }
161}