use super::{ContextValue, Result};
use std::marker::PhantomData;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ContextKind {
Text,
Integer,
Boolean,
Opaque,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ContextContract {
pub name: String,
pub version: u32,
pub kind: ContextKind,
}
pub trait ContextDatum: Sized {
const KIND: ContextKind;
fn into_value(self) -> ContextValue;
fn from_value(value: ContextValue) -> Result<Self>;
}
pub struct ContextKey<T> {
name: &'static str,
version: u32,
marker: PhantomData<fn() -> T>,
}
impl<T: ContextDatum> ContextKey<T> {
pub const fn new(name: &'static str, version: u32) -> Self {
Self {
name,
version,
marker: PhantomData,
}
}
pub fn contract(&self) -> ContextContract {
ContextContract {
name: self.name.into(),
version: self.version,
kind: T::KIND,
}
}
}