pub struct TagsStore { /* private fields */ }Expand description
A registry that maintains mappings between CBOR tags, their human-readable names, and optional summarizers.
The TagsStore is the primary implementation of the TagsStoreTrait,
providing a bidirectional mapping between CBOR tag values (numbers) and
their human-readable names. It also supports registering custom summarizers
for specific tags.
§Use Cases
The TagsStore serves several important purposes:
- Readability: Converting numeric tags to meaningful names in diagnostic output
- Consistency: Ensuring consistent tag usage throughout an application
- Documentation: Providing a central registry of all tags used in a system
- Customization: Supporting custom summarization of tagged values
§Features
- Bidirectional lookup between tag values and names
- Prevention of duplicate registrations with conflicting names
- Optional registration of custom summarizers for specific tags
- Default implementation for an empty registry
§Examples
§Basic usage
use dcbor::prelude::*;
// Create a new TagsStore with a set of predefined tags
let mut tags = TagsStore::new([
Tag::new(1, "date".to_string()),
Tag::new(2, "positive_bignum".to_string()),
Tag::new(3, "negative_bignum".to_string()),
]);
// Look up a tag by its value
let date_tag = tags.tag_for_value(1).unwrap();
assert_eq!(date_tag.name().unwrap(), "date");
// Look up a tag name by value
let name = tags.name_for_value(2);
assert_eq!(name, "positive_bignum");
// Look up a tag by its name
let neg_tag = tags.tag_for_name("negative_bignum").unwrap();
assert_eq!(neg_tag.value(), 3);§Adding summarizers
use std::sync::Arc;
use dcbor::prelude::*;
// Create an empty tags store
let mut tags = TagsStore::default();
// Register a tag
tags.insert(Tag::new(1, "date".to_string()));
// Add a summarizer for the date tag
tags.set_summarizer(
1,
Arc::new(|cbor, _flat| {
// Try to convert CBOR to f64 for timestamp formatting
let timestamp: f64 = cbor.clone().try_into().unwrap_or(0.0);
Ok(format!("Timestamp: {}", timestamp))
}),
);
// Later, this summarizer can be retrieved and used
let date_summarizer = tags.summarizer(1);
assert!(date_summarizer.is_some());§Implementation Notes
The TagsStore prevents registering the same tag value with different
names, which helps maintain consistency in CBOR tag usage. Attempting to
register a tag value that already exists with a different name will panic.
Implementations§
Trait Implementations§
Source§impl TagsStoreTrait for TagsStore
impl TagsStoreTrait for TagsStore
fn assigned_name_for_tag(&self, tag: &Tag) -> Option<String>
fn name_for_tag(&self, tag: &Tag) -> String
fn tag_for_name(&self, name: &str) -> Option<Tag>
fn tag_for_value(&self, value: u64) -> Option<Tag>
fn name_for_value(&self, value: u64) -> String
fn summarizer( &self, tag: u64, ) -> Option<&Arc<dyn Fn(CBOR, bool) -> Result<String, Error> + Send + Sync>>
fn name_for_tag_opt<T>(tag: &Tag, tags: Option<&T>) -> Stringwhere
T: TagsStoreTrait,
Self: Sized,
Auto Trait Implementations§
impl !RefUnwindSafe for TagsStore
impl !UnwindSafe for TagsStore
impl Freeze for TagsStore
impl Send for TagsStore
impl Sync for TagsStore
impl Unpin for TagsStore
impl UnsafeUnpin for TagsStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more