mod wrappers;
use facet::{Facet, Shape};
use crate::RunResult;
use crate::RuntimeError;
use crate::Value;
use crate::convert::{self, ConvertCx};
pub use wrappers::{XurBytes, XurList, XurPtr, XurStr};
pub trait FromValue: Sized + Send + 'static {
const SHAPE: Option<&'static Shape> = None;
fn from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self>;
}
pub trait IntoXurValue: Send + 'static {
const SHAPE: Option<&'static Shape> = None;
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 from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
Ok(value.clone())
}
}
impl IntoXurValue for Value {
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 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 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 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 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 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 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 {
fn try_from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self>;
}
impl<E: OneOfVariants> FromValue for OneOf<E> {
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 {
$( $(#[$vmeta:meta])* $variant:ident($ty:ty) ),+ $(,)?
}
) => {
$(#[$meta])*
$vis enum $name {
$( $(#[$vmeta])* $variant($ty) ),+
}
impl $crate::OneOfVariants for $name {
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),
"`"
)))
}
}
};
}