1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! Debug context for pipeline operations.
//!
//! This module contains the debug context implementation that provides
//! detailed logging and tracing capabilities for pipeline execution.
use crate::pipeline::{REGEX_CACHE, SPLIT_CACHE, StringOp, Value};
use std::time::Duration;
/// Debug tracer that provides hierarchical execution logging for pipeline operations.
///
/// The `DebugTracer` outputs detailed information about pipeline execution including
/// operation timing, input/output values, cache statistics, and hierarchical structure
/// visualization. It supports both main pipeline and sub-pipeline tracing with
/// appropriate indentation levels.
#[derive(Clone)]
pub struct DebugTracer {
enabled: bool,
is_sub_pipeline: bool,
}
impl DebugTracer {
/// Creates a new debug tracer.
///
/// # Arguments
///
/// * `enabled` - Whether debug output should be generated
pub fn new(enabled: bool) -> Self {
Self {
enabled,
is_sub_pipeline: false,
}
}
/// Creates a debug tracer for sub-pipeline operations.
///
/// Sub-pipeline tracers use deeper indentation levels and different
/// visual markers to distinguish nested operations from main pipeline operations.
///
/// # Arguments
///
/// * `enabled` - Whether debug output should be generated
pub fn sub_pipeline(enabled: bool) -> Self {
Self {
enabled,
is_sub_pipeline: true,
}
}
/// Logs the start of a session (template or template processing).
///
/// This marks the beginning of a complete processing session, showing
/// the session type, template string, input data, and optional additional information.
///
/// # Arguments
///
/// * `session_type` - Type of session (e.g., "SINGLE TEMPLATE", "MULTI TEMPLATE")
/// * `template` - The template string being processed
/// * `input` - The input data to be processed
/// * `info` - Optional additional information to display
pub fn session_start(
&self,
session_type: &str,
template: &str,
input: &str,
info: Option<&str>,
) {
if !self.enabled {
return;
}
self.line(format!("📂 {session_type}"));
self.line_with_prefix(format!("🏁 {session_type} START"), 1);
self.line_with_prefix(format!("Template: {template:?}"), 1);
self.line_with_prefix(format!("➡️ Input: {input:?}"), 1);
if let Some(info) = info {
self.line_with_prefix(info.to_string(), 1);
}
self.separator();
}
/// Logs the end of a session with results and timing information.
///
/// This marks the completion of a processing session, showing the final result,
/// execution time, and cache statistics.
///
/// # Arguments
///
/// * `session_type` - Type of session that completed
/// * `result` - The final processed result
/// * `elapsed` - Total execution time for the session
pub fn session_end(&self, session_type: &str, result: &str, elapsed: Duration) {
if !self.enabled {
return;
}
self.line_with_prefix(format!("🏁 ✅ {session_type} COMPLETE"), 1);
self.line_with_prefix(format!("🎯 Final result: {result:?}"), 1);
self.line_with_prefix(format!("Total execution time: {elapsed:?}"), 1);
self.line_with_ending_prefix(
format!(
"Cache stats: {} regex patterns, {} split operations cached",
REGEX_CACHE.len(),
SPLIT_CACHE.len()
),
1,
);
}
/// Logs the start of pipeline execution.
///
/// This shows the beginning of a pipeline (main or sub-pipeline) with the
/// operations to be executed and the initial input value.
///
/// # Arguments
///
/// * `ops` - The sequence of operations to be executed
/// * `input` - The initial input value for the pipeline
pub fn pipeline_start(&self, ops: &[StringOp], input: &Value) {
if !self.enabled {
return;
}
let depth = if self.is_sub_pipeline { 4 } else { 1 };
let icon = if self.is_sub_pipeline { "🔧" } else { "🚀" };
let label = if self.is_sub_pipeline {
"SUB-PIPELINE"
} else {
"PIPELINE"
};
self.line_with_prefix(
format!(
"📂 {}",
if self.is_sub_pipeline {
"Sub-Pipeline"
} else {
"Main Pipeline"
}
),
depth,
);
self.line_with_prefix(
format!("{} {} START: {} operations", icon, label, ops.len()),
depth + 1,
);
self.line_with_prefix(
format!("➡️ Input: {}", Self::format_value(input)),
depth + 1,
);
if ops.len() > 1 {
for (i, op) in ops.iter().enumerate() {
self.line_with_prefix(
format!("{}. {}", i + 1, Self::format_operation(op)),
depth + 1,
);
}
}
}
/// Logs the end of pipeline execution with results and timing.
///
/// This shows the completion of a pipeline with the final result and execution time.
///
/// # Arguments
///
/// * `result` - The final result value from the pipeline
/// * `elapsed` - Total execution time for the pipeline
pub fn pipeline_end(&self, result: &Value, elapsed: Duration) {
if !self.enabled {
return;
}
let depth = if self.is_sub_pipeline { 4 } else { 1 };
let label = if self.is_sub_pipeline {
"SUB-PIPELINE"
} else {
"PIPELINE"
};
self.line_with_prefix(format!("✅ {label} COMPLETE"), depth + 1);
self.line_with_prefix(
format!("🎯 Result: {}", Self::format_value(result)),
depth + 1,
);
self.line_with_ending_prefix(format!("Time: {elapsed:?}"), depth + 1);
if !self.is_sub_pipeline {
self.separator();
}
}
/// Logs an individual operation step with input, output, and timing.
///
/// This provides detailed information about each operation in the pipeline,
/// including the operation type, input value, result value, and execution time.
///
/// # Arguments
///
/// * `step` - The current step number (1-based)
/// * `_total` - The total number of steps (currently unused)
/// * `op` - The operation being executed
/// * `input` - The input value for this operation
/// * `result` - The result value from this operation
/// * `elapsed` - Execution time for this operation
pub fn operation_step(
&self,
step: usize,
_total: usize,
op: &StringOp,
input: &Value,
result: &Value,
elapsed: Duration,
) {
if !self.enabled {
return;
}
let depth = if self.is_sub_pipeline { 5 } else { 2 };
self.line_with_prefix(
format!("⚙️ Step {}: {}", step, Self::format_operation_name(op)),
depth,
);
self.line_with_prefix(
format!("➡️ Input: {}", Self::format_value(input)),
depth + 1,
);
self.line_with_prefix(
format!("🎯 Result: {}", Self::format_value(result)),
depth + 1,
);
self.line_with_ending_prefix(format!("Time: {elapsed:?}"), depth + 1);
}
/// Logs the start of processing a map operation item.
///
/// This shows when a map operation begins processing an individual item,
/// including the item index and input value.
///
/// # Arguments
///
/// * `item_idx` - The current item index (1-based)
/// * `total_items` - The total number of items being processed
/// * `input` - The input string for this item
pub fn map_item_start(&self, item_idx: usize, total_items: usize, input: &str) {
if !self.enabled {
return;
}
self.line_with_prefix(format!("🗂️ Item {item_idx}/{total_items}"), 3);
self.line_with_prefix(format!("➡️ Input: {input:?}"), 4);
}
/// Logs the end of processing a map operation item.
///
/// This shows the completion of processing an individual item in a map operation,
/// with either the successful result or error information.
///
/// # Arguments
///
/// * `output` - The result of processing the item, either success or error
pub fn map_item_end(&self, output: Result<&str, &str>) {
if !self.enabled {
return;
}
match output {
Ok(result) => self.line_with_ending_prefix(format!("Output: {result:?}"), 4),
Err(error) => self.line_with_ending_prefix(format!("❌ ERROR: {error}"), 4),
}
}
/// Logs the completion of a map operation with item counts.
///
/// This shows the final statistics for a map operation, including how many
/// items were processed and how many results were produced.
///
/// # Arguments
///
/// * `input_count` - Number of input items processed
/// * `output_count` - Number of output items produced
pub fn map_complete(&self, input_count: usize, output_count: usize) {
if !self.enabled {
return;
}
self.line_with_ending_prefix(
format!("📦 MAP COMPLETED: {input_count} → {output_count} items"),
3,
);
}
/// Logs cache-related operations.
///
/// This provides information about cache hits, misses, and other cache-related
/// operations that occur during pipeline execution.
///
/// # Arguments
///
/// * `operation` - The type of cache operation (e.g., "HIT", "MISS", "STORE")
/// * `details` - Additional details about the cache operation
pub fn cache_operation(&self, operation: &str, details: &str) {
if !self.enabled {
return;
}
self.line_with_prefix(format!("💾 {operation} {details}"), 1);
self.separator();
}
/// Logs section processing information for template operations.
///
/// This shows progress through different sections of a template,
/// including section type and content information.
///
/// # Arguments
///
/// * `section_num` - Current section number (1-based)
/// * `total_sections` - Total number of sections
/// * `section_type` - Type of section ("LITERAL" or "TEMPLATE")
/// * `content` - Content or description of the section
pub fn section(
&self,
section_num: usize,
total_sections: usize,
section_type: &str,
content: &str,
) {
if !self.enabled {
return;
}
self.line_with_prefix(
format!(
"📊 SECTION {section_num}/{total_sections}: [{section_type}{}]",
if content.is_empty() {
String::new()
} else {
format!(": {content}")
}
),
1,
);
}
// PRIVATE HELPERS
/// Outputs a debug line without indentation prefix.
fn line(&self, msg: String) {
eprintln!("DEBUG: {msg}");
}
/// Outputs a debug line with hierarchical indentation prefix.
///
/// Uses tree-like prefixes to show the hierarchical structure of operations.
///
/// # Arguments
///
/// * `msg` - The message to output
/// * `depth` - The indentation depth level
fn line_with_prefix(&self, msg: String, depth: usize) {
let prefix = match depth {
1 => "├── ".to_string(),
2 => "│ ├── ".to_string(),
3 => "│ │ ├── ".to_string(),
4 => "│ │ │ ├── ".to_string(),
5 => "│ │ │ │ ├── ".to_string(),
6 => "│ │ │ │ │ ├── ".to_string(),
_ => "│ ".repeat(depth.saturating_sub(1)) + "├── ",
};
eprintln!("DEBUG: {prefix}{msg}");
}
/// Outputs a debug line with ending hierarchical prefix.
///
/// Uses terminal tree prefixes (`└──`) to indicate the end of a section.
///
/// # Arguments
///
/// * `msg` - The message to output
/// * `depth` - The indentation depth level
fn line_with_ending_prefix(&self, msg: String, depth: usize) {
let prefix = match depth {
1 => "└── ".to_string(),
2 => "│ └── ".to_string(),
3 => "│ │ └── ".to_string(),
4 => "│ │ │ └── ".to_string(),
5 => "│ │ │ │ └── ".to_string(),
6 => "│ │ │ │ │ └── ".to_string(),
_ => "│ ".repeat(depth.saturating_sub(1)) + "└── ",
};
eprintln!("DEBUG: {prefix}{msg}");
}
/// Outputs a visual separator line.
pub fn separator(&self) {
eprintln!("DEBUG: │");
}
/// Formats a value for display in debug output.
///
/// Provides compact, readable representations of values with length limits
/// for large strings and lists.
///
/// # Arguments
///
/// * `val` - The value to format
///
/// # Returns
///
/// A formatted string representation of the value
fn format_value(val: &Value) -> String {
match val {
Value::Str(s) => {
if s.len() > 40 {
format!("String({}..)", &s[..40])
} else {
format!("String({s})")
}
}
Value::List(list) => {
if list.is_empty() {
"List(empty)".to_string()
} else if list.len() <= 3 {
format!("List{list:?}")
} else {
format!("List[{}, {}, ...+{}]", list[0], list[1], list.len() - 2)
}
}
}
}
/// Formats a string operation for display with key parameters.
///
/// Provides informative representations of operations including important
/// parameters like separators and operation counts.
fn format_operation(op: &StringOp) -> String {
match op {
StringOp::Split { sep, .. } => format!("Split('{sep}')"),
StringOp::Join { sep } => format!("Join('{sep}')"),
StringOp::Map { operations } => format!("Map({})", operations.len()),
_ => Self::format_operation_name(op),
}
}
/// Returns the simple name of a string operation without parameters.
fn format_operation_name(op: &StringOp) -> String {
match op {
StringOp::Split { .. } => "Split".to_string(),
StringOp::Join { .. } => "Join".to_string(),
StringOp::Map { .. } => "Map".to_string(),
StringOp::Upper => "Upper".to_string(),
StringOp::Lower => "Lower".to_string(),
StringOp::Trim { .. } => "Trim".to_string(),
StringOp::Replace { .. } => "Replace".to_string(),
StringOp::Filter { .. } => "Filter".to_string(),
StringOp::FilterNot { .. } => "FilterNot".to_string(),
StringOp::Sort { .. } => "Sort".to_string(),
StringOp::Reverse => "Reverse".to_string(),
StringOp::Unique => "Unique".to_string(),
StringOp::Substring { .. } => "Substring".to_string(),
StringOp::Append { .. } => "Append".to_string(),
StringOp::Prepend { .. } => "Prepend".to_string(),
StringOp::Surround { .. } => "Surround".to_string(),
StringOp::Pad { .. } => "Pad".to_string(),
StringOp::RegexExtract { .. } => "RegexExtract".to_string(),
StringOp::Slice { .. } => "Slice".to_string(),
StringOp::StripAnsi => "StripAnsi".to_string(),
}
}
}