1#[cfg(feature = "strings")]
46pub mod strings;
47
48#[cfg(feature = "lists")]
49pub mod lists;
50
51#[cfg(feature = "sets")]
52pub mod sets;
53
54#[cfg(feature = "regex_funcs")]
55pub mod regex_funcs;
56
57#[cfg(feature = "urls")]
58pub mod urls;
59
60#[cfg(feature = "ip")]
61pub mod ip;
62
63#[cfg(feature = "semver_funcs")]
64pub mod semver_funcs;
65
66#[cfg(feature = "format")]
67pub mod format;
68
69#[cfg(feature = "quantity")]
70pub mod quantity;
71
72#[cfg(feature = "jsonpatch")]
73pub mod jsonpatch;
74
75#[cfg(feature = "named_format")]
76pub mod named_format;
77
78#[cfg(feature = "validation")]
79pub mod escaping;
80
81#[cfg(feature = "validation")]
82pub mod values;
83
84#[cfg(feature = "validation")]
85pub mod compilation;
86
87#[cfg(feature = "validation")]
88pub mod validation;
89
90mod dispatch;
91
92pub fn register_all(ctx: &mut cel::Context<'_>) {
94 #[cfg(feature = "strings")]
95 strings::register(ctx);
96
97 #[cfg(feature = "lists")]
98 lists::register(ctx);
99
100 #[cfg(feature = "sets")]
101 sets::register(ctx);
102
103 #[cfg(feature = "regex_funcs")]
104 regex_funcs::register(ctx);
105
106 #[cfg(feature = "urls")]
107 urls::register(ctx);
108
109 #[cfg(feature = "ip")]
110 ip::register(ctx);
111
112 #[cfg(feature = "semver_funcs")]
113 semver_funcs::register(ctx);
114
115 #[cfg(feature = "format")]
116 format::register(ctx);
117
118 #[cfg(feature = "quantity")]
119 quantity::register(ctx);
120
121 #[cfg(feature = "jsonpatch")]
122 jsonpatch::register(ctx);
123
124 #[cfg(feature = "named_format")]
125 named_format::register(ctx);
126
127 dispatch::register(ctx);
129}
130
131#[cfg(test)]
132mod tests {
133 use super::*;
134
135 #[allow(unused_imports)]
136 use std::sync::Arc;
137
138 use cel::{Context, Program, Value};
139
140 #[allow(dead_code)]
141 fn eval(expr: &str) -> Value {
142 let mut ctx = Context::default();
143 register_all(&mut ctx);
144 Program::compile(expr).unwrap().execute(&ctx).unwrap()
145 }
146
147 #[test]
148 #[cfg(feature = "strings")]
149 fn test_integration_strings() {
150 assert_eq!(
151 eval("'hello'.charAt(1)"),
152 Value::String(Arc::new("e".into()))
153 );
154 assert_eq!(
155 eval("'HELLO'.lowerAscii()"),
156 Value::String(Arc::new("hello".into()))
157 );
158 assert_eq!(
159 eval("' hello '.trim()"),
160 Value::String(Arc::new("hello".into()))
161 );
162 }
163
164 #[test]
165 #[cfg(feature = "lists")]
166 fn test_integration_lists() {
167 assert_eq!(eval("[1, 2, 3].isSorted()"), Value::Bool(true));
168 assert_eq!(eval("[3, 1, 2].isSorted()"), Value::Bool(false));
169 assert_eq!(eval("[1, 2, 3].sum()"), Value::Int(6));
170 }
171
172 #[test]
173 #[cfg(feature = "sets")]
174 fn test_integration_sets() {
175 assert_eq!(eval("sets.contains([1, 2, 3], [1, 2])"), Value::Bool(true));
176 assert_eq!(eval("sets.intersects([1, 2], [2, 3])"), Value::Bool(true));
177 }
178
179 #[test]
180 #[cfg(feature = "regex_funcs")]
181 fn test_integration_regex() {
182 assert_eq!(
183 eval("'hello world'.find('[a-z]+')"),
184 Value::String(Arc::new("hello".into()))
185 );
186 }
187
188 #[test]
189 #[cfg(feature = "strings")]
190 fn test_dispatch_index_of_string() {
191 assert_eq!(eval("'hello world'.indexOf('world')"), Value::Int(6));
192 assert_eq!(eval("'hello'.indexOf('x')"), Value::Int(-1));
193 }
194
195 #[test]
196 #[cfg(feature = "lists")]
197 fn test_dispatch_index_of_list() {
198 assert_eq!(eval("[1, 2, 3].indexOf(2)"), Value::Int(1));
199 assert_eq!(eval("[1, 2, 3].indexOf(4)"), Value::Int(-1));
200 }
201
202 #[test]
203 #[cfg(feature = "strings")]
204 fn test_dispatch_last_index_of_string() {
205 assert_eq!(eval("'abcabc'.lastIndexOf('abc')"), Value::Int(3));
206 }
207
208 #[test]
209 #[cfg(feature = "lists")]
210 fn test_dispatch_last_index_of_list() {
211 assert_eq!(eval("[1, 2, 3, 2].lastIndexOf(2)"), Value::Int(3));
212 }
213
214 #[test]
215 #[cfg(feature = "format")]
216 fn test_integration_format() {
217 assert_eq!(
218 eval("'hello %s'.format(['world'])"),
219 Value::String(Arc::new("hello world".into()))
220 );
221 assert_eq!(
222 eval("'%d items'.format([5])"),
223 Value::String(Arc::new("5 items".into()))
224 );
225 }
226
227 #[test]
228 #[cfg(feature = "semver_funcs")]
229 fn test_integration_semver() {
230 assert_eq!(eval("isSemver('1.2.3')"), Value::Bool(true));
231 assert_eq!(eval("semver('1.2.3').major()"), Value::Int(1));
232 assert_eq!(
233 eval("semver('2.0.0').isGreaterThan(semver('1.0.0'))"),
234 Value::Bool(true)
235 );
236 }
237}