xur_runtime/marshal/
mod.rs1mod type_desc;
9mod wrappers;
10
11use facet::{Facet, Shape};
12
13use crate::RunResult;
14use crate::RuntimeError;
15use crate::Value;
16use crate::convert::{self, ConvertCx};
17
18pub use type_desc::TypeDesc;
19pub use wrappers::{XurBytes, XurList, XurPtr, XurStr};
20
21pub trait FromValue: Sized + Send + 'static {
23 const SHAPE: Option<&'static Shape> = None;
24 fn type_desc() -> TypeDesc {
26 TypeDesc::of_shape(Self::SHAPE)
27 }
28 fn from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self>;
29}
30
31pub trait IntoXurValue: Send + 'static {
33 const SHAPE: Option<&'static Shape> = None;
34 fn type_desc() -> TypeDesc {
36 TypeDesc::of_shape(Self::SHAPE)
37 }
38 fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value>;
39}
40
41impl<T: Facet<'static> + Send + 'static> FromValue for T {
42 const SHAPE: Option<&'static Shape> = Some(T::SHAPE);
43 fn from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
44 convert::from_value::<T>(cx, value)
45 }
46}
47
48impl<T: Facet<'static> + Send + 'static> IntoXurValue for T {
49 const SHAPE: Option<&'static Shape> = Some(T::SHAPE);
50 fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
51 convert::into_value(cx, self)
52 }
53}
54
55impl FromValue for Value {
56 fn type_desc() -> TypeDesc {
57 TypeDesc::ANY
58 }
59 fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
60 Ok(value.clone())
61 }
62}
63
64impl IntoXurValue for Value {
65 fn type_desc() -> TypeDesc {
66 TypeDesc::ANY
67 }
68 fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
69 Ok(self)
70 }
71}
72
73pub trait FromArg: Sized + Send + 'static {
79 const SHAPE: Option<&'static Shape> = None;
80 const REQUIRED: bool = true;
82 fn type_desc() -> TypeDesc {
84 TypeDesc::of_shape(Self::SHAPE)
85 }
86 fn from_arg(cx: &dyn ConvertCx, value: Option<&Value>) -> RunResult<Self>;
87}
88
89impl<T: FromValue> FromArg for T {
90 const SHAPE: Option<&'static Shape> = <T as FromValue>::SHAPE;
91 fn type_desc() -> TypeDesc {
92 <T as FromValue>::type_desc()
93 }
94 fn from_arg(cx: &dyn ConvertCx, value: Option<&Value>) -> RunResult<Self> {
95 match value {
96 Some(value) => T::from_value(cx, value),
97 None => Err(RuntimeError::new("missing a required argument")),
98 }
99 }
100}
101
102pub struct OptionalArg<T>(pub Option<T>);
107
108impl<T: FromValue> FromArg for OptionalArg<T> {
109 const SHAPE: Option<&'static Shape> = <T as FromValue>::SHAPE;
110 const REQUIRED: bool = false;
111 fn type_desc() -> TypeDesc {
112 <T as FromValue>::type_desc()
113 }
114 fn from_arg(cx: &dyn ConvertCx, value: Option<&Value>) -> RunResult<Self> {
115 match value {
116 Some(value) => Ok(OptionalArg(Some(T::from_value(cx, value)?))),
117 None => Ok(OptionalArg(None)),
118 }
119 }
120}
121
122pub struct Raise<T>(pub RunResult<T>);
142
143impl<T: IntoXurValue> IntoXurValue for Raise<T> {
144 const SHAPE: Option<&'static Shape> = T::SHAPE;
145 fn type_desc() -> TypeDesc {
146 T::type_desc()
147 }
148 fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
149 self.0?.into_xur_value(cx)
150 }
151}
152
153impl<T> From<RunResult<T>> for Raise<T> {
154 fn from(result: RunResult<T>) -> Self {
155 Raise(result)
156 }
157}
158
159pub struct Opt<T>(pub Option<T>);
164
165impl<T: IntoXurValue> IntoXurValue for Opt<T> {
166 fn type_desc() -> TypeDesc {
167 TypeDesc::Generic("Option", vec![T::type_desc()])
168 }
169 fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
170 let lang = cx.lang();
171 Ok(match self.0 {
172 Some(value) => Value::adt(lang.some, vec![value.into_xur_value(cx)?]),
173 None => Value::adt(lang.none, Vec::new()),
174 })
175 }
176}
177
178impl<T> From<Option<T>> for Opt<T> {
179 fn from(option: Option<T>) -> Self {
180 Opt(option)
181 }
182}
183
184pub struct Res<T>(pub Result<T, String>);
190
191impl<T: IntoXurValue> IntoXurValue for Res<T> {
192 fn type_desc() -> TypeDesc {
193 TypeDesc::Generic("Result", vec![T::type_desc(), TypeDesc::STR])
194 }
195 fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
196 let lang = cx.lang();
197 Ok(match self.0 {
198 Ok(value) => Value::adt(lang.ok, vec![value.into_xur_value(cx)?]),
199 Err(message) => Value::adt(lang.err, vec![Value::str(&message)]),
200 })
201 }
202}
203
204impl<T> From<Result<T, String>> for Res<T> {
205 fn from(result: Result<T, String>) -> Self {
206 Res(result)
207 }
208}
209
210pub struct OneOf<E>(pub E);
218
219pub trait OneOfVariants: Sized + Send + 'static {
222 fn type_descs() -> Vec<TypeDesc> {
224 Vec::new()
225 }
226 fn try_from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self>;
227}
228
229impl<E: OneOfVariants> FromValue for OneOf<E> {
230 fn type_desc() -> TypeDesc {
231 TypeDesc::Union(E::type_descs())
232 }
233 fn from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
234 Ok(OneOf(E::try_from_value(cx, value)?))
235 }
236}
237
238#[macro_export]
255macro_rules! one_of {
256 (
257 $(#[$meta:meta])*
258 $vis:vis enum $name:ident {
259 $( $(#[$vmeta:meta])* $variant:ident($ty:ty) ),+ $(,)?
260 }
261 ) => {
262 $(#[$meta])*
263 $vis enum $name {
264 $( $(#[$vmeta])* $variant($ty) ),+
265 }
266
267 impl $crate::OneOfVariants for $name {
268 fn type_descs() -> ::std::vec::Vec<$crate::TypeDesc> {
269 ::std::vec![$( <$ty as $crate::FromValue>::type_desc() ),+]
270 }
271 fn try_from_value(
272 cx: &dyn $crate::convert::ConvertCx,
273 value: &$crate::Value,
274 ) -> $crate::RunResult<Self> {
275 $(
276 if let Ok(v) = <$ty as $crate::FromValue>::from_value(cx, value) {
277 return Ok(Self::$variant(v));
278 }
279 )+
280 Err($crate::RuntimeError::new(concat!(
281 "expected one of the types accepted by `",
282 stringify!($name),
283 "`"
284 )))
285 }
286 }
287 };
288}