bc-envelope 0.43.0

Gordian Envelope for Rust.
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//! Formats an envelope as a CBOR data structure and provides human-readable
//! text representations.
//!
//! This module provides functionality for formatting envelopes in various ways:
//!
//! 1. **Envelope Notation**: A human-readable text representation showing the
//!    semantic structure of an envelope (the `format` methods)
//!
//! 2. **CBOR Diagnostics**: Text representation of the underlying CBOR structure, following
//!    [RFC-8949 ยง8](https://www.rfc-editor.org/rfc/rfc8949.html#name-diagnostic-notation)
//!    (the `diagnostic` methods)
//!
//! 3. **CBOR Hex**: Hexadecimal representation of the raw CBOR bytes (the `hex`
//!    methods)
//!
//! All of these formats can be used for debugging, visualization, and
//! understanding the structure of envelopes.
//!
//! # Examples
//!
//! ```
//! use bc_envelope::prelude::*;
//!
//! // Create a simple envelope with assertions
//! let envelope = Envelope::new("Alice")
//!     .add_assertion("knows", "Bob")
//!     .add_assertion("knows", "Carol");
//!
//! // Format the envelope as human-readable envelope notation
//! let formatted = envelope.format();
//! // Will output: "Alice" [ "knows": "Bob", "knows": "Carol" ]
//!
//! // Get the CBOR diagnostic notation (with annotations)
//! let diagnostic = envelope.diagnostic_annotated();
//!
//! // Get the CBOR hex representation
//! let hex = envelope.hex();
//! ```

use bc_components::XID;
use dcbor::prelude::*;

use super::EnvelopeSummary;
use crate::{
    Assertion, Envelope, FormatContextOpt, base::envelope::EnvelopeCase,
    string_utils::StringUtils,
};
#[cfg(feature = "known_value")]
use crate::{KnownValue, known_values};

#[derive(Clone, Default)]
pub struct EnvelopeFormatOpts<'a> {
    flat: bool,
    context: FormatContextOpt<'a>,
}

impl<'a> EnvelopeFormatOpts<'a> {
    /// Sets the flat format option.
    pub fn flat(mut self, flat: bool) -> Self {
        self.flat = flat;
        self
    }

    /// Sets the format context option.
    pub fn context(mut self, context: FormatContextOpt<'a>) -> Self {
        self.context = context;
        self
    }
}

/// Support for the various text output formats for ``Envelope``.
impl Envelope {
    /// Returns the envelope notation for this envelope.
    pub fn format_opt(&self, opts: &EnvelopeFormatOpts<'_>) -> String {
        self.format_item(opts).format(opts).trim().to_string()
    }

    /// Returns the envelope notation for this envelope.
    ///
    /// Uses the current format context.
    pub fn format(&self) -> String {
        self.format_opt(&EnvelopeFormatOpts::default())
    }

    /// Returns the envelope notation for this envelope in flat format.
    ///
    /// In flat format, the envelope is printed on a single line.
    pub fn format_flat(&self) -> String {
        self.format_opt(&EnvelopeFormatOpts::default().flat(true))
    }
}

/// Implementers of this trait define how to be formatted in when output in
/// envelope notation.
pub trait EnvelopeFormat {
    fn format_item(&self, opts: &EnvelopeFormatOpts<'_>) -> EnvelopeFormatItem;
}

/// This type is returned by implementers of the [`EnvelopeFormat`] trait.
#[derive(Debug, Clone, Eq)]
pub enum EnvelopeFormatItem {
    Begin(String),
    End(String),
    Item(String),
    Separator,
    List(Vec<EnvelopeFormatItem>),
}

impl EnvelopeFormatItem {
    fn flatten(&self) -> Vec<EnvelopeFormatItem> {
        match self {
            EnvelopeFormatItem::List(items) => {
                items.iter().flat_map(|i| i.flatten()).collect()
            }
            _ => vec![self.clone()],
        }
    }

    fn nicen(items: &[EnvelopeFormatItem]) -> Vec<EnvelopeFormatItem> {
        let mut input = items.to_vec();
        let mut result: Vec<EnvelopeFormatItem> = vec![];

        while !input.is_empty() {
            let current = input.remove(0);
            if input.is_empty() {
                result.push(current);
                break;
            }
            if let EnvelopeFormatItem::End(end_string) = current.clone() {
                if let EnvelopeFormatItem::Begin(begin_string) =
                    input[0].clone()
                {
                    result.push(EnvelopeFormatItem::End(format!(
                        "{} {}",
                        end_string, begin_string
                    )));
                    result.push(EnvelopeFormatItem::Begin("".to_string()));
                    input.remove(0);
                } else {
                    result.push(current);
                }
            } else {
                result.push(current);
            }
        }

        result
    }

    fn indent(level: usize) -> String { " ".repeat(level * 4) }

    fn add_space_at_end_if_needed(s: &str) -> String {
        if s.is_empty() {
            " ".to_string()
        } else if s.ends_with(' ') {
            s.to_string()
        } else {
            s.to_string() + " "
        }
    }

    fn format(&self, opts: &EnvelopeFormatOpts<'_>) -> String {
        if opts.flat {
            return self.format_flat();
        }
        self.format_hierarchical()
    }

    fn format_flat(&self) -> String {
        let mut line: String = "".to_string();
        let items = self.flatten();
        for item in items {
            match item {
                EnvelopeFormatItem::Begin(s) => {
                    if !line.ends_with(' ') {
                        line += " ";
                    }
                    line += &s;
                    line += " ";
                }
                EnvelopeFormatItem::End(s) => {
                    if !line.ends_with(' ') {
                        line += " ";
                    }
                    line += &s;
                    line += " ";
                }
                EnvelopeFormatItem::Item(s) => line += &s,
                EnvelopeFormatItem::Separator => {
                    line = line.trim_end().to_string() + ", ";
                }
                EnvelopeFormatItem::List(items) => {
                    for item in items {
                        line += &item.format_flat();
                    }
                }
            }
        }
        line
    }

    fn format_hierarchical(&self) -> String {
        let mut lines: Vec<String> = vec![];
        let mut level = 0;
        let mut current_line = "".to_string();
        let items = Self::nicen(&self.flatten());
        for item in items {
            match item {
                EnvelopeFormatItem::Begin(delimiter) => {
                    if !delimiter.is_empty() {
                        let c = if current_line.is_empty() {
                            delimiter
                        } else {
                            Self::add_space_at_end_if_needed(&current_line)
                                + &delimiter
                        };
                        lines.push(Self::indent(level) + &c + "\n");
                    }
                    level += 1;
                    current_line = "".to_string();
                }
                EnvelopeFormatItem::End(delimiter) => {
                    if !current_line.is_empty() {
                        lines.push(Self::indent(level) + &current_line + "\n");
                        current_line = "".to_string();
                    }
                    level -= 1;
                    lines.push(Self::indent(level) + &delimiter + "\n");
                }
                EnvelopeFormatItem::Item(string) => {
                    current_line += &string;
                }
                EnvelopeFormatItem::Separator => {
                    if !current_line.is_empty() {
                        lines.push(Self::indent(level) + &current_line + "\n");
                        current_line = "".to_string();
                    }
                }
                EnvelopeFormatItem::List(_) => {
                    lines.push("<list>".to_string());
                }
            }
        }
        if !current_line.is_empty() {
            lines.push(current_line);
        }
        lines.join("")
    }
}

/// Implements conversion from string slice to EnvelopeFormatItem.
impl From<&str> for EnvelopeFormatItem {
    fn from(s: &str) -> Self { Self::Item(s.to_string()) }
}

/// Implements string display for EnvelopeFormatItem.
impl std::fmt::Display for EnvelopeFormatItem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            EnvelopeFormatItem::Begin(s) => write!(f, ".begin({})", s),
            EnvelopeFormatItem::End(s) => write!(f, ".end({})", s),
            EnvelopeFormatItem::Item(s) => write!(f, ".item({})", s),
            EnvelopeFormatItem::Separator => write!(f, ".separator"),
            EnvelopeFormatItem::List(items) => write!(f, ".list({:?})", items),
        }
    }
}

/// Implements partial equality for EnvelopeFormatItem.
impl PartialEq for EnvelopeFormatItem {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (EnvelopeFormatItem::Begin(s1), EnvelopeFormatItem::Begin(s2)) => {
                s1 == s2
            }
            (EnvelopeFormatItem::End(s1), EnvelopeFormatItem::End(s2)) => {
                s1 == s2
            }
            (EnvelopeFormatItem::Item(s1), EnvelopeFormatItem::Item(s2)) => {
                s1 == s2
            }
            (EnvelopeFormatItem::Separator, EnvelopeFormatItem::Separator) => {
                true
            }
            (
                EnvelopeFormatItem::List(items1),
                EnvelopeFormatItem::List(items2),
            ) => items1 == items2,
            _ => false,
        }
    }
}

impl EnvelopeFormatItem {
    fn index(&self) -> u32 {
        match self {
            EnvelopeFormatItem::Begin(_) => 1,
            EnvelopeFormatItem::End(_) => 2,
            EnvelopeFormatItem::Item(_) => 3,
            EnvelopeFormatItem::Separator => 4,
            EnvelopeFormatItem::List(_) => 5,
        }
    }
}

/// Implements partial ordering for EnvelopeFormatItem.
impl PartialOrd for EnvelopeFormatItem {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

/// Implements total ordering for EnvelopeFormatItem.
impl Ord for EnvelopeFormatItem {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        let l_index = self.index();
        let r_index = other.index();
        match l_index.cmp(&r_index) {
            std::cmp::Ordering::Less => return std::cmp::Ordering::Less,
            std::cmp::Ordering::Greater => return std::cmp::Ordering::Greater,
            _ => {}
        }
        match (self, other) {
            (EnvelopeFormatItem::Begin(l), EnvelopeFormatItem::Begin(r)) => {
                l.cmp(r)
            }
            (EnvelopeFormatItem::End(l), EnvelopeFormatItem::End(r)) => {
                l.cmp(r)
            }
            (EnvelopeFormatItem::Item(l), EnvelopeFormatItem::Item(r)) => {
                l.cmp(r)
            }
            (EnvelopeFormatItem::Separator, EnvelopeFormatItem::Separator) => {
                std::cmp::Ordering::Equal
            }
            (EnvelopeFormatItem::List(l), EnvelopeFormatItem::List(r)) => {
                l.cmp(r)
            }
            _ => std::cmp::Ordering::Equal,
        }
    }
}

/// Implements formatting for CBOR values in envelope notation.
impl EnvelopeFormat for CBOR {
    fn format_item(&self, opts: &EnvelopeFormatOpts<'_>) -> EnvelopeFormatItem {
        match self.as_case() {
            CBORCase::Tagged(tag, cbor) if tag == &Envelope::cbor_tags()[0] => {
                Envelope::from_untagged_cbor(cbor.clone())
                    .map(|envelope| envelope.format_item(opts))
                    .unwrap_or_else(|_| "<error>".into())
            }
            _ => EnvelopeFormatItem::Item(
                self.envelope_summary(usize::MAX, &opts.context)
                    .unwrap_or_else(|_| "<error>".into()),
            ),
        }
    }
}

/// Implements formatting for Envelope in envelope notation.
impl EnvelopeFormat for Envelope {
    fn format_item(&self, opts: &EnvelopeFormatOpts<'_>) -> EnvelopeFormatItem {
        match self.case() {
            EnvelopeCase::Leaf { cbor, .. } => cbor.format_item(opts),
            EnvelopeCase::Wrapped { envelope, .. } => {
                EnvelopeFormatItem::List(vec![
                    EnvelopeFormatItem::Begin("{".to_string()),
                    envelope.format_item(opts),
                    EnvelopeFormatItem::End("}".to_string()),
                ])
            }
            EnvelopeCase::Assertion(assertion) => assertion.format_item(opts),
            #[cfg(feature = "known_value")]
            EnvelopeCase::KnownValue { value, .. } => value.format_item(opts),
            #[cfg(feature = "encrypt")]
            EnvelopeCase::Encrypted(_) => {
                EnvelopeFormatItem::Item("ENCRYPTED".to_string())
            }
            #[cfg(feature = "compress")]
            EnvelopeCase::Compressed(_) => {
                EnvelopeFormatItem::Item("COMPRESSED".to_string())
            }
            EnvelopeCase::Node { subject, assertions, .. } => {
                let mut items: Vec<EnvelopeFormatItem> = Vec::new();

                let subject_item = subject.format_item(opts);
                let mut elided_count = 0;
                #[cfg(feature = "encrypt")]
                let mut encrypted_count = 0;
                #[cfg(feature = "compress")]
                let mut compressed_count = 0;
                #[cfg(feature = "known_value")]
                let mut type_assertion_items: Vec<
                    Vec<EnvelopeFormatItem>,
                > = Vec::new();
                let mut assertion_items: Vec<Vec<EnvelopeFormatItem>> =
                    Vec::new();

                for assertion in assertions {
                    match assertion.case() {
                        EnvelopeCase::Elided(_) => {
                            elided_count += 1;
                        }
                        #[cfg(feature = "encrypt")]
                        EnvelopeCase::Encrypted(_) => {
                            encrypted_count += 1;
                        }
                        #[cfg(feature = "compress")]
                        EnvelopeCase::Compressed(_) => {
                            compressed_count += 1;
                        }
                        _ => {
                            let item = vec![assertion.format_item(opts)];
                            #[cfg(feature = "known_value")]
                            {
                                let mut is_type_assertion = false;
                                if let Some(predicate) =
                                    assertion.as_predicate()
                                    && let Some(known_value) =
                                        predicate.subject().as_known_value()
                                    && *known_value == known_values::IS_A
                                {
                                    is_type_assertion = true;
                                }

                                if is_type_assertion {
                                    type_assertion_items.push(item);
                                } else {
                                    assertion_items.push(item);
                                }
                            }
                            #[cfg(not(feature = "known_value"))]
                            assertion_items.push(item);
                        }
                    }
                }
                #[cfg(feature = "known_value")]
                type_assertion_items.sort();
                assertion_items.sort();
                #[cfg(feature = "known_value")]
                assertion_items.splice(0..0, type_assertion_items);
                #[cfg(feature = "compress")]
                if compressed_count > 1 {
                    assertion_items.push(vec![EnvelopeFormatItem::Item(
                        format!("COMPRESSED ({})", compressed_count),
                    )]);
                } else if compressed_count > 0 {
                    assertion_items.push(vec![EnvelopeFormatItem::Item(
                        "COMPRESSED".to_string(),
                    )]);
                }
                if elided_count > 1 {
                    assertion_items.push(vec![EnvelopeFormatItem::Item(
                        format!("ELIDED ({})", elided_count),
                    )]);
                } else if elided_count > 0 {
                    assertion_items.push(vec![EnvelopeFormatItem::Item(
                        "ELIDED".to_string(),
                    )]);
                }
                #[cfg(feature = "encrypt")]
                if encrypted_count > 1 {
                    assertion_items.push(vec![EnvelopeFormatItem::Item(
                        format!("ENCRYPTED ({})", encrypted_count),
                    )]);
                } else if encrypted_count > 0 {
                    assertion_items.push(vec![EnvelopeFormatItem::Item(
                        "ENCRYPTED".to_string(),
                    )]);
                }
                let joined_assertions_items: Vec<Vec<EnvelopeFormatItem>> =
                    itertools::intersperse_with(assertion_items, || {
                        vec![EnvelopeFormatItem::Separator]
                    })
                    .collect();

                let needs_braces = subject.is_subject_assertion();

                if needs_braces {
                    items.push(EnvelopeFormatItem::Begin("{".to_string()));
                }
                items.push(subject_item);
                if needs_braces {
                    items.push(EnvelopeFormatItem::End("}".to_string()));
                }
                items.push(EnvelopeFormatItem::Begin("[".to_string()));
                items.extend(joined_assertions_items.into_iter().flatten());
                items.push(EnvelopeFormatItem::End("]".to_string()));
                EnvelopeFormatItem::List(items)
            }
            EnvelopeCase::Elided(_) => {
                EnvelopeFormatItem::Item("ELIDED".to_string())
            }
        }
    }
}

/// Implements formatting for Assertion in envelope notation.
impl EnvelopeFormat for Assertion {
    fn format_item(&self, opts: &EnvelopeFormatOpts<'_>) -> EnvelopeFormatItem {
        EnvelopeFormatItem::List(vec![
            self.predicate().format_item(opts),
            EnvelopeFormatItem::Item(": ".to_string()),
            self.object().format_item(opts),
        ])
    }
}

#[cfg(feature = "known_value")]
/// Implements formatting for KnownValue in envelope notation.
impl EnvelopeFormat for KnownValue {
    fn format_item(&self, opts: &EnvelopeFormatOpts<'_>) -> EnvelopeFormatItem {
        let name = match opts.context {
            FormatContextOpt::None => {
                self.name().to_string().flanked_by("'", "'")
            }
            FormatContextOpt::Global => {
                crate::with_format_context!(|context: &crate::FormatContext| {
                    context
                        .known_values()
                        .assigned_name(self)
                        .map(|s| s.to_string())
                        .unwrap_or_else(|| self.name().to_string())
                })
            }
            FormatContextOpt::Custom(format_context) => format_context
                .known_values()
                .assigned_name(self)
                .map(|s| s.to_string())
                .unwrap_or_else(|| self.name().to_string()),
        };
        EnvelopeFormatItem::Item(name.flanked_by("'", "'"))
    }
}

/// Implements formatting for XID in envelope notation.
impl EnvelopeFormat for XID {
    fn format_item(
        &self,
        _opts: &EnvelopeFormatOpts<'_>,
    ) -> EnvelopeFormatItem {
        EnvelopeFormatItem::Item(hex::encode(self.data()))
    }
}

impl Envelope {
    fn description(&self, opts: &EnvelopeFormatOpts<'_>) -> String {
        match self.case() {
            EnvelopeCase::Node { subject, assertions, .. } => {
                let assertions = assertions
                    .iter()
                    .map(|a| a.to_string())
                    .collect::<Vec<String>>()
                    .join(", ")
                    .flanked_by("[", "]");
                format!(".node({}, {})", subject, assertions)
            }
            EnvelopeCase::Leaf { cbor, .. } => {
                format!(".cbor({})", cbor.format_item(opts))
            }
            EnvelopeCase::Wrapped { envelope, .. } => {
                format!(".wrapped({})", envelope)
            }
            EnvelopeCase::Assertion(assertion) => format!(
                ".assertion({}, {})",
                assertion.predicate(),
                assertion.object()
            ),
            EnvelopeCase::Elided(_) => ".elided".to_string(),
            #[cfg(feature = "known_value")]
            EnvelopeCase::KnownValue { value, .. } => {
                format!(".knownValue({})", value)
            }
            #[cfg(feature = "encrypt")]
            EnvelopeCase::Encrypted(_) => ".encrypted".to_string(),
            #[cfg(feature = "compress")]
            EnvelopeCase::Compressed(_) => ".compressed".to_string(),
        }
    }
}

/// Implements string display for Envelope.
impl std::fmt::Display for Envelope {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.description(&EnvelopeFormatOpts::default()))
    }
}