Skip to main content

tembra_core/context_values/
key.rs

1//! Stable typed identities shared by language adapters.
2
3use super::{ContextValue, Result};
4use std::marker::PhantomData;
5
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum ContextKind {
8    Text,
9    Integer,
10    Boolean,
11    Opaque,
12}
13
14#[derive(Clone, Debug, Eq, PartialEq)]
15pub struct ContextContract {
16    pub name: String,
17    pub version: u32,
18    pub kind: ContextKind,
19}
20
21pub trait ContextDatum: Sized {
22    const KIND: ContextKind;
23
24    /// Move typed metadata into its shared representation.
25    fn into_value(self) -> ContextValue;
26
27    /// Refuse a binding with a different scalar kind.
28    fn from_value(value: ContextValue) -> Result<Self>;
29}
30
31pub struct ContextKey<T> {
32    name: &'static str,
33    version: u32,
34    marker: PhantomData<fn() -> T>,
35}
36
37impl<T: ContextDatum> ContextKey<T> {
38    /// Declare a stable namespaced key without code generation.
39    pub const fn new(name: &'static str, version: u32) -> Self {
40        Self {
41            name,
42            version,
43            marker: PhantomData,
44        }
45    }
46
47    /// Describe this key for the shared engine.
48    pub fn contract(&self) -> ContextContract {
49        ContextContract {
50            name: self.name.into(),
51            version: self.version,
52            kind: T::KIND,
53        }
54    }
55}