1use crate::TextRange;
2
3#[derive(Debug, Clone, PartialEq)]
4pub struct Match {
5    range: TextRange,
6
7    index_of_patterns: Option<usize>,
9}
10
11impl Match {
12    pub fn new(range: TextRange, index_of_patterns: Option<usize>) -> Self {
13        Self { range, index_of_patterns }
14    }
15
16    pub fn range(&self) -> TextRange {
17        self.range
18    }
19
20    pub fn index_of_patterns(&self) -> Option<usize> {
21        self.index_of_patterns
22    }
23
24    pub fn value_from<T: Copy>(&self, map: Vec<T>) -> Option<T> {
26        match self.index_of_patterns {
27            Some(index) => {
28                match map.get(index as usize) {
29                    Some(result) => Some(*result),
30                    None => None
31                }
32            }
33            None => None
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    mod value {
41        use crate::{Match, TextRange};
42
43        #[test]
44        fn test_none() {
45            let mat = Match::new(TextRange::new(0, 1), None);
46            let map = vec![1, 2];
47
48            let result = mat.value_from(map);
49
50            assert!(result.is_none());
51        }
52
53        #[test]
54        fn test_some() {
55            let mat = Match::new(TextRange::new(0, 1), Some(1));
56            let map = vec!["0", "1"];
57
58            let result = mat.value_from(map).unwrap();
59
60            assert_eq!(result, "1");
61        }
62
63        #[test]
64        fn test_out_of_bounds() {
65            let mat = Match::new(TextRange::new(0, 1), Some(2));
66            let map = vec!["0", "1"];
67
68            let result = mat.value_from(map);
69
70            assert!(result.is_none());
71        }
72    }
73}