Skip to content

Commit 8a940cf

Browse files
committed
feat(parser): add debug operator {!...}
1 parent 9372c87 commit 8a940cf

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

src/pipeline/mod.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub enum RangeSpec {
4444
Range(Option<isize>, Option<isize>, bool), // (start, end, inclusive)
4545
}
4646

47-
pub fn parse_template(template: &str) -> Result<Vec<StringOp>, String> {
47+
pub fn parse_template(template: &str) -> Result<(Vec<StringOp>, bool), String> {
4848
parser::parse_template(template)
4949
}
5050

@@ -150,10 +150,18 @@ fn unescape(s: &str) -> String {
150150
out
151151
}
152152

153-
pub fn apply_ops(input: &str, ops: &[StringOp]) -> Result<String, String> {
153+
pub fn apply_ops(input: &str, ops: &[StringOp], debug: bool) -> Result<String, String> {
154154
let mut val = Value::Str(input.to_string());
155-
let mut default_sep = " ".to_string(); // Clear default
156-
for op in ops {
155+
let mut default_sep = " ".to_string();
156+
157+
if debug {
158+
eprintln!("DEBUG: Initial value: {:?}", val);
159+
}
160+
161+
for (i, op) in ops.iter().enumerate() {
162+
if debug {
163+
eprintln!("DEBUG: Applying operation {}: {:?}", i + 1, op);
164+
}
157165
match op {
158166
StringOp::Split { sep, range } => match &val {
159167
Value::Str(s) => {
@@ -306,6 +314,18 @@ pub fn apply_ops(input: &str, ops: &[StringOp]) -> Result<String, String> {
306314
}
307315
},
308316
}
317+
if debug {
318+
match &val {
319+
Value::Str(s) => eprintln!("DEBUG: Result: String({:?})", s),
320+
Value::List(list) => {
321+
eprintln!("DEBUG: Result: List with {} items:", list.len());
322+
for (idx, item) in list.iter().enumerate() {
323+
eprintln!("DEBUG: [{}]: {:?}", idx, item);
324+
}
325+
}
326+
}
327+
eprintln!("DEBUG: ---");
328+
}
309329
}
310330

311331
// Note: If the final value is a List, we join using the last split separator
@@ -323,8 +343,8 @@ pub fn apply_ops(input: &str, ops: &[StringOp]) -> Result<String, String> {
323343
}
324344

325345
pub fn process(input: &str, template: &str) -> Result<String, String> {
326-
let ops = parse_template(template)?;
327-
apply_ops(input, &ops)
346+
let (ops, debug) = parse_template(template)?;
347+
apply_ops(input, &ops, debug)
328348
}
329349

330350
#[cfg(test)]

src/pipeline/parser.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,30 @@ use super::{RangeSpec, StringOp, unescape};
77
#[grammar = "pipeline/template.pest"]
88
struct TemplateParser;
99

10-
pub fn parse_template(template: &str) -> Result<Vec<StringOp>, String> {
10+
pub fn parse_template(template: &str) -> Result<(Vec<StringOp>, bool), String> {
1111
let pairs = TemplateParser::parse(Rule::template, template)
1212
.map_err(|e| format!("Parse error: {}", e))?
1313
.next()
1414
.unwrap();
1515

1616
let mut ops = Vec::new();
17+
let mut debug = false;
18+
1719
for pair in pairs.into_inner() {
18-
if pair.as_rule() == Rule::operation_list {
19-
for op_pair in pair.into_inner() {
20-
ops.push(parse_operation(op_pair)?);
20+
match pair.as_rule() {
21+
Rule::operation_list => {
22+
for op_pair in pair.into_inner() {
23+
ops.push(parse_operation(op_pair)?);
24+
}
25+
}
26+
Rule::debug_flag => {
27+
debug = true;
2128
}
29+
_ => {}
2230
}
2331
}
24-
Ok(ops)
32+
33+
Ok((ops, debug))
2534
}
2635

2736
fn parse_operation(pair: pest::iterators::Pair<Rule>) -> Result<StringOp, String> {

src/pipeline/template.pest

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
template = { "{" ~ operation_list? ~ "}" }
1+
template = { "{" ~ debug_flag? ~ operation_list? ~ "}" }
2+
3+
debug_flag = { "!" }
24

35
operation_list = { operation ~ ("|" ~ operation)* }
46

0 commit comments

Comments
 (0)