Skip to main content

qubit_progress/
operation_attributes.rs

1// =============================================================================
2//    Copyright (c) 2025 - 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Immutable-friendly operation correlation attributes.
9
10use std::collections::BTreeMap;
11use std::sync::Arc;
12
13/// String key-value attributes shared by every event in one operation.
14#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
15#[cfg_attr(feature = "serde", serde(transparent))]
16#[derive(Clone, Debug, Default, Eq, PartialEq)]
17pub struct OperationAttributes {
18    /// Stable ordered attribute entries.
19    entries: BTreeMap<Arc<str>, Arc<str>>,
20}
21
22impl OperationAttributes {
23    /// Creates an empty attribute set.
24    #[must_use]
25    pub fn new() -> Self {
26        Self::default()
27    }
28
29    /// Inserts or replaces one attribute value.
30    pub fn insert(&mut self, key: &str, value: &str) {
31        self.entries.insert(Arc::from(key), Arc::from(value));
32    }
33
34    /// Returns one attribute value by key.
35    #[must_use]
36    pub fn get(&self, key: &str) -> Option<&str> {
37        self.entries.get(key).map(AsRef::as_ref)
38    }
39
40    /// Returns all attributes in stable key order.
41    #[must_use = "the iterator yields configured attributes"]
42    pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> + '_ {
43        self.entries
44            .iter()
45            .map(|(key, value)| (key.as_ref(), value.as_ref()))
46    }
47
48    /// Returns whether no attributes are configured.
49    #[must_use]
50    pub fn is_empty(&self) -> bool {
51        self.entries.is_empty()
52    }
53}