1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::borrow::Cow;
pub fn varname_prefix(s: &str) -> usize {
for (idx, ch) in s.char_indices() {
match ch {
'a'...'z' | 'A'...'Z' | '0'...'9' | '_' => {}
_ => return idx,
}
}
s.len()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Value<'a> {
Text(Cow<'a, str>),
Regex(Cow<'a, str>),
}
pub trait VariableMap {
fn lookup(&self, varname: &str) -> Option<Value>;
}
impl VariableMap for () {
fn lookup(&self, _: &str) -> Option<Value> {
None
}
}
pub const NO_VARIABLES: &'static VariableMap = &();
#[cfg(test)]
mod tests {
#[test]
fn varname() {
use super::varname_prefix;
assert_eq!(varname_prefix(""), 0);
assert_eq!(varname_prefix("\0"), 0);
assert_eq!(varname_prefix("_"), 1);
assert_eq!(varname_prefix("0"), 1);
assert_eq!(varname_prefix("01"), 2);
assert_eq!(varname_prefix("b"), 1);
assert_eq!(varname_prefix("C"), 1);
assert_eq!(varname_prefix("."), 0);
assert_eq!(varname_prefix(".s"), 0);
assert_eq!(varname_prefix("0."), 1);
assert_eq!(varname_prefix("01="), 2);
assert_eq!(varname_prefix("0a)"), 2);
}
}