Skip to main content

nu_protocol/value/
glob.rs

1use serde::Deserialize;
2use std::fmt::Display;
3
4// Introduce this `NuGlob` enum rather than using `Value::Glob` directlry
5// So we can handle glob easily without considering too much variant of `Value` enum.
6#[derive(Debug, Clone, Deserialize)]
7pub enum NuGlob {
8    /// Don't expand the glob pattern, normally it includes a quoted string(except backtick)
9    /// And a variable that doesn't annotated with `glob` type
10    DoNotExpand(String),
11    /// A glob pattern that is required to expand, it includes bare word
12    /// And a variable which is annotated with `glob` type
13    Expand(String),
14}
15
16impl NuGlob {
17    pub fn map(self, f: impl FnOnce(String) -> String) -> Self {
18        match self {
19            NuGlob::DoNotExpand(s) => NuGlob::DoNotExpand(f(s)),
20            NuGlob::Expand(s) => NuGlob::Expand(f(s)),
21        }
22    }
23
24    pub fn strip_ansi_string_unlikely(self) -> Self {
25        self.map(nu_utils::strip_ansi_string_unlikely)
26    }
27
28    pub fn is_expand(&self) -> bool {
29        matches!(self, NuGlob::Expand(..))
30    }
31}
32
33impl AsRef<str> for NuGlob {
34    fn as_ref(&self) -> &str {
35        match self {
36            NuGlob::DoNotExpand(s) | NuGlob::Expand(s) => s,
37        }
38    }
39}
40
41impl Display for NuGlob {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        write!(f, "{}", self.as_ref())
44    }
45}