great_tokenizer/
matcher.rs1use std::{any::Any, cell::LazyCell, fmt::{Debug, Display, Formatter}};
2
3use regex::Regex;
4
5pub trait MatcherTrait {
6 fn get(&self, src: &String) -> Option<String>;
7 fn get_at(&self, src: &String, index: usize) -> Option<String>;
8 fn _to_string(&self) -> String;
9}
10
11impl MatcherTrait for String {
12 fn get(&self, src: &String) -> Option<String> {
13 if src.starts_with(self) {
14 Some(self.clone())
15 } else {
16 None
17 }
18 }
19 fn get_at(&self, src: &String, index: usize) -> Option<String> {
20 if let Some(x) = src.match_indices(self).find(|x| x.0 == index) {
21 Some(x.1.to_string())
22 } else {
23 None
24 }
25 }
26 fn _to_string(&self) -> String {
27 self.clone()
28 }
29}
30
31impl MatcherTrait for Vec<String> {
32 fn get(&self, src: &String) -> Option<String> {
33 for v in self {
34 if let Some(x) = v.get(src) {
35 return Some(x);
36 }
37 }
38 None
39 }
40 fn get_at(&self, src: &String, index: usize) -> Option<String> {
41 for v in self {
42 if let Some(x) = v.get_at(src, index) {
43 return Some(x);
44 }
45 }
46 None
47 }
48 fn _to_string(&self) -> String {
49 serde_json::to_string(self).unwrap()
50 }
51}
52
53impl MatcherTrait for str {
54 fn get(&self, src: &String) -> Option<String> {
55 self.to_owned().get(src)
56 }
57 fn get_at(&self, src: &String, index: usize) -> Option<String> {
58 self.to_owned().get_at(src, index)
59 }
60 fn _to_string(&self) -> String {
61 self.to_owned()
62 }
63}
64
65impl MatcherTrait for &str {
66 fn get(&self, src: &String) -> Option<String> {
67 (*self).to_owned().get(src)
68 }
69 fn get_at(&self, src: &String, index: usize) -> Option<String> {
70 (*self).to_owned().get_at(src, index)
71 }
72 fn _to_string(&self) -> String {
73 (*self).to_owned()
74 }
75}
76
77impl<T> MatcherTrait for T
78where
79 T: Fn(&String) -> Option<String>,
80{
81 fn get(&self, src: &String) -> Option<String> {
82 self(src)
83 }
84 fn get_at(&self, src: &String, index: usize) -> Option<String> {
85 self(&src[index..].to_owned())
86 }
87 fn _to_string(&self) -> String {
88 "<Fn>".to_owned()
89 }
90}
91
92impl MatcherTrait for Regex {
93 fn get(&self, src: &String) -> Option<String> {
94 if let Some(s) = self.captures(src.as_str()) {
95 Some(s.get(0).unwrap().as_str().to_string())
96 } else {
97 None
98 }
99 }
100 fn get_at(&self, src: &String, index: usize) -> Option<String> {
101 if let Some(x) = self.captures_at(src.as_str(), index) {
102 Some(x.get(0).unwrap().as_str().to_string())
103 } else {
104 None
105 }
106 }
107 fn _to_string(&self) -> String {
108 self.to_string()
109 }
110}
111
112impl MatcherTrait for LazyCell<Regex> {
113 fn get(&self, src: &String) -> Option<String> {
114 (**self).get(src)
115 }
116 fn get_at(&self, src: &String, index: usize) -> Option<String> {
117 (**self).get_at(src, index)
118 }
119 fn _to_string(&self) -> String {
120 (**self)._to_string()
121 }
122}
123
124pub struct Matcher {
125 inner: Box<dyn MatcherTrait>,
126}
127
128impl Display for Matcher {
129 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
130 write!(f, "{}", self.inner._to_string())
131 }
132}
133
134impl Debug for Matcher {
135 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
136 <Self as Display>::fmt(&self, f)
137 }
138}
139
140impl Matcher {
141 pub fn get<T: ToString>(&self, src: T) -> Option<String> {
142 self.inner.get(&src.to_string())
143 }
144 pub fn get_at<T: ToString>(&self, src: T, index: usize) -> Option<String> {
145 self.inner.get_at(&src.to_string(), index)
146 }
147}
148
149impl<T: MatcherTrait + Any> From<T> for Matcher {
150 fn from(value: T) -> Self {
151 Self {
152 inner: Box::new(value),
153 }
154 }
155}