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:
- Convenience accessors like
Metadata::getandMetadata::setkeep the API terse and ergonomic. - Explicit accessors like
Metadata::try_getandMetadata::try_setpreserve failure reasons, which is useful for debugging and validation.
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
impl Metadata
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Returns true if the given key exists.
Sourcepub fn get<T>(&self, key: &str) -> Option<T>where
T: DeserializeOwned,
pub fn get<T>(&self, key: &str) -> Option<T>where
T: DeserializeOwned,
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.
Sourcepub fn try_get<T>(&self, key: &str) -> MetadataResult<T>where
T: DeserializeOwned,
pub fn try_get<T>(&self, key: &str) -> MetadataResult<T>where
T: DeserializeOwned,
Retrieves and deserializes the value associated with key, preserving
the reason when retrieval fails.
§Errors
MetadataError::MissingKeyifkeydoes not existMetadataError::DeserializationErrorif the stored JSON value cannot be deserialized intoT
Sourcepub fn get_raw(&self, key: &str) -> Option<&Value>
pub fn get_raw(&self, key: &str) -> Option<&Value>
Returns a reference to the raw Value for key, or None if absent.
Sourcepub fn value_kind(&self, key: &str) -> Option<MetadataValueKind>
pub fn value_kind(&self, key: &str) -> Option<MetadataValueKind>
Returns the coarse JSON kind 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.
Sourcepub fn get_or<T>(&self, key: &str, default: T) -> Twhere
T: DeserializeOwned,
pub fn get_or<T>(&self, key: &str, default: T) -> Twhere
T: DeserializeOwned,
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.
Sourcepub fn set<T>(&mut self, key: impl Into<String>, value: T) -> Option<Value>where
T: Serialize,
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.
Sourcepub fn try_set<T>(
&mut self,
key: impl Into<String>,
value: T,
) -> MetadataResult<Option<Value>>where
T: Serialize,
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.
Sourcepub fn set_raw(&mut self, key: impl Into<String>, value: Value) -> Option<Value>
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.
Sourcepub fn remove(&mut self, key: &str) -> Option<Value>
pub fn remove(&mut self, key: &str) -> Option<Value>
Removes the entry for key and returns the raw Value if it existed.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&str, &Value)>
pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)>
Returns an iterator over (&str, &Value) pairs in key-sorted order.
Sourcepub fn keys(&self) -> impl Iterator<Item = &str>
pub fn keys(&self) -> impl Iterator<Item = &str>
Returns an iterator over the keys in sorted order.
Sourcepub fn values(&self) -> impl Iterator<Item = &Value>
pub fn values(&self) -> impl Iterator<Item = &Value>
Returns an iterator over the raw values in key-sorted order.
Sourcepub fn merge(&mut self, other: Metadata)
pub fn merge(&mut self, other: Metadata)
Merges all entries from other into self, overwriting existing keys.
Sourcepub fn merged(&self, other: &Metadata) -> Metadata
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.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Metadata
impl<'de> Deserialize<'de> for Metadata
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Extend<(String, Value)> for Metadata
impl Extend<(String, Value)> for Metadata
Source§fn extend<I: IntoIterator<Item = (String, Value)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (String, Value)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)