pub struct MetadataStore(/* private fields */);Expand description
A lightweight key-value metadata store backed by a HashMap.
MetadataStore is designed for storing arbitrary string metadata such as
task attributes, labels, annotations, headers, or contextual information.
Keys are unique within the store. Attempting to insert a duplicate key
returns a MetadataError::DuplicateKey error.
§Examples
let mut metadata = MetadataStore::new();
metadata.insert("request_id", "abc-123")?;
metadata.insert("environment", "production")?;
assert_eq!(
metadata.get("request_id"),
Some("abc-123")
);
assert!(metadata.contains_key("environment"));
Implementations§
Source§impl MetadataStore
impl MetadataStore
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty MetadataStore.
§Examples
let metadata = MetadataStore::new();
assert_eq!(metadata.iter().count(), 0);Sourcepub fn insert<K, V>(&mut self, key: K, value: V) -> Result<(), MetadataError>
pub fn insert<K, V>(&mut self, key: K, value: V) -> Result<(), MetadataError>
Inserts a key-value pair into the store.
Returns an error if the key already exists.
§Errors
Returns MetadataError::DuplicateKey if the provided key is already
present in the store.
§Examples
let mut metadata = MetadataStore::new();
metadata.insert("region", "us-east-1")?;
assert_eq!(
metadata.get("region"),
Some("us-east-1")
);
Duplicate keys are rejected:
let mut metadata = MetadataStore::new();
metadata.insert("service", "api")?;
let err = metadata.insert("service", "worker").unwrap_err();
assert_eq!(
err,
MetadataError::DuplicateKey("service".to_string())
);
Sourcepub fn get(&self, key: &str) -> Option<&str>
pub fn get(&self, key: &str) -> Option<&str>
Returns a reference to the value corresponding to the given key.
Returns None if the key does not exist.
§Examples
let mut metadata = MetadataStore::new();
metadata.insert("version", "1.0")?;
assert_eq!(
metadata.get("version"),
Some("1.0")
);
assert_eq!(metadata.get("missing"), None);
Sourcepub fn remove(&mut self, key: &str) -> Option<String>
pub fn remove(&mut self, key: &str) -> Option<String>
Removes a key from the store, returning the stored value if it existed.
§Examples
let mut metadata = MetadataStore::new();
metadata.insert("token", "secret")?;
assert_eq!(
metadata.remove("token"),
Some("secret".to_string())
);
assert!(!metadata.contains_key("token"));
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Returns true if the store contains the specified key.
§Examples
let mut metadata = MetadataStore::new();
metadata.insert("owner", "alice")?;
assert!(metadata.contains_key("owner"));
assert!(!metadata.contains_key("missing"));
Sourcepub fn iter(&self) -> impl Iterator<Item = (&String, &String)>
pub fn iter(&self) -> impl Iterator<Item = (&String, &String)>
Returns an iterator over all key-value pairs in the store.
The iterator yields (&String, &String) pairs.
§Examples
let mut metadata = MetadataStore::new();
metadata.insert("a", "1")?;
metadata.insert("b", "2")?;
let items: Vec<_> = metadata.iter().collect();
assert_eq!(items.len(), 2);
Sourcepub fn into_inner(self) -> HashMap<String, String>
pub fn into_inner(self) -> HashMap<String, String>
Consumes the store and returns the underlying HashMap.
§Examples
let mut metadata = MetadataStore::new();
metadata.insert("key", "value")?;
let inner = metadata.into_inner();
assert_eq!(
inner.get("key"),
Some(&"value".to_string())
);
Sourcepub fn extract_as<M: Metadata>(&self) -> Result<M, M::Error>
pub fn extract_as<M: Metadata>(&self) -> Result<M, M::Error>
Get a typed metadata entry.
Sourcepub fn from_map(map: HashMap<String, String>) -> Self
pub fn from_map(map: HashMap<String, String>) -> Self
Create a MetadataStore from a HashMap<String, String>.
Sourcepub fn get_mut(&mut self, key: &str) -> Option<&mut String>
pub fn get_mut(&mut self, key: &str) -> Option<&mut String>
Returns a mutable reference to the value corresponding to the key.
Returns None if the key does not exist.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (&String, &mut String)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&String, &mut String)>
Returns a mutable iterator over all key-value pairs in the store.
Sourcepub fn keys(&self) -> impl Iterator<Item = &String>
pub fn keys(&self) -> impl Iterator<Item = &String>
Returns an iterator over all keys in the store.
Sourcepub fn values(&self) -> impl Iterator<Item = &String>
pub fn values(&self) -> impl Iterator<Item = &String>
Returns an iterator over all values in the store.
Sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut String>
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut String>
Returns a mutable iterator over all values in the store.
Sourcepub fn extend<I, K, V>(&mut self, iter: I)
pub fn extend<I, K, V>(&mut self, iter: I)
Extends the store with key-value pairs.
Existing keys are overwritten.
Use MetadataStore::insert when duplicate keys should be rejected.
Trait Implementations§
Source§impl Clone for MetadataStore
impl Clone for MetadataStore
Source§impl Debug for MetadataStore
impl Debug for MetadataStore
Source§impl Default for MetadataStore
impl Default for MetadataStore
Source§impl<'de> Deserialize<'de> for MetadataStore
Available on crate feature serde only.
impl<'de> Deserialize<'de> for MetadataStore
serde only.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>,
impl Eq for MetadataStore
Source§impl<K, V> FromIterator<(K, V)> for MetadataStore
impl<K, V> FromIterator<(K, V)> for MetadataStore
Source§impl<Args: Send + Sync> FromRequest<Task<Args>> for MetadataStore
impl<Args: Send + Sync> FromRequest<Task<Args>> for MetadataStore
Source§impl Index<&str> for MetadataStore
impl Index<&str> for MetadataStore
Source§impl IndexMut<&str> for MetadataStore
impl IndexMut<&str> for MetadataStore
Source§impl IntoIterator for MetadataStore
impl IntoIterator for MetadataStore
Source§impl<'a> IntoIterator for &'a MetadataStore
impl<'a> IntoIterator for &'a MetadataStore
Source§impl<'a> IntoIterator for &'a mut MetadataStore
impl<'a> IntoIterator for &'a mut MetadataStore
Source§impl PartialEq for MetadataStore
impl PartialEq for MetadataStore
Source§impl Serialize for MetadataStore
Available on crate feature serde only.
impl Serialize for MetadataStore
serde only.