Coverage Report

Created: 2023-04-25 07:07

/rust/registry/src/index.crates.io-6f17d22bba15001f/filecheck-0.5.0/src/variable.rs
Line
Count
Source (jump to first uncovered line)
1
use std::borrow::Cow;
2
3
/// A variable name is one or more ASCII alphanumerical characters, including underscore.
4
/// Note that numerical variable names like `$45` are allowed too.
5
///
6
/// Try to parse a variable name from the beginning of `s`.
7
/// Return the index of the character following the varname.
8
/// This returns 0 if `s` doesn't have a prefix that is a variable name.
9
0
pub fn varname_prefix(s: &str) -> usize {
10
0
    for (idx, ch) in s.char_indices() {
11
0
        match ch {
12
0
            'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => {}
13
0
            _ => return idx,
14
        }
15
    }
16
0
    s.len()
17
0
}
18
19
/// A variable can contain either a regular expression or plain text.
20
0
#[derive(Debug, Clone, PartialEq, Eq)]
21
pub enum Value<'a> {
22
    /// Verbatim text.
23
    Text(Cow<'a, str>),
24
    /// Regular expression.
25
    Regex(Cow<'a, str>),
26
}
27
28
/// Resolve variables by name.
29
pub trait VariableMap {
30
    /// Get the value of the variable `varname`, or return `None` for an unknown variable name.
31
    fn lookup(&self, varname: &str) -> Option<Value>;
32
}
33
34
impl VariableMap for () {
35
0
    fn lookup(&self, _: &str) -> Option<Value> {
36
0
        None
37
0
    }
38
}
39
40
/// An empty variable map.
41
pub const NO_VARIABLES: &'static dyn VariableMap = &();
42
43
#[cfg(test)]
44
mod tests {
45
    #[test]
46
    fn varname() {
47
        use super::varname_prefix;
48
49
        assert_eq!(varname_prefix(""), 0);
50
        assert_eq!(varname_prefix("\0"), 0);
51
        assert_eq!(varname_prefix("_"), 1);
52
        assert_eq!(varname_prefix("0"), 1);
53
        assert_eq!(varname_prefix("01"), 2);
54
        assert_eq!(varname_prefix("b"), 1);
55
        assert_eq!(varname_prefix("C"), 1);
56
        assert_eq!(varname_prefix("."), 0);
57
        assert_eq!(varname_prefix(".s"), 0);
58
        assert_eq!(varname_prefix("0."), 1);
59
        assert_eq!(varname_prefix("01="), 2);
60
        assert_eq!(varname_prefix("0a)"), 2);
61
    }
62
}