tembra_core/context_values/
key.rs1use 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 fn into_value(self) -> ContextValue;
26
27 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 pub const fn new(name: &'static str, version: u32) -> Self {
40 Self {
41 name,
42 version,
43 marker: PhantomData,
44 }
45 }
46
47 pub fn contract(&self) -> ContextContract {
49 ContextContract {
50 name: self.name.into(),
51 version: self.version,
52 kind: T::KIND,
53 }
54 }
55}