Struct TagsStore

Source
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:

  1. Readability: Converting numeric tags to meaningful names in diagnostic output
  2. Consistency: Ensuring consistent tag usage throughout an application
  3. Documentation: Providing a central registry of all tags used in a system
  4. 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 dcbor::prelude::*;
use std::sync::Arc;

// 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| {
    // 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§

Source§

impl TagsStore

Source

pub fn new<T>(tags: T) -> Self
where T: IntoIterator<Item = Tag>,

Source

pub fn insert(&mut self, tag: Tag)

Source

pub fn insert_all(&mut self, tags: Vec<Tag>)

Source

pub fn set_summarizer(&mut self, tag: TagValue, summarizer: CBORSummarizer)

Trait Implementations§

Source§

impl Clone for TagsStore

Source§

fn clone(&self) -> TagsStore

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for TagsStore

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl TagsStoreTrait for TagsStore

Source§

fn assigned_name_for_tag(&self, tag: &Tag) -> Option<String>

Source§

fn name_for_tag(&self, tag: &Tag) -> String

Source§

fn tag_for_name(&self, name: &str) -> Option<Tag>

Source§

fn tag_for_value(&self, value: u64) -> Option<Tag>

Source§

fn name_for_value(&self, value: u64) -> String

Source§

fn summarizer(&self, tag: TagValue) -> Option<&CBORSummarizer>

Source§

fn name_for_tag_opt<T>(tag: &Tag, tags: Option<&T>) -> String
where T: TagsStoreTrait, Self: Sized,

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.