xur-runtime 0.1.8

Values, native-function registry and facet conversions for the Xur language
Documentation
//! Dynamic values that say what they are, for signatures that would
//! otherwise read `any`.
//!
//! A native function that takes or returns whatever its receiver holds (a
//! list's element, a map's value) works on a raw [`Value`]. Taking or
//! returning [`Dyn<D>`] instead converts nothing (the value passes through
//! as is) but describes the parameter as `D` does: `list.get` returns
//! `Opt<Dyn<desc::T>>`, shown `Option<T>`.

use std::marker::PhantomData;

use super::{FromValue, IntoXurValue, TypeDesc};
use crate::convert::ConvertCx;
use crate::{RunResult, Value};

/// A type's description, for [`Dyn`].
pub trait Describe {
    fn type_desc() -> TypeDesc;
}

/// A [`Value`] passed through unchanged, described as `D`.
pub struct Dyn<D>(pub Value, PhantomData<fn() -> D>);

impl<D> Dyn<D> {
    pub fn new(value: Value) -> Self {
        Self(value, PhantomData)
    }
}

impl<D: Describe + 'static> FromValue for Dyn<D> {
    fn type_desc() -> TypeDesc {
        D::type_desc()
    }
    fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
        Ok(Self::new(value.clone()))
    }
}

impl<D: Describe + 'static> IntoXurValue for Dyn<D> {
    fn type_desc() -> TypeDesc {
        D::type_desc()
    }
    fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
        Ok(self.0)
    }
}

/// Descriptions for [`Dyn`]: type parameters and common shapes.
pub mod desc {
    use std::marker::PhantomData;

    use super::{Describe, TypeDesc};

    macro_rules! params {
        ($($name:ident: $doc:literal),*) => {$(
            #[doc = $doc]
            pub struct $name;
            impl Describe for $name {
                fn type_desc() -> TypeDesc {
                    TypeDesc::Name(stringify!($name))
                }
            }
        )*};
    }
    params! {
        T: "The receiver's element type: a list's items, a cell's value.",
        U: "A second type parameter, chosen by the caller (what a closure returns).",
        K: "A map's key type.",
        V: "A map's value type."
    }

    /// Any value.
    pub struct Any;
    impl Describe for Any {
        fn type_desc() -> TypeDesc {
            TypeDesc::ANY
        }
    }

    /// An integer or a float.
    pub struct Number;
    impl Describe for Number {
        fn type_desc() -> TypeDesc {
            TypeDesc::Union(vec![TypeDesc::Name("int"), TypeDesc::Name("float")])
        }
    }

    /// `list<D>`.
    pub struct ListOf<D>(PhantomData<fn() -> D>);
    impl<D: Describe> Describe for ListOf<D> {
        fn type_desc() -> TypeDesc {
            TypeDesc::Generic("list", vec![D::type_desc()])
        }
    }

    /// `map<K, V>`.
    pub struct MapOf<K, V>(PhantomData<fn() -> (K, V)>);
    impl<K: Describe, V: Describe> Describe for MapOf<K, V> {
        fn type_desc() -> TypeDesc {
            TypeDesc::Generic("map", vec![K::type_desc(), V::type_desc()])
        }
    }

    /// `(A, B)`.
    pub struct Tuple2<A, B>(PhantomData<fn() -> (A, B)>);
    impl<A: Describe, B: Describe> Describe for Tuple2<A, B> {
        fn type_desc() -> TypeDesc {
            TypeDesc::Tuple(vec![A::type_desc(), B::type_desc()])
        }
    }

    /// `str`, for tuples like `(str, T)`.
    pub struct Str;
    impl Describe for Str {
        fn type_desc() -> TypeDesc {
            TypeDesc::STR
        }
    }

    /// `i64`, for tuples like `(i64, T)`.
    pub struct Int;
    impl Describe for Int {
        fn type_desc() -> TypeDesc {
            TypeDesc::Name("i64")
        }
    }
}