bc_envelope/format/
hex.rs

1use dcbor::prelude::*;
2
3use crate::{Envelope, FormatContextOpt};
4
5#[derive(Clone, Default)]
6pub struct HexFormatOpts<'a> {
7    annotate: bool,
8    context: FormatContextOpt<'a>,
9}
10
11impl<'a> From<HexFormatOpts<'a>> for dcbor::HexFormatOpts<'a> {
12    fn from(opts: HexFormatOpts<'a>) -> Self {
13        dcbor::HexFormatOpts::default()
14            .annotate(opts.annotate)
15            .context(opts.context.into())
16    }
17}
18
19impl<'a> HexFormatOpts<'a> {
20    /// Sets whether to annotate the hex dump with tags.
21    pub fn annotate(mut self, annotate: bool) -> Self {
22        self.annotate = annotate;
23        self
24    }
25
26    /// Sets the formatting context for the hex dump.
27    pub fn context(mut self, context: FormatContextOpt<'a>) -> Self {
28        self.context = context;
29        self
30    }
31}
32
33impl Envelope {
34    /// Returns the CBOR hex dump of this envelope.
35    ///
36    /// See [RFC-8949](https://www.rfc-editor.org/rfc/rfc8949.html) for information on
37    /// the CBOR binary format.
38    pub fn hex_opt<'a>(&self, opts: HexFormatOpts<'a>) -> String {
39        let cbor: CBOR = self.clone().into();
40        let hex_opts: dcbor::HexFormatOpts<'a> = opts.into();
41        cbor.hex_opt(&hex_opts)
42    }
43
44    /// Returns the CBOR hex dump of this envelope.
45    ///
46    /// Uses the current format context.
47    ///
48    /// See [RFC-8949](https://www.rfc-editor.org/rfc/rfc8949.html) for information on
49    /// the CBOR binary format.
50    pub fn hex(&self) -> String {
51        self.hex_opt(HexFormatOpts::default().annotate(true))
52    }
53}