Skip to main content

roam_types/
metadata.rs

1use facet::Facet;
2
3// r[impl rpc.metadata]
4// r[impl rpc.metadata.value]
5/// Metadata value.
6#[repr(u8)]
7#[derive(Debug, Clone, PartialEq, Eq, Facet)]
8pub enum MetadataValue<'a> {
9    String(&'a str) = 0,
10    Bytes(&'a [u8]) = 1,
11    U64(u64) = 2,
12}
13
14/// Metadata entry flags.
15///
16/// Flags control metadata handling behavior.
17// r[impl rpc.metadata.flags]
18// r[impl rpc.metadata.flags.sensitive]
19// r[impl rpc.metadata.flags.no-propagate]
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Facet)]
21#[repr(transparent)]
22#[facet(transparent)]
23pub struct MetadataFlags(u64);
24
25impl MetadataFlags {
26    /// No special handling.
27    pub const NONE: Self = Self(0);
28
29    /// Value MUST NOT be logged, traced, or included in error messages.
30    pub const SENSITIVE: Self = Self(1 << 0);
31
32    /// Value MUST NOT be forwarded to downstream calls.
33    pub const NO_PROPAGATE: Self = Self(1 << 1);
34
35    /// Returns `true` if all flags in `other` are set in `self`.
36    pub fn contains(self, other: Self) -> bool {
37        (self.0 & other.0) == other.0
38    }
39}
40
41impl std::ops::BitOr for MetadataFlags {
42    type Output = Self;
43    fn bitor(self, rhs: Self) -> Self {
44        Self(self.0 | rhs.0)
45    }
46}
47
48impl std::ops::BitOrAssign for MetadataFlags {
49    fn bitor_assign(&mut self, rhs: Self) {
50        self.0 |= rhs.0;
51    }
52}
53
54impl std::ops::BitAnd for MetadataFlags {
55    type Output = Self;
56    fn bitand(self, rhs: Self) -> Self {
57        Self(self.0 & rhs.0)
58    }
59}
60
61impl std::ops::BitAndAssign for MetadataFlags {
62    fn bitand_assign(&mut self, rhs: Self) {
63        self.0 &= rhs.0;
64    }
65}
66
67// r[impl rpc.metadata.keys]
68// r[impl rpc.metadata.duplicates]
69/// A single metadata entry with a key, value, and flags.
70#[derive(Debug, Clone, PartialEq, Eq, Facet)]
71pub struct MetadataEntry<'a> {
72    pub key: &'a str,
73    pub value: MetadataValue<'a>,
74    pub flags: MetadataFlags,
75}
76
77// r[impl rpc.metadata.unknown]
78/// A list of metadata entries.
79pub type Metadata<'a> = Vec<MetadataEntry<'a>>;