bc-envelope-pattern 0.14.0

Pattern matcher for Gordian Envelope
Documentation
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
use bc_envelope::prelude::*;

use crate::Path;

/// A builder that provides formatting options for each path element.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PathElementFormat {
    /// Summary format, with optional maximum length for truncation.
    Summary(Option<usize>),
    EnvelopeUR,
    DigestUR,
}

impl Default for PathElementFormat {
    fn default() -> Self { PathElementFormat::Summary(None) }
}

/// Options for formatting paths.
#[derive(Debug, Clone)]
pub struct FormatPathsOpts {
    /// Whether to indent each path element.
    /// If true, each element will be indented by 4 spaces per level.
    indent: bool,

    /// Format for each path element.
    /// Default is `PathElementFormat::Summary(None)`.
    element_format: PathElementFormat,

    /// If true, only the last element of each path will be formatted.
    /// This is useful for displaying only the final destination of a path.
    /// If false, all elements will be formatted.
    last_element_only: bool,
}

impl Default for FormatPathsOpts {
    /// Returns the default formatting options:
    /// - `indent`: true
    /// - `element_format`: PathElementFormat::Summary(None)
    /// - `last_element_only`: false
    fn default() -> Self {
        Self {
            indent: true,
            element_format: PathElementFormat::default(),
            last_element_only: false,
        }
    }
}

impl FormatPathsOpts {
    /// Creates a new FormatPathsOpts with default values.
    pub fn new() -> Self { Self::default() }

    /// Sets whether to indent each path element.
    /// If true, each element will be indented by 4 spaces per level.
    pub fn indent(mut self, indent: bool) -> Self {
        self.indent = indent;
        self
    }

    /// Sets the format for each path element.
    /// Default is `PathElementFormat::Summary(None)`.
    pub fn element_format(mut self, format: PathElementFormat) -> Self {
        self.element_format = format;
        self
    }

    /// Sets whether to format only the last element of each path.
    /// If true, only the last element will be formatted.
    /// If false, all elements will be formatted.
    pub fn last_element_only(mut self, last_element_only: bool) -> Self {
        self.last_element_only = last_element_only;
        self
    }
}

impl AsRef<FormatPathsOpts> for FormatPathsOpts {
    fn as_ref(&self) -> &FormatPathsOpts { self }
}

pub fn envelope_summary(env: &Envelope) -> String {
    let id = env.short_id(DigestDisplayFormat::Short);
    let summary = match env.case() {
        EnvelopeCase::Node { .. } => {
            format!("NODE {}", env.format_flat())
        }
        EnvelopeCase::Leaf { cbor, .. } => {
            format!(
                "LEAF {}",
                cbor.envelope_summary(usize::MAX, &FormatContextOpt::default())
                    .unwrap_or_else(|_| "ERROR".to_string())
            )
        }
        EnvelopeCase::Wrapped { .. } => {
            format!("WRAPPED {}", env.format_flat())
        }
        EnvelopeCase::Assertion(_) => {
            format!("ASSERTION {}", env.format_flat())
        }
        EnvelopeCase::Elided(_) => "ELIDED".to_string(),
        EnvelopeCase::KnownValue { value, .. } => {
            let content = with_format_context!(|ctx: &FormatContext| {
                let known_value = KnownValuesStore::known_value_for_raw_value(
                    value.value(),
                    Some(ctx.known_values()),
                );
                format!("'{}'", known_value)
            });
            format!("KNOWN_VALUE {}", content)
        }
        EnvelopeCase::Encrypted(_) => "ENCRYPTED".to_string(),
        EnvelopeCase::Compressed(_) => "COMPRESSED".to_string(),
    };
    format!("{} {}", id, summary)
}

/// Truncates a string to the specified maximum length, appending an ellipsis if
/// truncated. If `max_length` is None, returns the original string.
fn truncate_with_ellipsis(s: &str, max_length: Option<usize>) -> String {
    match max_length {
        Some(max_len) if s.len() > max_len => {
            if max_len > 1 {
                format!("{}", &s[0..(max_len - 1)])
            } else {
                "".to_string()
            }
        }
        _ => s.to_string(),
    }
}

/// Format each path element on its own line, each line successively indented by
/// 4 spaces. Options can be provided to customize the formatting.
pub fn format_path_opt(
    path: &Path,
    opts: impl AsRef<FormatPathsOpts>,
) -> String {
    let opts = opts.as_ref();

    if opts.last_element_only {
        // Only format the last element, no indentation.
        if let Some(element) = path.iter().last() {
            match opts.element_format {
                PathElementFormat::Summary(max_length) => {
                    let summary = envelope_summary(element);
                    truncate_with_ellipsis(&summary, max_length)
                }
                PathElementFormat::EnvelopeUR => element.ur_string(),
                PathElementFormat::DigestUR => element.digest().ur_string(),
            }
        } else {
            String::new()
        }
    } else {
        match opts.element_format {
            PathElementFormat::Summary(max_length) => {
                // Multi-line output with indentation for summaries.
                let mut lines = Vec::new();
                for (index, element) in path.iter().enumerate() {
                    let indent = if opts.indent {
                        " ".repeat(index * 4)
                    } else {
                        String::new()
                    };

                    let summary = envelope_summary(element);
                    let content = truncate_with_ellipsis(&summary, max_length);

                    lines.push(format!("{}{}", indent, content));
                }
                lines.join("\n")
            }
            PathElementFormat::EnvelopeUR => {
                // Single-line, space-separated envelope URs.
                path.iter()
                    .map(|element| element.ur_string())
                    .collect::<Vec<_>>()
                    .join(" ")
            }
            PathElementFormat::DigestUR => {
                // Single-line, space-separated digest URs.
                path.iter()
                    .map(|element| element.digest().ur_string())
                    .collect::<Vec<_>>()
                    .join(" ")
            }
        }
    }
}

/// Format each path element on its own line, each line successively indented by
/// 4 spaces.
pub fn format_path(path: &Path) -> String {
    format_path_opt(path, FormatPathsOpts::default())
}

pub fn format_paths_with_captures(
    paths: &[Path],
    captures: &std::collections::HashMap<String, Vec<Path>>,
) -> String {
    format_paths_with_captures_opt(paths, captures, FormatPathsOpts::default())
}

/// Format multiple paths with captures in a structured way.
/// Captures come first, sorted lexicographically by name, with their name
/// prefixed by '@'. Regular paths follow after all captures.
pub fn format_paths_with_captures_opt(
    paths: &[Path],
    captures: &std::collections::HashMap<String, Vec<Path>>,
    opts: impl AsRef<FormatPathsOpts>,
) -> String {
    let opts = opts.as_ref();
    let mut result = Vec::new();

    // First, format all captures, sorted lexicographically by name
    let mut capture_names: Vec<&String> = captures.keys().collect();
    capture_names.sort();

    for capture_name in capture_names {
        if let Some(capture_paths) = captures.get(capture_name) {
            result.push(format!("@{}", capture_name));
            for path in capture_paths {
                let formatted_path = format_path_opt(path, opts);
                // Add indentation to each line of the formatted path
                for line in formatted_path.split('\n') {
                    if !line.is_empty() {
                        result.push(format!("    {}", line));
                    }
                }
            }
        }
    }

    // Then, format all regular paths
    match opts.element_format {
        PathElementFormat::EnvelopeUR | PathElementFormat::DigestUR => {
            // For UR formats, join paths with spaces on same line
            if !paths.is_empty() {
                let formatted_paths = paths
                    .iter()
                    .map(|path| format_path_opt(path, opts))
                    .collect::<Vec<_>>()
                    .join(" ");
                if !formatted_paths.is_empty() {
                    result.push(formatted_paths);
                }
            }
        }
        PathElementFormat::Summary(_) => {
            // For summary format, format each path separately
            for path in paths {
                let formatted_path = format_path_opt(path, opts);
                for line in formatted_path.split('\n') {
                    if !line.is_empty() {
                        result.push(line.to_string());
                    }
                }
            }
        }
    }

    result.join("\n")
}

/// Format multiple paths with custom formatting options.
pub fn format_paths_opt(
    paths: &[Path],
    opts: impl AsRef<FormatPathsOpts>,
) -> String {
    // Call format_paths_with_captures with empty captures
    format_paths_with_captures_opt(
        paths,
        &std::collections::HashMap::new(),
        opts,
    )
}

/// Format multiple paths with default options.
pub fn format_paths(paths: &[Path]) -> String {
    format_paths_opt(paths, FormatPathsOpts::default())
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use bc_envelope::prelude::*;
    use indoc::indoc;

    use super::*;

    fn create_test_path() -> Path {
        vec![
            Envelope::new(42),
            Envelope::new("test"),
            Envelope::new(vec![1, 2, 3]),
        ]
    }

    #[test]
    fn test_format_path_default() {
        let path = create_test_path();
        let actual = format_path(&path);

        #[rustfmt::skip]
        let expected = indoc! {r#"
            7f83f7bd LEAF 42
                6fe3180f LEAF "test"
                    4abc3113 LEAF [1, 2, 3]
        "#}.trim();

        assert_eq!(actual, expected, "format_path with default options");
    }

    #[test]
    fn test_format_path_last_element_only() {
        let path = create_test_path();
        let opts = FormatPathsOpts::new().last_element_only(true);
        let actual = format_path_opt(&path, opts);

        #[rustfmt::skip]
        let expected = indoc! {r#"
            4abc3113 LEAF [1, 2, 3]
        "#}.trim();

        assert_eq!(actual, expected, "format_path with last_element_only");
    }

    #[test]
    fn test_format_paths_multiple() {
        let path1 = vec![Envelope::new(1)];
        let path2 = vec![Envelope::new(2)];
        let paths = vec![path1, path2];

        let actual = format_paths(&paths);

        #[rustfmt::skip]
        let expected = indoc! {r#"
            4bf5122f LEAF 1
            dbc1b4c9 LEAF 2
        "#}.trim();

        assert_eq!(actual, expected, "format_paths with multiple paths");
    }

    #[test]
    fn test_format_paths_with_captures() {
        let path1 = vec![Envelope::new(1)];
        let path2 = vec![Envelope::new(2)];
        let paths = vec![path1.clone(), path2.clone()];

        let mut captures = HashMap::new();
        captures.insert("capture1".to_string(), vec![path1]);
        captures.insert("capture2".to_string(), vec![path2]);

        let actual = format_paths_with_captures_opt(
            &paths,
            &captures,
            FormatPathsOpts::default(),
        );

        #[rustfmt::skip]
        let expected = indoc! {r#"
            @capture1
                4bf5122f LEAF 1
            @capture2
                dbc1b4c9 LEAF 2
            4bf5122f LEAF 1
            dbc1b4c9 LEAF 2
        "#}.trim();

        assert_eq!(
            actual, expected,
            "format_paths_with_captures with sorted captures"
        );
    }

    #[test]
    fn test_format_paths_with_empty_captures() {
        let path1 = vec![Envelope::new(1)];
        let path2 = vec![Envelope::new(2)];
        let paths = vec![path1, path2];

        let captures = HashMap::new();
        let formatted = format_paths_with_captures_opt(
            &paths,
            &captures,
            FormatPathsOpts::default(),
        );

        // Should be same as format_paths when no captures
        let expected = format_paths(&paths);
        assert_eq!(formatted, expected);
    }

    #[test]
    fn test_capture_names_sorted() {
        let path1 = vec![Envelope::new(1)];
        let path2 = vec![Envelope::new(2)];
        let path3 = vec![Envelope::new(3)];
        let paths = vec![];

        let mut captures = HashMap::new();
        captures.insert("zebra".to_string(), vec![path1]);
        captures.insert("alpha".to_string(), vec![path2]);
        captures.insert("beta".to_string(), vec![path3]);

        let actual = format_paths_with_captures_opt(
            &paths,
            &captures,
            FormatPathsOpts::default(),
        );

        #[rustfmt::skip]
        let expected = indoc! {r#"
            @alpha
                dbc1b4c9 LEAF 2
            @beta
                084fed08 LEAF 3
            @zebra
                4bf5122f LEAF 1
        "#}.trim();

        assert_eq!(
            actual, expected,
            "capture names should be sorted lexicographically"
        );
    }

    #[test]
    fn test_format_paths_with_captures_envelope_ur() {
        bc_components::register_tags();

        let path1 = vec![Envelope::new(1)];
        let path2 = vec![Envelope::new(2)];
        let paths = vec![path1.clone(), path2.clone()];

        let mut captures = HashMap::new();
        captures.insert("capture1".to_string(), vec![path1]);

        let opts = FormatPathsOpts::new()
            .element_format(PathElementFormat::EnvelopeUR);

        let actual = format_paths_with_captures_opt(&paths, &captures, opts);

        // For this test, we need to check the structure but URs are long and
        // variable So we'll verify the structure exists
        assert!(actual.contains("@capture1"));
        assert!(actual.contains("ur:envelope"));

        // Count the number of ur:envelope occurrences (should be 3: 1 capture +
        // 2 regular paths)
        let ur_count = actual.matches("ur:envelope").count();
        assert_eq!(ur_count, 3, "Should have 3 envelope URs total");
    }

    #[test]
    fn test_format_paths_with_captures_digest_ur() {
        bc_components::register_tags();

        let path1 = vec![Envelope::new(1)];
        let path2 = vec![Envelope::new(2)];
        let paths = vec![path1.clone(), path2.clone()];

        let mut captures = HashMap::new();
        captures.insert("capture1".to_string(), vec![path1]);

        let opts =
            FormatPathsOpts::new().element_format(PathElementFormat::DigestUR);

        let actual = format_paths_with_captures_opt(&paths, &captures, opts);

        // For this test, we need to check the structure but digest URs are also
        // variable
        assert!(actual.contains("@capture1"));
        assert!(actual.contains("ur:digest"));

        // Count the number of ur:digest occurrences (should be 3: 1 capture + 2
        // regular paths)
        let ur_count = actual.matches("ur:digest").count();
        assert_eq!(ur_count, 3, "Should have 3 digest URs total");
    }

    #[test]
    fn test_format_paths_with_captures_no_indent() {
        let path1 = vec![Envelope::new(1)];
        let paths = vec![path1.clone()];

        let mut captures = HashMap::new();
        captures.insert("capture1".to_string(), vec![path1]);

        let opts = FormatPathsOpts::new().indent(false);

        let actual = format_paths_with_captures_opt(&paths, &captures, opts);

        #[rustfmt::skip]
        let expected = indoc! {r#"
            @capture1
                4bf5122f LEAF 1
            4bf5122f LEAF 1
        "#}.trim();

        assert_eq!(
            actual, expected,
            "captures should still have fixed indentation even with indent=false"
        );
    }

    #[test]
    fn test_truncate_with_ellipsis() {
        assert_eq!(truncate_with_ellipsis("hello", None), "hello");
        assert_eq!(truncate_with_ellipsis("hello", Some(10)), "hello");
        assert_eq!(truncate_with_ellipsis("hello world", Some(5)), "hell…");
        assert_eq!(truncate_with_ellipsis("hello", Some(1)), "");
    }
}