1#[allow(
27 unused_imports,
28 clippy::wildcard_imports,
29 reason = "Standard workspace module prelude"
30)]
31use crate::utilities::*;
32
33pub mod ast;
34pub mod dc_stream;
35pub mod formatter;
36pub mod parser;
37pub mod validator;
38
39pub use ast::{
40 FormatExpr, FormatOp, MAX_FORMAT_EXPR_DEPTH, MAX_FORMAT_EXPR_NODES,
41 ParamBinding, ParamValue,
42};
43pub use dc_stream::{
44 DcToken, decode_dc_stream, decode_dc_stream_string, encode_dc_stream,
45 encode_dc_stream_string,
46};
47pub use formatter::{format_chain_directive, format_expr};
48pub use parser::parse_format_expr;
49pub use validator::validate_format_expr;
50
51#[cfg(test)]
52#[allow(
53 clippy::panic,
54 clippy::expect_used,
55 clippy::unwrap_used,
56 clippy::unwrap_in_result,
57 clippy::panic_in_result_fn,
58 clippy::indexing_slicing,
59 clippy::arithmetic_side_effects,
60 reason = "Standard repository test boilerplate"
61)]
62mod tests {
63 use super::*;
64 use ctb_storage_minimal::shorthand::DcShorthand;
65
66 #[crate::ctb_test]
67 fn test_parse_mojibake_expression_and_round_trip() {
68 let input = "((f15 > f542) ! f0) > f0";
69 let expr = parse_format_expr(input).unwrap();
70
71 assert_eq!(
72 expr,
73 FormatExpr::Convert(
74 Box::new(FormatExpr::Transmute(
75 Box::new(FormatExpr::Convert(
76 Box::new(FormatExpr::Dc(DcShorthand::Format(15))),
77 Box::new(FormatExpr::Dc(DcShorthand::Format(542))),
78 )),
79 Box::new(FormatExpr::Dc(DcShorthand::Format(0))),
80 )),
81 Box::new(FormatExpr::Dc(DcShorthand::Format(0))),
82 )
83 );
84
85 let formatted = format_expr(&expr);
87 assert_eq!(formatted, "((f15 > f542) ! f0) > f0");
88
89 let dc_string = encode_dc_stream_string(&expr);
91 assert_eq!(dc_string, "302 303 302 f15 f542 f0 f0");
92
93 let decoded = decode_dc_stream_string(&dc_string).unwrap();
95 assert_eq!(decoded, expr);
96 let long_dc_string =
98 "298 302 298 303 298 302 f15 f542 299 f0 299 f0 299";
99 let decoded = decode_dc_stream_string(long_dc_string).unwrap();
100 assert_eq!(decoded, expr);
101 let manually_written_dc_string = "302 303 302 f15 f542 299 f0 299 f0";
102 let decoded =
103 decode_dc_stream_string(manually_written_dc_string).unwrap();
104 assert_eq!(decoded, expr);
105 let manually_written_dc_string = "302 303 302 f15 f542 f0 f0";
107 let decoded =
108 decode_dc_stream_string(manually_written_dc_string).unwrap();
109 assert_eq!(decoded, expr);
110 }
111
112 #[crate::ctb_test]
113 fn test_variable_arity_disambiguation_and_dual_syntax() {
114 let infix = "1 & ((2 | 3 | 4) > 5)";
116 let expr = parse_format_expr(infix).unwrap();
117
118 assert_eq!(
119 expr,
120 FormatExpr::Union(vec![
121 FormatExpr::Dc(DcShorthand::Short(1)),
122 FormatExpr::Convert(
123 Box::new(FormatExpr::Intersection(vec![
124 FormatExpr::Dc(DcShorthand::Short(2)),
125 FormatExpr::Dc(DcShorthand::Short(3)),
126 FormatExpr::Dc(DcShorthand::Short(4)),
127 ])),
128 Box::new(FormatExpr::Dc(DcShorthand::Short(5))),
129 ),
130 ])
131 );
132
133 let dc_string = encode_dc_stream_string(&expr);
135 assert_eq!(dc_string, "300 1 302 516 2 3 4 299 5");
136
137 let decoded = decode_dc_stream_string(&dc_string).unwrap();
139 assert_eq!(decoded, expr);
140
141 let decoded_sym = decode_dc_stream_string("& 1 > | 2 3 4 ) 5").unwrap();
143 assert_eq!(decoded_sym, expr);
144
145 let expr_from_prefix = parse_format_expr("& 1 > | 2 3 4 ) 5").unwrap();
147 assert_eq!(expr_from_prefix, expr);
148
149 let tail_infix = "5 > (2 | 3 | 4)";
151 let tail_expr = parse_format_expr(tail_infix).unwrap();
152 let tail_dc_string = encode_dc_stream_string(&tail_expr);
153 assert_eq!(tail_dc_string, "302 5 516 2 3 4");
154
155 let tail_from_prefix = parse_format_expr("> 5 | 2 3 4").unwrap();
156 assert_eq!(tail_from_prefix, tail_expr);
157 }
158
159 #[crate::ctb_test]
160 fn test_chain_directive_parsing_and_formatting() {
161 let directive = "@chain(((f15 > f542) ! f0) > f0)";
162 let expr = parse_format_expr(directive).unwrap();
163 assert_eq!(
164 format_chain_directive(&expr),
165 "@chain(((f15 > f542) ! f0) > f0)"
166 );
167 }
168
169 #[crate::ctb_test]
170 fn test_left_associative_operational_operators() {
171 let input = "f10 > f20 > f30";
172 let expr = parse_format_expr(input).unwrap();
173 assert_eq!(
174 expr,
175 FormatExpr::Convert(
176 Box::new(FormatExpr::Convert(
177 Box::new(FormatExpr::Dc(DcShorthand::Format(10))),
178 Box::new(FormatExpr::Dc(DcShorthand::Format(20))),
179 )),
180 Box::new(FormatExpr::Dc(DcShorthand::Format(30))),
181 )
182 );
183 assert_eq!(format_expr(&expr), "f10 > f20 > f30");
184 }
185
186 #[crate::ctb_test]
187 fn test_strict_disambiguation_rules() {
188 parse_format_expr("f10 & f20 | f30").unwrap_err();
190 parse_format_expr("(f10 & f20) | f30").unwrap();
191 parse_format_expr("f10 & (f20 | f30)").unwrap();
192
193 parse_format_expr("f10 & f20 > f30").unwrap_err();
195 parse_format_expr("f10 > f20 & f30").unwrap_err();
196 parse_format_expr("f10 & (f20 > f30)").unwrap();
197 parse_format_expr("(f10 > f20) & f30").unwrap();
198
199 parse_format_expr("f10 & f20 & f30").unwrap();
201 parse_format_expr("f10 | f20 | f30").unwrap();
202 }
203
204 #[crate::ctb_test]
205 fn test_validator_registered_named_types_and_shorthands() {
206 let valid_named = parse_format_expr("f0 > string").unwrap();
208 validate_format_expr(&valid_named).unwrap();
209
210 let invalid_nick = parse_format_expr("f0 > utf8").unwrap();
212 assert!(validate_format_expr(&invalid_nick).is_err());
213
214 let valid_trans = parse_format_expr("f10 : f323").unwrap();
216 validate_format_expr(&valid_trans).unwrap();
217
218 let invalid_trans = parse_format_expr("f10 : f0").unwrap();
220 assert!(validate_format_expr(&invalid_trans).is_err());
221 }
222
223 #[crate::ctb_test]
224 fn test_stream_decode_round_trip_for_composite_trees() {
225 let input = "f10 & (f20 | (f30 > f40))";
226 let expr = parse_format_expr(input).unwrap();
227 let dc_tokens = encode_dc_stream_string(&expr);
228 let decoded = decode_dc_stream_string(&dc_tokens).unwrap();
229 assert_eq!(decoded, expr);
230 assert_eq!(format_expr(&decoded), input);
231 }
232
233 #[crate::ctb_test]
234 fn test_parametric_application_parsing_and_formatting() {
235 let expr1 = parse_format_expr("f350(radix=16)").unwrap();
237 assert_eq!(
238 expr1,
239 FormatExpr::Apply {
240 target: Box::new(FormatExpr::Dc(DcShorthand::Format(350))),
241 params: vec![ParamBinding {
242 name: "radix".to_string(),
243 value: ParamValue::Integer(16),
244 }],
245 }
246 );
247 assert_eq!(format_expr(&expr1), "f350(radix=16)");
248
249 let expr2 = parse_format_expr("f350(radix=16, alphabet=f359)").unwrap();
251 assert_eq!(
252 expr2,
253 FormatExpr::Apply {
254 target: Box::new(FormatExpr::Dc(DcShorthand::Format(350))),
255 params: vec![
256 ParamBinding {
257 name: "radix".to_string(),
258 value: ParamValue::Integer(16),
259 },
260 ParamBinding {
261 name: "alphabet".to_string(),
262 value: ParamValue::Dc(DcShorthand::Format(359)),
263 },
264 ],
265 }
266 );
267 assert_eq!(format_expr(&expr2), "f350(radix=16, alphabet=f359)");
268
269 let expr3 = parse_format_expr("@chain(f350(radix=16) > f0)").unwrap();
271 assert_eq!(
272 format_chain_directive(&expr3),
273 "@chain(f350(radix=16) > f0)"
274 );
275
276 let err = parse_format_expr("BaseNNumeral(radix=16)");
278 assert!(err.is_err());
279 let err_msg = err.unwrap_err().to_string();
280 assert!(
281 err_msg.contains(
282 "Identifiers are not permitted as application targets"
283 )
284 );
285 }
286
287 #[crate::ctb_test]
288 fn test_parametric_application_dc_stream() {
289 let expr = parse_format_expr("f350(radix=16)").unwrap();
290 let dc_stream = encode_dc_stream_string(&expr);
291 assert_eq!(dc_stream, "535 f350 258 radix 16 259");
292
293 let decoded = decode_dc_stream_string(&dc_stream).unwrap();
294 assert_eq!(decoded, expr);
295
296 let compound = parse_format_expr("f350(radix=16) > f0").unwrap();
298 let comp_stream = encode_dc_stream_string(&compound);
299 assert_eq!(comp_stream, "302 535 f350 258 radix 16 259 f0");
300 let comp_decoded = decode_dc_stream_string(&comp_stream).unwrap();
301 assert_eq!(comp_decoded, compound);
302
303 let nested_intersection =
305 parse_format_expr("(f355 | f350(radix=16)) > f0").unwrap();
306 let intersection_stream = encode_dc_stream_string(&nested_intersection);
307 assert_eq!(
308 intersection_stream,
309 "302 516 f355 535 f350 258 radix 16 259 299 f0"
310 );
311 let intersection_decoded =
312 decode_dc_stream_string(&intersection_stream).unwrap();
313 assert_eq!(intersection_decoded, nested_intersection);
314 }
315
316 #[crate::ctb_test]
317 fn test_parametric_application_validation() {
318 let valid = parse_format_expr("f350(radix=16)").unwrap();
320 validate_format_expr(&valid).unwrap();
321
322 let valid_base_ref = parse_format_expr("f350(radix=f354)").unwrap();
323 validate_format_expr(&valid_base_ref).unwrap();
324
325 let invalid_radix = parse_format_expr("f350(radix=0)").unwrap();
327 let err = validate_format_expr(&invalid_radix);
328 assert!(err.is_err());
329 assert!(
330 err.unwrap_err()
331 .to_string()
332 .contains("Radix parameter must be greater than 0")
333 );
334
335 let dup = FormatExpr::Apply {
337 target: Box::new(FormatExpr::Dc(DcShorthand::Format(350))),
338 params: vec![
339 ParamBinding {
340 name: "radix".to_string(),
341 value: ParamValue::Integer(16),
342 },
343 ParamBinding {
344 name: "radix".to_string(),
345 value: ParamValue::Integer(2),
346 },
347 ],
348 };
349 let err_dup = validate_format_expr(&dup);
350 assert!(err_dup.is_err());
351 assert!(
352 err_dup
353 .unwrap_err()
354 .to_string()
355 .contains("Duplicate parameter 'radix'")
356 );
357 }
358}