Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TryFromJs for Either<L, R> #3822

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ intrusive-collections = "0.9.6"
cfg-if = "1.0.0"
time.workspace = true
hashbrown.workspace = true
either = { version = "1", optional = true }
hansl marked this conversation as resolved.
Show resolved Hide resolved

# intl deps
boa_icu_provider = { workspace = true, features = ["std"], optional = true }
Expand Down
44 changes: 44 additions & 0 deletions core/engine/src/value/conversions/either.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//! Implementation of [`TryFromJs`] for [`Either`].
//!
//! This will try to deserialize for the [`Either::Left`] type
//! first, and if it fails will try the [`Either::Right`] type.
//!
//! Upon failure of both, the second failure will be returned.
#![cfg(feature = "either")]

use crate::value::TryFromJs;
use boa_engine::{Context, JsResult, JsValue};
use either::Either;

impl<L, R> TryFromJs for Either<L, R>
where
L: TryFromJs,
R: TryFromJs,
{
#[inline]
fn try_from_js(value: &JsValue, context: &mut Context) -> JsResult<Self> {
L::try_from_js(value, context)
.map(Self::Left)
.or_else(|_| R::try_from_js(value, context).map(Self::Right))
}
}

#[test]
fn either() {
let v = JsValue::Integer(123);
let mut context = Context::default();

assert_eq!(
Either::<i32, i32>::try_from_js(&v, &mut context),
Ok(Either::Left(123))
);
assert_eq!(
Either::<i32, String>::try_from_js(&v, &mut context),
Ok(Either::Left(123))
);
assert_eq!(
Either::<String, i32>::try_from_js(&v, &mut context),
Ok(Either::Right(123))
);
assert!(Either::<String, String>::try_from_js(&v, &mut context).is_err());
}
1 change: 1 addition & 0 deletions core/engine/src/value/conversions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::js_string;

use super::{JsBigInt, JsObject, JsString, JsSymbol, JsValue, Profiler};

mod either;
mod serde_json;
pub(super) mod try_from_js;

Expand Down
Loading