Skip to main content

context_core/document/
metadata.rs

1use serde::{Deserialize, Serialize};
2use std::collections::BTreeMap;
3
4#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(untagged)]
6pub enum MetadataValue {
7    String(String),
8    Number(i64),
9}
10
11#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
12#[serde(transparent)]
13pub struct Metadata {
14    inner: BTreeMap<String, MetadataValue>,
15}
16
17impl Metadata {
18    pub fn new() -> Self {
19        Metadata {
20            inner: BTreeMap::new(),
21        }
22    }
23
24    pub fn insert_string(&mut self, key: impl Into<String>, value: impl Into<String>) {
25        self.inner.insert(key.into(), MetadataValue::String(value.into()));
26    }
27
28    pub fn insert_number(&mut self, key: impl Into<String>, value: i64) {
29        self.inner.insert(key.into(), MetadataValue::Number(value));
30    }
31    
32    // Helper to merge another metadata into this one (overriding common keys)
33    pub fn merge(&mut self, other: Metadata) {
34        for (k, v) in other.inner {
35            self.inner.insert(k, v);
36        }
37    }
38
39    pub fn get(&self, key: &str) -> Option<&MetadataValue> {
40        self.inner.get(key)
41    }
42
43    pub fn iter(&self) -> impl Iterator<Item = (&String, &MetadataValue)> {
44        self.inner.iter()
45    }
46}
47
48