pub type CBORSummarizer = Arc<dyn Fn(CBOR, bool) -> Result<String> + Send + Sync>;Expand description
A function type for summarizing CBOR values as human-readable strings.
CBORSummarizer is used to create custom formatters for tagged CBOR values.
It’s a type alias for a thread-safe function pointer that takes a CBOR value
and returns a string representation or an error.
§Purpose
In CBOR, tags provide semantic meaning to data structures. A
CBORSummarizer helps interpret and display these tagged values in a
human-readable format. This is particularly useful for debugging, logging,
or displaying CBOR data to users.
§Thread Safety
The CBORSummarizer type is wrapped in an Arc (Atomic Reference Count)
and requires Send + Sync traits, making it safe to share between threads.
§Examples
use std::sync::Arc;
use dcbor::prelude::*;
// Create a custom summarizer for a date tag
let date_summarizer: CBORSummarizer = Arc::new(|cbor, _flat| {
// Extract timestamp from tagged CBOR
let timestamp: f64 = cbor.clone().try_into()?;
// Format timestamp as ISO date (simplified example)
Ok(format!("Date: {:.1} seconds since epoch", timestamp))
});
// Create a tags store
let mut tags = TagsStore::default();
// Register a tag for date (tag 1 is the standard CBOR tag for dates)
tags.insert(Tag::new(1, "date".to_string()));
// Register our summarizer for tag 1
tags.set_summarizer(1, date_summarizer);When this summarizer is used (for example in diagnostic output), it would convert a tagged CBOR timestamp into a more readable date format.
Aliased Type§
pub struct CBORSummarizer { /* private fields */ }