Skip to main content

Metadata

Struct Metadata 

Source
pub struct Metadata(/* private fields */);
Expand description

A structured, ordered, type-safe key-value store for attaching arbitrary annotations to domain objects.

Metadata is backed by a BTreeMap<String, Value> (ordered by key) and provides two layers of typed access:

The type model intentionally stays JSON-shaped rather than closed over a fixed enum of Rust scalar types. This keeps the crate interoperable with serde_json, nested objects, and external JSON-based APIs.

§Examples

use qubit_metadata::Metadata;

let mut meta = Metadata::new();
meta.set("author", "alice");
meta.set("priority", 3_i64);
meta.set("reviewed", true);

// Convenience API
let author: Option<String> = meta.get("author");
assert_eq!(author.as_deref(), Some("alice"));

// Explicit API
let priority = meta.try_get::<i64>("priority").unwrap();
assert_eq!(priority, 3);

Implementations§

Source§

impl Metadata

Source

pub fn new() -> Self

Creates an empty Metadata instance.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no entries.

Source

pub fn len(&self) -> usize

Returns the number of key-value pairs.

Source

pub fn contains_key(&self, key: &str) -> bool

Returns true if the given key exists.

Source

pub fn get<T>(&self, key: &str) -> Option<T>

Retrieves and deserializes the value associated with key.

This is the convenience version of Metadata::try_get. It returns None when the key is absent or when deserialization into T fails.

Use this when a concise, best-effort lookup is preferred over detailed diagnostics.

Source

pub fn try_get<T>(&self, key: &str) -> MetadataResult<T>

Retrieves and deserializes the value associated with key, preserving the reason when retrieval fails.

§Errors
Source

pub fn get_raw(&self, key: &str) -> Option<&Value>

Returns a reference to the raw Value for key, or None if absent.

Source

pub fn value_type(&self, key: &str) -> Option<MetadataValueType>

Returns the coarse JSON value type of the value stored under key.

This is a lightweight inspection API inspired by the stricter type introspection facilities in qubit-value, adapted to Metadata’s open-ended JSON storage model.

Source

pub fn get_or<T>(&self, key: &str, default: T) -> T

Retrieves and deserializes the value associated with key, or returns default if lookup fails for any reason.

This mirrors the forgiving default-value style used by qubit-config. It is intentionally convenience-oriented: both missing keys and type mismatches fall back to the supplied default.

Source

pub fn set<T>(&mut self, key: impl Into<String>, value: T) -> Option<Value>
where T: Serialize,

Serializes value and inserts it under key.

This is the convenience version of Metadata::try_set. It preserves the current ergonomic API and panics if serialization fails.

Source

pub fn try_set<T>( &mut self, key: impl Into<String>, value: T, ) -> MetadataResult<Option<Value>>
where T: Serialize,

Serializes value and inserts it under key, preserving serialization failures instead of panicking.

§Errors

Returns MetadataError::SerializationError when value fails to serialize into serde_json::Value.

Source

pub fn set_raw(&mut self, key: impl Into<String>, value: Value) -> Option<Value>

Inserts a raw Value directly, bypassing serialization.

Returns the previous value if present.

Source

pub fn remove(&mut self, key: &str) -> Option<Value>

Removes the entry for key and returns the raw Value if it existed.

Source

pub fn clear(&mut self)

Removes all entries.

Source

pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)>

Returns an iterator over (&str, &Value) pairs in key-sorted order.

Source

pub fn keys(&self) -> impl Iterator<Item = &str>

Returns an iterator over the keys in sorted order.

Source

pub fn values(&self) -> impl Iterator<Item = &Value>

Returns an iterator over the raw values in key-sorted order.

Source

pub fn merge(&mut self, other: Metadata)

Merges all entries from other into self, overwriting existing keys.

Source

pub fn merged(&self, other: &Metadata) -> Metadata

Returns a new Metadata that contains all entries from both self and other. Entries in other take precedence on key conflicts.

Source

pub fn retain<F>(&mut self, predicate: F)
where F: FnMut(&str, &Value) -> bool,

Retains only the entries for which predicate returns true.

Source

pub fn into_inner(self) -> BTreeMap<String, Value>

Converts this Metadata into its underlying BTreeMap.

Trait Implementations§

Source§

impl Clone for Metadata

Source§

fn clone(&self) -> Metadata

Returns a duplicate 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 Debug for Metadata

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Metadata

Source§

fn default() -> Metadata

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

impl<'de> Deserialize<'de> for Metadata

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Extend<(String, Value)> for Metadata

Source§

fn extend<I: IntoIterator<Item = (String, Value)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<BTreeMap<String, Value>> for Metadata

Source§

fn from(map: BTreeMap<String, Value>) -> Self

Converts to this type from the input type.
Source§

impl From<Metadata> for BTreeMap<String, Value>

Source§

fn from(meta: Metadata) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<(String, Value)> for Metadata

Source§

fn from_iter<I: IntoIterator<Item = (String, Value)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a> IntoIterator for &'a Metadata

Source§

type Item = (&'a String, &'a Value)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, String, Value>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Metadata

Source§

type Item = (String, Value)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<String, Value>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Metadata

Source§

fn eq(&self, other: &Metadata) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Metadata

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Metadata

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,