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
63
64
65
66
67
68
69
70
71
72
73
use crate::TextRange;
#[derive(Debug, Clone, PartialEq)]
pub struct Match {
range: TextRange,
index_of_patterns: Option<u32>,
}
impl Match {
pub fn new(range: TextRange, index_of_patterns: Option<u32>) -> Self {
Self { range, index_of_patterns }
}
pub fn range(&self) -> TextRange {
self.range
}
pub fn index_of_patterns(&self) -> Option<u32> {
self.index_of_patterns
}
pub fn value_from<T: Copy>(&self, map: Vec<T>) -> Option<T> {
match self.index_of_patterns {
Some(index) => {
match map.get(index as usize) {
Some(result) => Some(*result),
None => None
}
}
None => None
}
}
}
#[cfg(test)]
mod tests {
mod value {
use crate::{Match, TextRange};
#[test]
fn test_none() {
let mat = Match::new(TextRange::new(0, 1), None);
let map = vec![1, 2];
let result = mat.value_from(map);
assert!(result.is_none());
}
#[test]
fn test_some() {
let mat = Match::new(TextRange::new(0, 1), Some(1));
let map = vec!["0", "1"];
let result = mat.value_from(map).unwrap();
assert_eq!(result, "1");
}
#[test]
fn test_out_of_bounds() {
let mat = Match::new(TextRange::new(0, 1), Some(2));
let map = vec!["0", "1"];
let result = mat.value_from(map);
assert!(result.is_none());
}
}
}