mz_expr_parser/
command.rs1use mz_repr::explain::ExplainConfig;
13
14use crate::{Def, TestCatalog, try_parse_def, try_parse_mir};
15
16pub fn handle_define(catalog: &mut TestCatalog, input: &str) -> String {
17 let result = match try_parse_def(catalog, input) {
19 Ok(def) => match def {
20 Def::Source { name, cols, typ } => match catalog.insert(&name, cols, typ, true) {
21 Ok(id) => format!("Source defined as {id}"),
22 Err(err) => err,
23 },
24 },
25 Err(err) => err,
26 };
27 result + "\n"
28}
29
30pub fn handle_roundtrip(catalog: &TestCatalog, input: &str) -> String {
31 let output = match try_parse_mir(catalog, input) {
32 Ok(expr) => expr.explain(&ExplainConfig::default(), Some(catalog)),
33 Err(err) => return err,
34 };
35
36 if strip_comments(input).trim() == output.trim() {
37 "roundtrip OK\n".to_string()
38 } else {
39 format!(
40 "roundtrip produced a different output:\n~~~ expected:\n{}\n~~~ actual:\n{}\n~~~\n",
41 strip_comments(input),
42 output
43 )
44 }
45}
46
47fn strip_comments(s: &str) -> String {
48 let mut r = "".to_string();
49 for line in s.lines() {
50 let len = line.find("// {").unwrap_or(line.len());
51 r.push_str(line[0..len].trim_end());
52 r.push_str("\n");
53 }
54 r
55}