xur_runtime/marshal/
mod.rs1mod dynamic;
9mod type_desc;
10mod wrappers;
11
12use facet::{Facet, Shape};
13
14use crate::RunResult;
15use crate::RuntimeError;
16use crate::Value;
17use crate::convert::{self, ConvertCx};
18
19pub use dynamic::{Describe, Dyn, desc};
20pub use type_desc::TypeDesc;
21pub use wrappers::{XurBytes, XurList, XurPtr, XurStr};
22
23pub trait FromValue: Sized + Send + 'static {
25 const SHAPE: Option<&'static Shape> = None;
26 fn type_desc() -> TypeDesc {
28 TypeDesc::of_shape(Self::SHAPE)
29 }
30 fn from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self>;
31}
32
33pub trait IntoXurValue: Send + 'static {
35 const SHAPE: Option<&'static Shape> = None;
36 fn type_desc() -> TypeDesc {
38 TypeDesc::of_shape(Self::SHAPE)
39 }
40 fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value>;
41}
42
43impl<T: Facet<'static> + Send + 'static> FromValue for T {
44 const SHAPE: Option<&'static Shape> = Some(T::SHAPE);
45 fn from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
46 convert::from_value::<T>(cx, value)
47 }
48}
49
50impl<T: Facet<'static> + Send + 'static> IntoXurValue for T {
51 const SHAPE: Option<&'static Shape> = Some(T::SHAPE);
52 fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
53 convert::into_value(cx, self)
54 }
55}
56
57impl FromValue for Value {
58 fn type_desc() -> TypeDesc {
59 TypeDesc::ANY
60 }
61 fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
62 Ok(value.clone())
63 }
64}
65
66impl IntoXurValue for Value {
67 fn type_desc() -> TypeDesc {
68 TypeDesc::ANY
69 }
70 fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
71 Ok(self)
72 }
73}
74
75pub trait FromArg: Sized + Send + 'static {
81 const SHAPE: Option<&'static Shape> = None;
82 const REQUIRED: bool = true;
84 fn type_desc() -> TypeDesc {
86 TypeDesc::of_shape(Self::SHAPE)
87 }
88 fn from_arg(cx: &dyn ConvertCx, value: Option<&Value>) -> RunResult<Self>;
89}
90
91impl<T: FromValue> FromArg for T {
92 const SHAPE: Option<&'static Shape> = <T as FromValue>::SHAPE;
93 fn type_desc() -> TypeDesc {
94 <T as FromValue>::type_desc()
95 }
96 fn from_arg(cx: &dyn ConvertCx, value: Option<&Value>) -> RunResult<Self> {
97 match value {
98 Some(value) => T::from_value(cx, value),
99 None => Err(RuntimeError::new("missing a required argument")),
100 }
101 }
102}
103
104pub struct OptionalArg<T>(pub Option<T>);
109
110impl<T: FromValue> FromArg for OptionalArg<T> {
111 const SHAPE: Option<&'static Shape> = <T as FromValue>::SHAPE;
112 const REQUIRED: bool = false;
113 fn type_desc() -> TypeDesc {
114 <T as FromValue>::type_desc()
115 }
116 fn from_arg(cx: &dyn ConvertCx, value: Option<&Value>) -> RunResult<Self> {
117 match value {
118 Some(value) => Ok(OptionalArg(Some(T::from_value(cx, value)?))),
119 None => Ok(OptionalArg(None)),
120 }
121 }
122}
123
124pub struct Raise<T>(pub RunResult<T>);
144
145impl<T: IntoXurValue> IntoXurValue for Raise<T> {
146 const SHAPE: Option<&'static Shape> = T::SHAPE;
147 fn type_desc() -> TypeDesc {
148 T::type_desc()
149 }
150 fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
151 self.0?.into_xur_value(cx)
152 }
153}
154
155impl<T> From<RunResult<T>> for Raise<T> {
156 fn from(result: RunResult<T>) -> Self {
157 Raise(result)
158 }
159}
160
161pub struct Opt<T>(pub Option<T>);
166
167impl<T: IntoXurValue> IntoXurValue for Opt<T> {
168 fn type_desc() -> TypeDesc {
169 TypeDesc::Generic("Option", vec![T::type_desc()])
170 }
171 fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
172 let lang = cx.lang();
173 Ok(match self.0 {
174 Some(value) => Value::adt(lang.some, vec![value.into_xur_value(cx)?]),
175 None => Value::adt(lang.none, Vec::new()),
176 })
177 }
178}
179
180impl<T> From<Option<T>> for Opt<T> {
181 fn from(option: Option<T>) -> Self {
182 Opt(option)
183 }
184}
185
186pub struct Res<T>(pub Result<T, String>);
192
193impl<T: IntoXurValue> IntoXurValue for Res<T> {
194 fn type_desc() -> TypeDesc {
195 TypeDesc::Generic("Result", vec![T::type_desc(), TypeDesc::STR])
196 }
197 fn into_xur_value(self, cx: &dyn ConvertCx) -> RunResult<Value> {
198 let lang = cx.lang();
199 Ok(match self.0 {
200 Ok(value) => Value::adt(lang.ok, vec![value.into_xur_value(cx)?]),
201 Err(message) => Value::adt(lang.err, vec![Value::str(&message)]),
202 })
203 }
204}
205
206impl<T> From<Result<T, String>> for Res<T> {
207 fn from(result: Result<T, String>) -> Self {
208 Res(result)
209 }
210}
211
212pub struct OneOf<E>(pub E);
220
221pub trait OneOfVariants: Sized + Send + 'static {
224 const NAME: &'static str = "";
226 fn type_descs() -> Vec<TypeDesc> {
228 Vec::new()
229 }
230 fn try_from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self>;
231}
232
233impl<E: OneOfVariants> FromValue for OneOf<E> {
234 fn type_desc() -> TypeDesc {
235 if E::NAME.is_empty() {
236 TypeDesc::Union(E::type_descs())
237 } else {
238 TypeDesc::Alias(E::NAME, E::type_descs())
239 }
240 }
241 fn from_value(cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
242 Ok(OneOf(E::try_from_value(cx, value)?))
243 }
244}
245
246#[macro_export]
266macro_rules! one_of {
267 (
268 $(#[$meta:meta])*
269 $vis:vis enum $name:ident as $shown:literal {
270 $( $(#[$vmeta:meta])* $variant:ident($ty:ty) ),+ $(,)?
271 }
272 ) => {
273 $crate::one_of!(@impl $shown; $(#[$meta])* $vis enum $name { $( $(#[$vmeta])* $variant($ty) ),+ });
274 };
275 (
276 $(#[$meta:meta])*
277 $vis:vis enum $name:ident {
278 $( $(#[$vmeta:meta])* $variant:ident($ty:ty) ),+ $(,)?
279 }
280 ) => {
281 $crate::one_of!(@impl stringify!($name); $(#[$meta])* $vis enum $name { $( $(#[$vmeta])* $variant($ty) ),+ });
282 };
283 (@impl $shown:expr;
284 $(#[$meta:meta])*
285 $vis:vis enum $name:ident {
286 $( $(#[$vmeta:meta])* $variant:ident($ty:ty) ),+
287 }
288 ) => {
289 $(#[$meta])*
290 $vis enum $name {
291 $( $(#[$vmeta])* $variant($ty) ),+
292 }
293
294 impl $crate::OneOfVariants for $name {
295 const NAME: &'static str = $shown;
296 fn type_descs() -> ::std::vec::Vec<$crate::TypeDesc> {
297 ::std::vec![$( <$ty as $crate::FromValue>::type_desc() ),+]
298 }
299 fn try_from_value(
300 cx: &dyn $crate::convert::ConvertCx,
301 value: &$crate::Value,
302 ) -> $crate::RunResult<Self> {
303 $(
304 if let Ok(v) = <$ty as $crate::FromValue>::from_value(cx, value) {
305 return Ok(Self::$variant(v));
306 }
307 )+
308 Err($crate::RuntimeError::new(concat!(
309 "expected one of the types accepted by `",
310 stringify!($name),
311 "`"
312 )))
313 }
314 }
315 };
316}