Skip to main content

shellexpand/
path.rs

1//! Expansion of [`Path`] values
2//!
3//! These functions are the same as the ones in the crate toplevel,
4//! except that they take [`Path`]s as input and return `Cow<Path>`.
5//!
6//! (Note that the individual doc comments and examples still refer to
7//! `str` and `String` and so on - please refer to the actual types.
8//! The semantics are as described.)
9
10use std::ffi::OsString;
11use std::path::Path;
12
13use bstr::ByteSlice as _;
14
15#[path="wtraits.rs"]
16pub(crate) mod wtraits;
17use wtraits::*;
18
19#[path="funcs.rs"]
20pub(crate) mod funcs;
21pub use funcs::*;
22
23use os_str_bytes::RawOsStr;
24
25type Xstr = std::path::Path;
26
27pub(crate) type WInput<'x> = Cow<'x, RawOsStr>;
28
29pub(crate) type Wstr = RawOsStr;
30
31pub(crate) type OString = OsString;
32
33impl XstrRequirements for Path {
34}
35
36impl<P: AsRef<Path> + ?Sized> AsRefXstrExt for P {
37    fn into_winput(&self) -> Cow<'_, RawOsStr> { RawOsStr::new(self.as_ref().as_os_str()) }
38    fn into_ocow(&self) -> Cow<'_, Path> { self.as_ref().into() }
39}
40
41impl WstrExt for RawOsStr {
42    fn wstr_as_str(&self) -> Option<&str> { self.to_str() }
43    fn wstr_len(&self) -> usize {
44        // Bizarrely, while a `RawOsStr` implements `Index` with various `usize`-based
45        // indices, there doesn't seem to be a way to get the maximum index!
46        //
47        // There's `raw_len` but that may be something else.  In os_str_bytes 7.x it's
48        // behind a `conversions` method, and is the length of some converted representation.
49        //
50        // But we can (ab)use `.rfind("")` since that will always match, at the end.
51        self.rfind("").expect("empty string not found!")
52    }
53    fn wstr_to_ostring(&self) -> OsString { self.to_os_str().into_owned() }
54    fn wstr_strip_prefix(&self, c: char) -> Option<&Self> { self.strip_prefix(c) }
55}
56
57impl<'s> WstrRefExt for &'s RawOsStr {
58    type Chars = bstr::Chars<'s>;
59
60    /// This is quite approximate, really.
61    ///
62    /// os_str_bytes says the encoding is "compatible with" UTF-8, and that splitting on UTF-8
63    /// characters yields valid substrings.
64    ///
65    /// We assume, without justification, that the characters we care about handling correctly
66    /// in un-{} $var expansions, are represented closely enough that this works.
67    ///
68    /// On Unix-using-UTF-8 this will be true because it's all, in fact, Unicode.
69    /// On other Unix, at least ASCII will work right.
70    /// On Windows things that use surrogates will possibly go wrong?
71    /// On platforms where this is some mutant form of EBCDIC or something, this will be hopeless.
72    // In os_str_ext 7.x, `as_raw_bytes` is only available with the `conversions` feature.
73    // (Because it can involve a non-cheap conversation; this is not unexpected here.)
74    // We're not upgrading to 7.x though, now because it would break our MSRV.
75    #[allow(deprecated)]
76    fn wstr_chars_approx(self) -> bstr::Chars<'s> {
77        self.as_raw_bytes().chars()
78    }
79
80}
81
82impl CharsExt for bstr::Chars<'_> {
83    fn wstr_len(&self) -> usize {
84        self.as_bytes().len()
85    }
86}
87
88impl OStringExt for OsString {
89    fn push_str(&mut self, x: &str) { self.push(x) }
90    fn push_wstr(&mut self, x: &RawOsStr) { self.push(x.to_os_str()) }
91    fn push_xstr(&mut self, x: &Path) { self.push(x.as_os_str()) }
92    fn into_ocow(self) -> Cow<'static, Path> { PathBuf::from(self).into() }
93
94    fn display_possibly_lossy(&self, f: &mut fmt::Formatter) -> fmt::Result {
95        f.write_str(&self.to_string_lossy())
96    }
97}
98
99impl PathBufExt for PathBuf {
100    fn try_into_xstring(self) -> Option<PathBuf> { Some(self) }
101}