ferrum_router/recognizer/
glob.rs1use recognizer::types::{GlobTypes, DefaultStore};
2
3#[derive(Default)]
4pub struct Glob<S, T = DefaultStore>
5 where S: AsRef<[u8]>,
6 T: GlobTypes,
7{
8 path: S,
9 types: Option<T>,
10}
11
12impl<S, T> Glob<S, T>
13 where S: AsRef<[u8]>,
14 T: GlobTypes,
15{
16 pub fn new(path: S, types: Option<T>) -> Self {
17 Glob {
18 path,
19 types,
20 }
21 }
22
23 pub fn path(&self) -> &[u8] {
24 self.path.as_ref()
25 }
26
27 pub fn types(&self) -> Option<&T> {
28 self.types.as_ref()
29 }
30}
31
32impl<S> From<S> for Glob<S, DefaultStore>
33 where S: AsRef<[u8]>
34{
35 fn from(path: S) -> Self {
36 Glob::new(path, None)
37 }
38}
39
40impl<S, T> From<(S, T)> for Glob<S, T>
41 where S: AsRef<[u8]>,
42 T: GlobTypes,
43{
44 fn from(pair: (S, T)) -> Self {
45 let (path, types) = pair;
46 Glob::new(path, Some(types))
47 }
48}
49
50
51#[cfg(test)]
52mod tests {
53 use std::borrow::Borrow;
54 use super::*;
55 use recognizer::types::Store;
56
57 fn assert_glob_key<'a, G, S, T>(glob: G, key: <T as GlobTypes>::Name, expected: <T as GlobTypes>::Pattern)
58 where G: Into<Glob<S, T>>,
59 S: AsRef<[u8]> + 'a,
60 T: GlobTypes + 'a,
61 {
62 let glob = glob.into();
63 let types = glob.types().unwrap().store();
64 let value = types.get(key.borrow()).unwrap();
65 assert_eq!(value.as_ref(), expected.as_ref());
66 }
67
68 #[test]
69 fn glob_from() {
70 let glob_str = "path/str";
71 let glob = Glob::from(glob_str);
72 assert_eq!(glob.path(), glob_str.as_bytes());
73 assert!(glob.types().is_none());
74
75 let glob_string = "path/string".to_string();
76 let glob = Glob::from(glob_string.clone());
77 assert_eq!(glob.path(), glob_string.as_bytes());
78 assert!(glob.types().is_none());
79
80 let glob_bytes = "path/bytes".as_bytes();
81 let glob = Glob::from(glob_bytes);
82 assert_eq!(glob.path(), glob_bytes);
83 assert!(glob.types().is_none());
84
85 let glob_vec = "path/vec".as_bytes().to_vec();
86 let glob = Glob::from(glob_vec.clone());
87 assert_eq!(glob.path(), glob_vec.as_slice());
88 assert!(glob.types().is_none());
89
90
91 let types_default = DefaultStore::default();
92 let types_string = Store::<String, String>::default();
93
94 let glob: Glob<_, &DefaultStore> = Glob::from((glob_str, &types_default));
95 assert_eq!(glob.path(), glob_str.as_bytes());
96 assert!(glob.types().is_some());
97 assert_eq!(glob.types().unwrap().store(), &types_default);
98
99 let glob = Glob::from((glob_str, &types_string));
100 assert_eq!(glob.path(), glob_str.as_bytes());
101 assert!(glob.types().is_some());
102 let types = glob.types().unwrap();
103 assert_eq!(types.store(), &types_string);
104
105 let mut types = Store::<&str, String>::default();
106 types.insert("key", "value".to_string());
107 assert_glob_key(("", &types), "key", "value".to_string());
108 assert_glob_key(("", types), "key", "value".to_string());
109 }
110}