|
1 | 1 | /// Utilities for handling paths on ``rustic_core``
|
| 2 | +use std::borrow::Cow; |
| 3 | + |
2 | 4 | use globset::GlobMatcher;
|
3 | 5 | use serde::{Serialize, Serializer};
|
4 |
| -use typed_path::{UnixPath, UnixPathBuf}; |
| 6 | +use typed_path::{ |
| 7 | + Component, TypedPath, UnixComponent, UnixPath, UnixPathBuf, WindowsComponent, WindowsPath, |
| 8 | + WindowsPrefix, |
| 9 | +}; |
5 | 10 |
|
6 | 11 | /// Extend `globset::GlobMatcher` to allow mathing on unix paths (on every platform)
|
7 | 12 | pub trait GlobMatcherExt {
|
|
43 | 48 | let s = format!("{}", path.display());
|
44 | 49 | serializer.serialize_str(&s)
|
45 | 50 | }
|
| 51 | + |
| 52 | +/// Converts a [`TypedPath`] to an [`Cow<UnixPath>`]. |
| 53 | +/// |
| 54 | +/// # Arguments |
| 55 | +/// |
| 56 | +/// * `path` - The path to convert. |
| 57 | +#[must_use] |
| 58 | +pub fn typed_path_to_unix_path<'a>(path: &'a TypedPath<'_>) -> Cow<'a, UnixPath> { |
| 59 | + match path { |
| 60 | + TypedPath::Unix(p) => Cow::Borrowed(p), |
| 61 | + TypedPath::Windows(p) => Cow::Owned(windows_path_to_unix_path(p)), |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// Converts a [`WindowsPath`] to a [`UnixPathBuf`]. |
| 66 | +/// |
| 67 | +/// # Arguments |
| 68 | +/// |
| 69 | +/// * `path` - The path to convert. |
| 70 | +#[must_use] |
| 71 | +pub fn windows_path_to_unix_path(path: &WindowsPath) -> UnixPathBuf { |
| 72 | + let mut unix_path = UnixPathBuf::new(); |
| 73 | + let mut components = path.components(); |
| 74 | + if let Some(c) = components.next() { |
| 75 | + match c { |
| 76 | + WindowsComponent::Prefix(p) => { |
| 77 | + unix_path.push(UnixComponent::RootDir); |
| 78 | + match p.kind() { |
| 79 | + WindowsPrefix::Verbatim(p) | WindowsPrefix::DeviceNS(p) => { |
| 80 | + unix_path.push(p); |
| 81 | + } |
| 82 | + WindowsPrefix::VerbatimUNC(_, q) | WindowsPrefix::UNC(_, q) => { |
| 83 | + unix_path.push(q); |
| 84 | + } |
| 85 | + WindowsPrefix::VerbatimDisk(p) | WindowsPrefix::Disk(p) => { |
| 86 | + let c = vec![p]; |
| 87 | + unix_path.push(&c); |
| 88 | + } |
| 89 | + } |
| 90 | + // remove RootDir from iterator |
| 91 | + _ = components.next(); |
| 92 | + } |
| 93 | + WindowsComponent::RootDir => { |
| 94 | + unix_path.push(UnixComponent::RootDir); |
| 95 | + } |
| 96 | + c => { |
| 97 | + unix_path.push(c.as_bytes()); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + for c in components { |
| 102 | + match c { |
| 103 | + WindowsComponent::RootDir => { |
| 104 | + unix_path.push(UnixComponent::RootDir); |
| 105 | + } |
| 106 | + c => { |
| 107 | + unix_path.push(c); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + unix_path |
| 112 | +} |
| 113 | + |
| 114 | +#[cfg(test)] |
| 115 | +mod tests { |
| 116 | + use super::*; |
| 117 | + use rstest::rstest; |
| 118 | + #[rstest] |
| 119 | + #[case("/", "/")] |
| 120 | + #[case(r#"\"#, "/")] |
| 121 | + #[case("/test/test2", "/test/test2")] |
| 122 | + #[case(r#"\test\test2"#, "/test/test2")] |
| 123 | + #[case(r#"C:\"#, "/C")] |
| 124 | + #[case(r#"C:\dir"#, "/C/dir")] |
| 125 | + #[case(r#"a\b\"#, "a/b")] |
| 126 | + #[case(r#"a\b\c"#, "a/b/c")] |
| 127 | + fn test_typed_path_to_unix_path(#[case] windows_path: &str, #[case] unix_path: &str) { |
| 128 | + assert_eq!( |
| 129 | + windows_path_to_unix_path(WindowsPath::new(windows_path)) |
| 130 | + .to_str() |
| 131 | + .unwrap(), |
| 132 | + UnixPath::new(unix_path).to_str().unwrap() |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
0 commit comments