boa_engine/value/conversions/
either.rs1#![cfg(feature = "either")]
8
9use crate::value::TryFromJs;
10use boa_engine::{Context, JsResult, JsValue};
11use either::Either;
12
13impl<L, R> TryFromJs for Either<L, R>
14where
15 L: TryFromJs,
16 R: TryFromJs,
17{
18 #[inline]
19 fn try_from_js(value: &JsValue, context: &mut Context) -> JsResult<Self> {
20 L::try_from_js(value, context)
21 .map(Self::Left)
22 .or_else(|_| R::try_from_js(value, context).map(Self::Right))
23 }
24}
25
26#[test]
27fn either() {
28 let v = JsValue::new(123);
29 let mut context = Context::default();
30
31 assert_eq!(
32 Either::<i32, i32>::try_from_js(&v, &mut context),
33 Ok(Either::Left(123))
34 );
35 assert_eq!(
36 Either::<i32, String>::try_from_js(&v, &mut context),
37 Ok(Either::Left(123))
38 );
39 assert_eq!(
40 Either::<String, i32>::try_from_js(&v, &mut context),
41 Ok(Either::Right(123))
42 );
43 assert!(Either::<String, String>::try_from_js(&v, &mut context).is_err());
44}