mod dynamic;
mod type_desc;
mod wrappers;
use facet::{Facet, Shape};
use crate::RunResult;
use crate::RuntimeError;
use crate::Value;
use crate::convert::{self, ConvertCx};
pub use dynamic::{Describe, Dyn, desc};
pub use type_desc::TypeDesc;
pub use wrappers::{XurBytes, XurList, XurPtr, XurStr};
pub trait FromValue: Sized + Send + 'static {
const SHAPE: Option<&'static Shape> = None;
fn type_desc() -> TypeDesc {
TypeDesc::of_shape(Self::SHAPE)
}
fn from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self>;
}
pub trait IntoXurValue: Send + 'static {
const SHAPE: Option<&'static Shape> = None;
fn type_desc() -> TypeDesc {
TypeDesc::of_shape(Self::SHAPE)
}
fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value>;
}
impl<T: Facet<'static> + Send + 'static> FromValue for T {
const SHAPE: Option<&'static Shape> = Some(T::SHAPE);
fn from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
convert::from_value::<T>(cx, value)
}
}
impl<T: Facet<'static> + Send + 'static> IntoXurValue for T {
const SHAPE: Option<&'static Shape> = Some(T::SHAPE);
fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
convert::into_value(cx, self)
}
}
impl FromValue for Value {
fn type_desc() -> TypeDesc {
TypeDesc::ANY
}
fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
Ok(value.clone())
}
}
impl IntoXurValue for Value {
fn type_desc() -> TypeDesc {
TypeDesc::ANY
}
fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
Ok(self)
}
}
pub trait FromArg: Sized + Send + 'static {
const SHAPE: Option<&'static Shape> = None;
const REQUIRED: bool = true;
fn type_desc() -> TypeDesc {
TypeDesc::of_shape(Self::SHAPE)
}
fn from_arg(cx: &dyn ConvertCx, value: Option<&Value>) -> RunResult<Self>;
}
impl<T: FromValue> FromArg for T {
const SHAPE: Option<&'static Shape> = <T as FromValue>::SHAPE;
fn type_desc() -> TypeDesc {
<T as FromValue>::type_desc()
}
fn from_arg(cx: &dyn ConvertCx, value: Option<&Value>) -> RunResult<Self> {
match value {
Some(value) => T::from_value(cx, value),
None => Err(RuntimeError::new("missing a required argument")),
}
}
}
pub struct OptionalArg<T>(pub Option<T>);
impl<T: FromValue> FromArg for OptionalArg<T> {
const SHAPE: Option<&'static Shape> = <T as FromValue>::SHAPE;
const REQUIRED: bool = false;
fn type_desc() -> TypeDesc {
<T as FromValue>::type_desc()
}
fn from_arg(cx: &dyn ConvertCx, value: Option<&Value>) -> RunResult<Self> {
match value {
Some(value) => Ok(OptionalArg(Some(T::from_value(cx, value)?))),
None => Ok(OptionalArg(None)),
}
}
}
pub struct Raise<T>(pub RunResult<T>);
impl<T: IntoXurValue> IntoXurValue for Raise<T> {
const SHAPE: Option<&'static Shape> = T::SHAPE;
fn type_desc() -> TypeDesc {
T::type_desc()
}
fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
self.0?.into_xur_value(cx)
}
}
impl<T> From<RunResult<T>> for Raise<T> {
fn from(result: RunResult<T>) -> Self {
Raise(result)
}
}
pub struct Opt<T>(pub Option<T>);
impl<T: IntoXurValue> IntoXurValue for Opt<T> {
fn type_desc() -> TypeDesc {
TypeDesc::Generic("Option", vec![T::type_desc()])
}
fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
let lang = cx.lang();
Ok(match self.0 {
Some(value) => Value::adt(lang.some, vec![value.into_xur_value(cx)?]),
None => Value::adt(lang.none, Vec::new()),
})
}
}
impl<T> From<Option<T>> for Opt<T> {
fn from(option: Option<T>) -> Self {
Opt(option)
}
}
pub struct Res<T>(pub Result<T, String>);
impl<T: IntoXurValue> IntoXurValue for Res<T> {
fn type_desc() -> TypeDesc {
TypeDesc::Generic("Result", vec![T::type_desc(), TypeDesc::STR])
}
fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
let lang = cx.lang();
Ok(match self.0 {
Ok(value) => Value::adt(lang.ok, vec![value.into_xur_value(cx)?]),
Err(message) => Value::adt(lang.err, vec![Value::str(&message)]),
})
}
}
impl<T> From<Result<T, String>> for Res<T> {
fn from(result: Result<T, String>) -> Self {
Res(result)
}
}
pub struct OneOf<E>(pub E);
pub trait OneOfVariants: Sized + Send + 'static {
const NAME: &'static str = "";
fn type_descs() -> Vec<TypeDesc> {
Vec::new()
}
fn try_from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self>;
}
impl<E: OneOfVariants> FromValue for OneOf<E> {
fn type_desc() -> TypeDesc {
if E::NAME.is_empty() {
TypeDesc::Union(E::type_descs())
} else {
TypeDesc::Alias(E::NAME, E::type_descs())
}
}
fn from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
Ok(OneOf(E::try_from_value(cx, value)?))
}
}
#[macro_export]
macro_rules! one_of {
(
$(#[$meta:meta])*
$vis:vis enum $name:ident as $shown:literal {
$( $(#[$vmeta:meta])* $variant:ident($ty:ty) ),+ $(,)?
}
) => {
$crate::one_of!(@impl $shown; $(#[$meta])* $vis enum $name { $( $(#[$vmeta])* $variant($ty) ),+ });
};
(
$(#[$meta:meta])*
$vis:vis enum $name:ident {
$( $(#[$vmeta:meta])* $variant:ident($ty:ty) ),+ $(,)?
}
) => {
$crate::one_of!(@impl stringify!($name); $(#[$meta])* $vis enum $name { $( $(#[$vmeta])* $variant($ty) ),+ });
};
(@impl $shown:expr;
$(#[$meta:meta])*
$vis:vis enum $name:ident {
$( $(#[$vmeta:meta])* $variant:ident($ty:ty) ),+
}
) => {
$(#[$meta])*
$vis enum $name {
$( $(#[$vmeta])* $variant($ty) ),+
}
impl $crate::OneOfVariants for $name {
const NAME: &'static str = $shown;
fn type_descs() -> ::std::vec::Vec<$crate::TypeDesc> {
::std::vec![$( <$ty as $crate::FromValue>::type_desc() ),+]
}
fn try_from_value(
cx: &dyn $crate::convert::ConvertCx,
value: &$crate::Value,
) -> $crate::RunResult<Self> {
$(
if let Ok(v) = <$ty as $crate::FromValue>::from_value(cx, value) {
return Ok(Self::$variant(v));
}
)+
Err($crate::RuntimeError::new(concat!(
"expected one of the types accepted by `",
stringify!($name),
"`"
)))
}
}
};
}