pub struct Metadata(/* private fields */);Expand description
A structured, ordered, typed key-value store for metadata fields.
Metadata stores values as qubit_value::Value, preserving concrete Rust
scalar types such as i64, u32, f64, String, and bool. This avoids
the ambiguity of a single JSON number type while still allowing callers to
store explicit Value::Json values when they really need JSON payloads.
Use Metadata::with for fluent construction and Metadata::set when
mutating an existing object.
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: DataTypeOf + FromMetadataValue,
pub fn get<T>(&self, key: &str) -> Option<T>where
T: DataTypeOf + FromMetadataValue,
Retrieves the value associated with key and converts it to T.
This convenience method returns None when the key is absent or when the
stored Value cannot be converted to T.
Sourcepub fn try_get<T>(&self, key: &str) -> MetadataResult<T>where
T: DataTypeOf + FromMetadataValue,
pub fn try_get<T>(&self, key: &str) -> MetadataResult<T>where
T: DataTypeOf + FromMetadataValue,
Retrieves the value associated with key and converts it to T.
§Errors
Returns MetadataError::MissingKey when the key is absent, or
MetadataError::TypeMismatch when the stored value cannot be converted
to the requested type.
Sourcepub fn get_raw(&self, key: &str) -> Option<&Value>
pub fn get_raw(&self, key: &str) -> Option<&Value>
Returns a reference to the stored Value for key, or None if absent.
Sourcepub fn data_type(&self, key: &str) -> Option<DataType>
pub fn data_type(&self, key: &str) -> Option<DataType>
Returns the concrete data type of the value stored under key.
Sourcepub fn get_or<T>(&self, key: &str, default: T) -> Twhere
T: DataTypeOf + FromMetadataValue,
pub fn get_or<T>(&self, key: &str, default: T) -> Twhere
T: DataTypeOf + FromMetadataValue,
Retrieves and converts the value associated with key, or returns
default if lookup or conversion fails.
Sourcepub fn set<T>(&mut self, key: &str, value: T) -> Option<Value>where
T: IntoMetadataValue,
pub fn set<T>(&mut self, key: &str, value: T) -> Option<Value>where
T: IntoMetadataValue,
Inserts a typed value under key and returns the previous value if present.
Sourcepub fn set_checked<T>(
&mut self,
schema: &MetadataSchema,
key: &str,
value: T,
) -> MetadataResult<Option<Value>>where
T: IntoMetadataValue,
pub fn set_checked<T>(
&mut self,
schema: &MetadataSchema,
key: &str,
value: T,
) -> MetadataResult<Option<Value>>where
T: IntoMetadataValue,
Inserts a typed value after validating it against schema.
§Errors
Returns MetadataError::UnknownField when key is rejected by the
schema, or MetadataError::TypeMismatch when the constructed value’s
concrete type does not match the schema field type.
Sourcepub fn with_checked<T>(
self,
schema: &MetadataSchema,
key: &str,
value: T,
) -> MetadataResult<Self>where
T: IntoMetadataValue,
pub fn with_checked<T>(
self,
schema: &MetadataSchema,
key: &str,
value: T,
) -> MetadataResult<Self>where
T: IntoMetadataValue,
Returns a new metadata object with a typed value validated and inserted.
§Errors
Returns MetadataError::UnknownField when key is rejected by the
schema, or MetadataError::TypeMismatch when the constructed value’s
concrete type does not match the schema field type.
Sourcepub fn with<T>(self, key: &str, value: T) -> Selfwhere
T: IntoMetadataValue,
pub fn with<T>(self, key: &str, value: T) -> Selfwhere
T: IntoMetadataValue,
Returns a new metadata object with key set to value.
Sourcepub fn set_raw(&mut self, key: &str, value: Value) -> Option<Value>
pub fn set_raw(&mut self, key: &str, value: Value) -> Option<Value>
Inserts a raw Value directly and returns the previous value if present.
Sourcepub fn with_raw(self, key: &str, value: Value) -> Self
pub fn with_raw(self, key: &str, value: Value) -> Self
Returns a new metadata object with a raw Value inserted.
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 stored 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 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 entries from self and other.
Entries from other take precedence on key conflicts.
Sourcepub fn retain<F>(&mut self, predicate: F)
pub fn retain<F>(&mut self, predicate: F)
Retains only the entries for which predicate returns true.
Sourcepub fn into_inner(self) -> BTreeMap<String, Value>
pub fn into_inner(self) -> BTreeMap<String, Value>
Converts this metadata object into its underlying map.
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)