1 //! Utilities for working with Protobuf paths.
2 
3 use std::collections::HashMap;
4 use std::iter;
5 
6 /// Maps a fully-qualified Protobuf path to a value using path matchers.
7 #[derive(Debug, Default)]
8 pub(crate) struct PathMap<T> {
9     matchers: HashMap<String, T>,
10 }
11 
12 impl<T> PathMap<T> {
13     /// Inserts a new matcher and associated value to the path map.
insert(&mut self, matcher: String, value: T)14     pub(crate) fn insert(&mut self, matcher: String, value: T) {
15         self.matchers.insert(matcher, value);
16     }
17 
18     /// Returns the value which matches the provided fully-qualified Protobuf path.
get(&self, fq_path: &'_ str) -> Option<&T>19     pub(crate) fn get(&self, fq_path: &'_ str) -> Option<&T> {
20         // First, try matching the full path.
21         iter::once(fq_path)
22             // Then, try matching path suffixes.
23             .chain(suffixes(fq_path))
24             // Then, try matching path prefixes.
25             .chain(prefixes(fq_path))
26             // Then, match the global path. This matcher must never fail, since the constructor
27             // initializes it.
28             .chain(iter::once("."))
29             .flat_map(|path| self.matchers.get(path))
30             .next()
31     }
32 
33     /// Returns the value which matches the provided fully-qualified Protobuf path and field name.
get_field(&self, fq_path: &'_ str, field: &'_ str) -> Option<&T>34     pub(crate) fn get_field(&self, fq_path: &'_ str, field: &'_ str) -> Option<&T> {
35         let full_path = format!("{}.{}", fq_path, field);
36         let full_path = full_path.as_str();
37 
38         // First, try matching the path.
39         let value = iter::once(full_path)
40             // Then, try matching path suffixes.
41             .chain(suffixes(full_path))
42             // Then, try matching path suffixes without the field name.
43             .chain(suffixes(fq_path))
44             // Then, try matching path prefixes.
45             .chain(prefixes(full_path))
46             // Then, match the global path. This matcher must never fail, since the constructor
47             // initializes it.
48             .chain(iter::once("."))
49             .flat_map(|path| self.matchers.get(path))
50             .next();
51 
52         value
53     }
54 
55     /// Removes all matchers from the path map.
clear(&mut self)56     pub(crate) fn clear(&mut self) {
57         self.matchers.clear();
58     }
59 }
60 
61 /// Given a fully-qualified path, returns a sequence of fully-qualified paths which match a prefix
62 /// of the input path, in decreasing path-length order.
63 ///
64 /// Example: prefixes(".a.b.c.d") -> [".a.b.c", ".a.b", ".a"]
prefixes(fq_path: &str) -> impl Iterator<Item = &str>65 fn prefixes(fq_path: &str) -> impl Iterator<Item = &str> {
66     std::iter::successors(Some(fq_path), |path| {
67         path.rsplitn(2, '.').nth(1).filter(|path| !path.is_empty())
68     })
69     .skip(1)
70 }
71 
72 /// Given a fully-qualified path, returns a sequence of paths which match the suffix of the input
73 /// path, in decreasing path-length order.
74 ///
75 /// Example: suffixes(".a.b.c.d") -> ["a.b.c.d", "b.c.d", "c.d", "d"]
suffixes(fq_path: &str) -> impl Iterator<Item = &str>76 fn suffixes(fq_path: &str) -> impl Iterator<Item = &str> {
77     std::iter::successors(Some(fq_path), |path| {
78         path.splitn(2, '.').nth(1).filter(|path| !path.is_empty())
79     })
80     .skip(1)
81 }
82 
83 #[cfg(test)]
84 mod tests {
85 
86     use super::*;
87 
88     #[test]
test_prefixes()89     fn test_prefixes() {
90         assert_eq!(
91             prefixes(".a.b.c.d").collect::<Vec<_>>(),
92             vec![".a.b.c", ".a.b", ".a"],
93         );
94         assert_eq!(prefixes(".a").count(), 0);
95         assert_eq!(prefixes(".").count(), 0);
96     }
97 
98     #[test]
test_suffixes()99     fn test_suffixes() {
100         assert_eq!(
101             suffixes(".a.b.c.d").collect::<Vec<_>>(),
102             vec!["a.b.c.d", "b.c.d", "c.d", "d"],
103         );
104         assert_eq!(suffixes(".a").collect::<Vec<_>>(), vec!["a"]);
105         assert_eq!(suffixes(".").collect::<Vec<_>>(), Vec::<&str>::new());
106     }
107 
108     #[test]
test_path_map_get()109     fn test_path_map_get() {
110         let mut path_map = PathMap::default();
111         path_map.insert(".a.b.c.d".to_owned(), 1);
112         path_map.insert(".a.b".to_owned(), 2);
113         path_map.insert("M1".to_owned(), 3);
114         path_map.insert("M1.M2".to_owned(), 4);
115         path_map.insert("M1.M2.f1".to_owned(), 5);
116         path_map.insert("M1.M2.f2".to_owned(), 6);
117 
118         assert_eq!(None, path_map.get(".a.other"));
119         assert_eq!(None, path_map.get(".a.bother"));
120         assert_eq!(None, path_map.get(".other"));
121         assert_eq!(None, path_map.get(".M1.other"));
122         assert_eq!(None, path_map.get(".M1.M2.other"));
123 
124         assert_eq!(Some(&1), path_map.get(".a.b.c.d"));
125         assert_eq!(Some(&1), path_map.get(".a.b.c.d.other"));
126 
127         assert_eq!(Some(&2), path_map.get(".a.b"));
128         assert_eq!(Some(&2), path_map.get(".a.b.c"));
129         assert_eq!(Some(&2), path_map.get(".a.b.other"));
130         assert_eq!(Some(&2), path_map.get(".a.b.other.Other"));
131         assert_eq!(Some(&2), path_map.get(".a.b.c.dother"));
132 
133         assert_eq!(Some(&3), path_map.get(".M1"));
134         assert_eq!(Some(&3), path_map.get(".a.b.c.d.M1"));
135         assert_eq!(Some(&3), path_map.get(".a.b.M1"));
136 
137         assert_eq!(Some(&4), path_map.get(".M1.M2"));
138         assert_eq!(Some(&4), path_map.get(".a.b.c.d.M1.M2"));
139         assert_eq!(Some(&4), path_map.get(".a.b.M1.M2"));
140 
141         assert_eq!(Some(&5), path_map.get(".M1.M2.f1"));
142         assert_eq!(Some(&5), path_map.get(".a.M1.M2.f1"));
143         assert_eq!(Some(&5), path_map.get(".a.b.M1.M2.f1"));
144 
145         assert_eq!(Some(&6), path_map.get(".M1.M2.f2"));
146         assert_eq!(Some(&6), path_map.get(".a.M1.M2.f2"));
147         assert_eq!(Some(&6), path_map.get(".a.b.M1.M2.f2"));
148 
149         // get_field
150 
151         assert_eq!(Some(&2), path_map.get_field(".a.b.Other", "other"));
152 
153         assert_eq!(Some(&4), path_map.get_field(".M1.M2", "other"));
154         assert_eq!(Some(&4), path_map.get_field(".a.M1.M2", "other"));
155         assert_eq!(Some(&4), path_map.get_field(".a.b.M1.M2", "other"));
156 
157         assert_eq!(Some(&5), path_map.get_field(".M1.M2", "f1"));
158         assert_eq!(Some(&5), path_map.get_field(".a.M1.M2", "f1"));
159         assert_eq!(Some(&5), path_map.get_field(".a.b.M1.M2", "f1"));
160 
161         assert_eq!(Some(&6), path_map.get_field(".M1.M2", "f2"));
162         assert_eq!(Some(&6), path_map.get_field(".a.M1.M2", "f2"));
163         assert_eq!(Some(&6), path_map.get_field(".a.b.M1.M2", "f2"));
164     }
165 }
166