shellexpand/
strings.rs

1
2use std::fmt::Display;
3use std::str::Chars;
4
5#[path="wtraits.rs"]
6pub mod wtraits;
7use self::wtraits::*;
8
9#[path="funcs.rs"]
10pub mod funcs;
11
12pub type Xstr = str;
13
14pub type WInput<'x> = &'x str;
15
16pub type Wstr = str;
17
18pub type OString = String;
19
20impl XstrRequirements for str {
21}
22
23impl<S: AsRef<str> + ?Sized> AsRefXstrExt for S {
24    fn into_winput(&self) -> &str { self.as_ref() }
25    fn into_ocow(&self) -> Cow<'_, str> { self.as_ref().into() }
26}
27
28impl WstrExt for str {
29    fn as_str(&self) -> Option<&str> { Some(self) }
30    fn len(&self) -> usize { str::len(self) }
31    fn to_ostring(&self) -> String { self.to_owned() }
32    fn strip_prefix(&self, c: char) -> Option<&Self> {
33        if self.starts_with(c) {
34            Some(&self[c.len_utf8()..])
35        } else {
36            None
37        }
38    }
39}
40
41impl<'s> WstrRefExt for &'s str {
42    type Chars = Chars<'s>;
43
44    /// Must be used only for the-{}-unbracketed $varname expansion variable name termination detection
45    ///
46    /// The implementation for `paths.rs` is ... limited.
47    fn chars_approx(self) -> Chars<'s> {
48        self.chars()
49    }
50}
51
52impl CharsExt for Chars<'_> {
53    fn wstr_len(&self) -> usize {
54        self.as_str().len()
55    }
56}
57
58impl OStringExt for String {
59    fn push_str(&mut self, x: &str) { self.push_str(x) }
60    fn push_wstr(&mut self, x: &str) { self.push_str(x) }
61    fn push_xstr(&mut self, x: &str) { self.push_str(x) }
62    fn into_ocow(self) -> Cow<'static, str> { self.into() }
63
64    fn display_possibly_lossy(&self, f: &mut fmt::Formatter) -> fmt::Result {
65        Display::fmt(self, f)
66    }
67}
68
69impl PathBufExt for PathBuf {
70    fn try_into_xstring(self) -> Option<String> {
71        self.into_os_string().into_string().ok()
72    }
73}