Skip to main content

strict_path/path/virtual_path/
traits.rs

1use super::VirtualPath;
2use std::fmt;
3use std::hash::{Hash, Hasher};
4use std::path::Path;
5
6impl<Marker> fmt::Debug for VirtualPath<Marker> {
7    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8        f.debug_struct("VirtualPath")
9            .field("system_path", &self.inner.path())
10            .field("virtual", &self.virtualpath_display().to_string())
11            .field("boundary", &self.inner.boundary().path())
12            .field("marker", &std::any::type_name::<Marker>())
13            .finish()
14    }
15}
16
17impl<Marker> PartialEq for VirtualPath<Marker> {
18    #[inline]
19    fn eq(&self, other: &Self) -> bool {
20        self.inner.path() == other.inner.path()
21    }
22}
23
24impl<Marker> Eq for VirtualPath<Marker> {}
25
26impl<Marker> Hash for VirtualPath<Marker> {
27    #[inline]
28    fn hash<H: Hasher>(&self, state: &mut H) {
29        self.inner.path().hash(state);
30    }
31}
32
33impl<Marker> PartialEq<crate::path::strict_path::StrictPath<Marker>> for VirtualPath<Marker> {
34    #[inline]
35    fn eq(&self, other: &crate::path::strict_path::StrictPath<Marker>) -> bool {
36        self.inner.path() == other.path()
37    }
38}
39
40impl<T: AsRef<Path>, Marker> PartialEq<T> for VirtualPath<Marker> {
41    #[inline]
42    fn eq(&self, other: &T) -> bool {
43        // Compare virtual paths - the user-facing representation
44        // If you want system path comparison, use as_unvirtual()
45        let virtual_str = self.virtualpath_display().to_string();
46        let other_str = other.as_ref().to_string_lossy();
47
48        // Normalize both to forward slashes and ensure leading slash
49        let normalized_virtual = virtual_str.as_str();
50
51        #[cfg(windows)]
52        let other_normalized = other_str.replace('\\', "/");
53        #[cfg(not(windows))]
54        let other_normalized = other_str.to_string();
55
56        let normalized_other = if other_normalized.starts_with('/') {
57            other_normalized
58        } else {
59            format!("/{other_normalized}")
60        };
61
62        normalized_virtual == normalized_other
63    }
64}
65
66impl<T: AsRef<Path>, Marker> PartialOrd<T> for VirtualPath<Marker> {
67    #[inline]
68    fn partial_cmp(&self, other: &T) -> Option<std::cmp::Ordering> {
69        // Compare virtual paths - the user-facing representation
70        let virtual_str = self.virtualpath_display().to_string();
71        let other_str = other.as_ref().to_string_lossy();
72
73        // Normalize both to forward slashes and ensure leading slash
74        let normalized_virtual = virtual_str.as_str();
75
76        #[cfg(windows)]
77        let other_normalized = other_str.replace('\\', "/");
78        #[cfg(not(windows))]
79        let other_normalized = other_str.to_string();
80
81        let normalized_other = if other_normalized.starts_with('/') {
82            other_normalized
83        } else {
84            format!("/{other_normalized}")
85        };
86
87        Some(normalized_virtual.cmp(&normalized_other))
88    }
89}
90
91impl<Marker> PartialOrd for VirtualPath<Marker> {
92    #[inline]
93    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
94        Some(self.cmp(other))
95    }
96}
97
98impl<Marker> Ord for VirtualPath<Marker> {
99    #[inline]
100    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
101        self.inner.path().cmp(other.inner.path())
102    }
103}