File_system/Fundamentals/Path/
Path_reference.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
use std::borrow::Borrow;

use super::*;

/// A borrowed path type.
/// The implementation is very similar to the standard library's [`std::path::Path`].
/// However, this implementation is more lightweight and allows for std-less usage.
#[derive(Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Path_type(str);

impl Path_type {
    pub const Root: &'static Path_type = Self::From_str("/");
    pub const Empty: &'static Path_type = Self::From_str("");

    /// Contains the OS core, including the kernel, init system, and critical drivers.
    /// Prevents modification by regular users.
    pub const System: &'static Path_type = Self::From_str("/System");

    /// Stores system-wide settings in a structured format (e.g., JSON, TOML).
    pub const Devices: &'static Path_type = Self::From_str("/Devices");

    /// Hardware devices, symlinks for human-friendly names.
    pub const Configuration: &'static Path_type = Self::From_str("/Configuration");

    /// Contains the shared configurations between applications.
    pub const Shared_configuration: &'static Path_type = Self::From_str("/Configuration/Shared");

    /// Binaries data.
    pub const Data: &'static Path_type = Self::From_str("/Data");

    /// Shared data between binaries.
    pub const Shared_data: &'static Path_type = Self::From_str("/Data/Shared");

    /// Contains the system's binaries, including the shell and other executables.
    pub const Binaries: &'static Path_type = Self::From_str("/Binaries");

    /// Contains the user's data, including documents, downloads, and other files.
    pub const Users: &'static Path_type = Self::From_str("/Users");

    /// Contains temporary files, including logs and caches.
    pub const Temporary: &'static Path_type = Self::From_str("/Temporary");

    /// Contains logs, including system logs and application logs.
    pub const Logs: &'static Path_type = Self::From_str("/Temporary/Logs");

    /// # Safety
    /// The caller must ensure that the string is a valid path string.
    pub const fn From_str(Path: &str) -> &Path_type {
        unsafe { &*(Path as *const str as *const Path_type) }
    }

    /// # Safety
    /// The caller must ensure that the string is a valid path string.
    pub fn New<S: AsRef<str> + ?Sized>(Path: &S) -> &Path_type {
        unsafe { &*(Path.as_ref() as *const str as *const Path_type) }
    }

    pub fn Is_valid(&self) -> bool {
        Is_valid_string(&self.0)
    }

    pub fn Is_absolute(&self) -> bool {
        self.0.starts_with('/')
    }

    pub fn Is_root(&self) -> bool {
        &self.0 == "/"
    }

    pub fn Is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn Is_canonical(&self) -> bool {
        self.0.chars().all(|c| c != '.')
    }

    pub fn Go_parent(&self) -> Option<&Path_type> {
        let Characters_to_remove = match self.0.rfind(Separator) {
            Some(index) => index,
            None => {
                // If there is no separator, the path is either empty or relative to the current directory.
                if self.Get_length() > 0 {
                    // Relative to the current directory.
                    return Some(Self::Empty);
                } else {
                    return None;
                }
            }
        };

        if Characters_to_remove == 0 {
            if self.Get_length() == 1 {
                return None;
            }

            if self.Is_absolute() {
                return Some(Self::Root);
            } else {
                return Some(Self::From_str(""));
            }
        }

        Some(Self::From_str(&self.0[..Characters_to_remove]))
    }

    pub fn Get_file_prefix(&self) -> Option<&str> {
        let Extension_start = self
            .0
            .rfind(Extension_separator)
            .or_else(|| Some(self.Get_length()))?; // Find the extension separator.
        let File_prefix_start = self.0.rfind(Separator).map(|i| i + 1).unwrap_or(0); // Find the file prefix start.

        if Extension_start <= File_prefix_start {
            return None;
        }

        Some(&self.0[File_prefix_start..Extension_start])
    }

    pub fn Get_file_name(&self) -> Option<&str> {
        let File_prefix_start = self.0.rfind(Separator).map(|i| i + 1).unwrap_or(0); // Find the file prefix start.

        if File_prefix_start >= self.Get_length() {
            return None;
        }

        Some(&self.0[File_prefix_start..])
    }

    pub fn Get_extension(&self) -> Option<&str> {
        let Extension_start = self.0.rfind(Extension_separator)?;

        Some(&self.0[Extension_start + 1..])
    }

    pub fn Set_extension(&self, Extension: &str) -> Option<Path_owned_type> {
        let Extension_start = self
            .0
            .rfind(Extension_separator)
            .unwrap_or(self.Get_length());

        Some(unsafe {
            Path_owned_type::New_unchecked(format!("{}{}", &self.0[..Extension_start], Extension))
        })
    }

    pub fn Strip_prefix<'b>(&'b self, Path_prefix: &Path_type) -> Option<&'b Path_type> {
        let mut Stripped_prefix = self.0.strip_prefix(&Path_prefix.0)?;

        if Stripped_prefix.starts_with(Separator) {
            Stripped_prefix = &Stripped_prefix[1..]
        }

        Some(Self::From_str(Stripped_prefix))
    }

    pub fn Strip_prefix_absolute<'b>(&'b self, Path_prefix: &Path_type) -> Option<&'b Path_type> {
        if Path_prefix.Is_root() {
            return Some(self);
        }

        let Stripped_prefix = self.0.strip_prefix(&Path_prefix.0)?;

        if Stripped_prefix.is_empty() {
            return Some(Self::Root);
        }

        Some(Self::From_str(Stripped_prefix))
    }

    pub fn Strip_suffix<'b>(&'b self, Path_suffix: &Path_type) -> Option<&'b Path_type> {
        let Stripped_suffix = self.0.strip_suffix(&Path_suffix.0)?;

        if Stripped_suffix.ends_with(Separator) {
            return Some(Self::From_str(
                &Stripped_suffix[..Stripped_suffix.len() - 1],
            ));
        }

        Some(Self::From_str(Stripped_suffix))
    }

    pub fn Get_components(&self) -> Components_type {
        Components_type::New(self)
    }

    pub fn Join(&self, Path: &Path_type) -> Option<Path_owned_type> {
        self.to_owned().Join(Path)
    }

    pub fn Append(&self, Path: &str) -> Option<Path_owned_type> {
        self.to_owned().Append(Path)
    }

    pub fn Get_length(&self) -> usize {
        self.0.len()
    }

    pub fn As_str(&self) -> &str {
        &self.0
    }
}

#[cfg(feature = "std")]
impl AsRef<std::path::Path> for Path_type {
    fn as_ref(&self) -> &std::path::Path {
        std::path::Path::new(&self.0)
    }
}

impl ToOwned for Path_type {
    type Owned = Path_owned_type;

    fn to_owned(&self) -> Self::Owned {
        unsafe { Path_owned_type::New_unchecked(self.0.to_string()) }
    }

    fn clone_into(&self, target: &mut Self::Owned) {
        *target = self.to_owned();
    }
}

impl Borrow<Path_type> for Path_owned_type {
    fn borrow(&self) -> &Path_type {
        Path_type::From_str(&self.0)
    }
}

impl AsRef<Path_type> for str {
    fn as_ref(&self) -> &Path_type {
        Path_type::From_str(self)
    }
}

impl AsRef<str> for &Path_type {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl AsRef<Path_type> for Path_type {
    fn as_ref(&self) -> &Path_type {
        self
    }
}

#[cfg(test)]
mod Tests {
    use super::*;

    #[test]
    fn Test_strip_prefix() {
        let Path = Path_type::From_str("/home/user/file.txt");
        let Prefix = Path_type::From_str("/home/user");
        assert_eq!(Path.Strip_prefix(Prefix).unwrap().As_str(), "file.txt");

        let Path = Path_type::From_str("/home/user/file.txt");
        let Prefix = Path_type::From_str("/");
        assert_eq!(
            Path.Strip_prefix(Prefix).unwrap().As_str(),
            "home/user/file.txt"
        );

        let Invalid_prefix = Path_type::From_str("/home/invalid/");
        assert_eq!(Path.Strip_prefix(Invalid_prefix), None);
    }

    #[test]
    fn Test_strip_prefix_absolute() {
        let Path = Path_type::From_str("/");
        let Prefix = Path_type::From_str("/");
        assert_eq!(Path.Strip_prefix_absolute(Prefix).unwrap().As_str(), "/");

        let Path = Path_type::From_str("/Foo/Bar");
        let Prefix = Path_type::From_str("/Foo/Bar");
        assert_eq!(Path.Strip_prefix_absolute(Prefix).unwrap().As_str(), "/");

        let Path = Path_type::From_str("/home/user/file.txt");
        let Prefix = Path_type::From_str("/home/user");
        assert_eq!(
            Path.Strip_prefix_absolute(Prefix).unwrap().As_str(),
            "/file.txt"
        );

        let Path = Path_type::From_str("/home/user/file.txt");
        let Prefix = Path_type::From_str("/");
        assert_eq!(
            Path.Strip_prefix_absolute(Prefix).unwrap().As_str(),
            "/home/user/file.txt"
        );

        let Invalid_prefix = Path_type::From_str("/home/invalid/");
        assert_eq!(Path.Strip_prefix_absolute(Invalid_prefix), None);
    }

    #[test]
    fn Test_strip_suffix() {
        let Path = Path_type::From_str("/home/user/file.txt");
        let Suffix = Path_type::From_str("user/file.txt");
        assert_eq!(Path.Strip_suffix(Suffix).unwrap().As_str(), "/home");

        let Invalid_suffix = Path_type::From_str("user/invalid.txt");
        assert_eq!(Path.Strip_suffix(Invalid_suffix), None);
    }

    #[test]
    fn Test_go_parent() {
        let Path = Path_type::From_str("/home/user/file.txt");
        assert_eq!(&Path.Go_parent().unwrap().0, "/home/user");
        assert_eq!(&Path.Go_parent().unwrap().Go_parent().unwrap().0, "/home");
        assert_eq!(
            &Path
                .Go_parent()
                .unwrap()
                .Go_parent()
                .unwrap()
                .Go_parent()
                .unwrap()
                .0,
            "/"
        );
        assert_eq!(
            Path.Go_parent()
                .unwrap()
                .Go_parent()
                .unwrap()
                .Go_parent()
                .unwrap()
                .Go_parent(),
            None
        );

        let Path = Path_type::From_str("home/user/file.txt");
        assert_eq!(&Path.Go_parent().unwrap().0, "home/user");
        assert_eq!(&Path.Go_parent().unwrap().Go_parent().unwrap().0, "home");
        assert_eq!(
            &Path
                .Go_parent()
                .unwrap()
                .Go_parent()
                .unwrap()
                .Go_parent()
                .unwrap()
                .0,
            ""
        );
    }

    #[test]
    fn Test_path_file() {
        // Regular case
        let Path = Path_type::From_str("/Directory/File.txt");
        assert_eq!(Path.Get_extension(), Some("txt"));
        assert_eq!(Path.Get_file_prefix(), Some("File"));
        assert_eq!(Path.Get_file_name(), Some("File.txt"));

        // No extension
        let Path = Path_type::From_str("/Directory/File");
        assert_eq!(Path.Get_extension(), None);
        assert_eq!(Path.Get_file_prefix(), Some("File"));
        assert_eq!(Path.Get_file_name(), Some("File"));

        // No file prefix
        let Path = Path_type::From_str("File.txt");
        assert_eq!(Path.Get_extension(), Some("txt"));
        assert_eq!(Path.Get_file_prefix(), Some("File"));
        assert_eq!(Path.Get_file_name(), Some("File.txt"));

        // No file prefix or extension
        let Path = Path_type::From_str("/");
        assert_eq!(Path.Get_extension(), None);
        assert_eq!(Path.Get_file_prefix(), None);
        assert_eq!(Path.Get_file_name(), None);

        // No file prefix or extension
        let Path = Path_type::From_str("File");
        assert_eq!(Path.Get_extension(), None);
        assert_eq!(Path.Get_file_prefix(), Some("File"));
        assert_eq!(Path.Get_file_name(), Some("File"));
    }
}