compose_library/foundations/cast/
mod.rs

1mod reflect;
2mod into_value;
3mod into_result;
4
5use crate::diag::{Spanned, StrResult};
6use crate::{UnitValue, Value};
7use compose_macros::cast;
8pub use into_result::*;
9pub use into_value::*;
10pub use reflect::*;
11
12
13pub trait FromValue<V = Value>: Sized {
14    fn from_value(value: V) -> StrResult<Self>;
15}
16
17impl FromValue for Value {
18    fn from_value(value: Value) -> StrResult<Self> {
19        Ok(value)
20    }
21}
22
23impl<T: FromValue> FromValue<Spanned<Value>> for T {
24    fn from_value(value: Spanned<Value>) -> StrResult<Self> {
25        T::from_value(value.value)
26    }
27}
28
29impl<T: FromValue> FromValue<Spanned<Value>> for Spanned<T> {
30    fn from_value(value: Spanned<Value>) -> StrResult<Self> {
31        Ok(Spanned::new(T::from_value(value.value)?, value.span))
32    }
33}
34
35impl<T: FromValue> FromValue for Option<T> {
36    fn from_value(value: Value) -> StrResult<Self> {
37        match value {
38            Value::Unit(_) => Ok(None),
39            _ => Ok(Some(T::from_value(value)?)),
40        }
41    }
42}
43
44cast! {
45    (),
46    self => Value::Unit(UnitValue),
47    v: () => v,
48}