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 strip_ansi_string_unlikely(self) -> Self {
18        match self {
19            NuGlob::DoNotExpand(s) => NuGlob::DoNotExpand(nu_utils::strip_ansi_string_unlikely(s)),
20            NuGlob::Expand(s) => NuGlob::Expand(nu_utils::strip_ansi_string_unlikely(s)),
21        }
22    }
23
24    pub fn is_expand(&self) -> bool {
25        matches!(self, NuGlob::Expand(..))
26    }
27}
28
29impl AsRef<str> for NuGlob {
30    fn as_ref(&self) -> &str {
31        match self {
32            NuGlob::DoNotExpand(s) | NuGlob::Expand(s) => s,
33        }
34    }
35}
36
37impl Display for NuGlob {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        write!(f, "{}", self.as_ref())
40    }
41}