use super::{ContextDatum, ContextKind, Result, invalid};
use crate::Id;
use std::{any::Any, fmt, rc::Rc};
#[derive(Clone, Debug, PartialEq)]
pub enum ContextValue {
Text(Rc<str>),
Integer(i64),
Boolean(bool),
Opaque(OpaqueContextValue),
}
impl ContextValue {
pub fn kind(&self) -> ContextKind {
match self {
Self::Text(_) => ContextKind::Text,
Self::Integer(_) => ContextKind::Integer,
Self::Boolean(_) => ContextKind::Boolean,
Self::Opaque(_) => ContextKind::Opaque,
}
}
}
macro_rules! datum {
($ty:ty, $kind:ident, $variant:ident) => {
impl ContextDatum for $ty {
const KIND: ContextKind = ContextKind::$kind;
fn into_value(self) -> ContextValue {
ContextValue::$variant(self)
}
fn from_value(value: ContextValue) -> Result<Self> {
match value {
ContextValue::$variant(value) => Ok(value),
_ => Err(invalid("context type mismatch")),
}
}
}
};
}
datum!(Rc<str>, Text, Text);
impl ContextDatum for String {
const KIND: ContextKind = ContextKind::Text;
fn into_value(self) -> ContextValue {
ContextValue::Text(self.into())
}
fn from_value(value: ContextValue) -> Result<Self> {
match value {
ContextValue::Text(value) => Ok(value.to_string()),
_ => Err(invalid("context type mismatch")),
}
}
}
datum!(i64, Integer, Integer);
datum!(bool, Boolean, Boolean);
#[derive(Clone)]
pub struct OpaqueContextValue {
pub id: Id,
lease: Rc<dyn Any>,
}
impl OpaqueContextValue {
pub fn new(id: Id, lease: impl Any) -> Self {
Self {
id,
lease: Rc::new(lease),
}
}
}
impl fmt::Debug for OpaqueContextValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("OpaqueContextValue").field(&self.id).finish()
}
}
impl PartialEq for OpaqueContextValue {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && Rc::ptr_eq(&self.lease, &other.lease)
}
}
impl ContextDatum for OpaqueContextValue {
const KIND: ContextKind = ContextKind::Opaque;
fn into_value(self) -> ContextValue {
ContextValue::Opaque(self)
}
fn from_value(value: ContextValue) -> Result<Self> {
match value {
ContextValue::Opaque(value) => Ok(value),
_ => Err(invalid("context type mismatch")),
}
}
}