Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 17 additions & 7 deletions core/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,10 @@ fn generate_conversion(

let mut from_js_with = None;
let mut field_name = rename.rename(format!("{name}"));
if let Some(attr) = field
for attr in field
.attrs
.into_iter()
.find(|attr| attr.path().is_ident("boa"))
.filter(|attr| attr.path().is_ident("boa"))
{
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("from_js_with") {
Expand All @@ -538,11 +538,18 @@ fn generate_conversion(
let value = meta.value()?;
field_name = value.parse::<LitStr>()?.value();
Ok(())
} else if meta.path.is_ident("into_js_with") {
let _unused = meta.value()?.parse::<LitStr>()?;
Ok(())
} else if meta.path.is_ident("skip") && meta.input.is_empty() {
Ok(())
} else {
Err(meta.error(
"invalid syntax in the `#[boa()]` attribute. \
Note that this attribute only accepts the following syntax: \
`#[boa(from_js_with = \"fully::qualified::path\")]`",
Note that this attribute only accepts the following syntax: \
\n* `#[boa(from_js_with = \"fully::qualified::path\")]` \
\n* `#[boa(rename = \"jsPropertyName\")]` \
",
))
}
})
Expand Down Expand Up @@ -666,14 +673,17 @@ fn generate_obj_properties(
let value = meta.value()?;
prop_key = value.parse::<LitStr>()?.value();
Ok(())
} else if meta.path.is_ident("skip") & meta.input.is_empty() {
} else if meta.path.is_ident("skip") && meta.input.is_empty() {
skip = true;
Ok(())
} else if meta.path.is_ident("from_js_with") {
let _unused = meta.value()?.parse::<LitStr>()?;
Ok(())
} else {
Err(meta.error(
"invalid syntax in the `#[boa()]` attribute. \
Note that this attribute only accepts the following syntax: \
\n* `#[boa(into_js_with = \"fully::qualified::path\")]`\
Note that this attribute only accepts the following syntax: \
\n* `#[boa(into_js_with = \"fully::qualified::path\")]` \
\n* `#[boa(rename = \"jsPropertyName\")]` \
\n* `#[boa(skip)]` \
",
Expand Down
23 changes: 23 additions & 0 deletions tests/macros/tests/derive/both.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![allow(unused)]

use boa_engine::{
value::{TryFromJs, TryIntoJs},
Context, JsResult, JsValue,
};

#[derive(TryFromJs, TryIntoJs)]
struct Blah {
#[boa(into_js_with = "my_custom_converter_to_js")]
#[boa(from_js_with = "my_custom_converter_from_js")]
x: i32,
}

fn my_custom_converter_to_js(value: &i32, _context: &mut Context) -> JsResult<JsValue> {
Ok(JsValue::new(*value))
}

fn my_custom_converter_from_js(value: &JsValue, _context: &mut Context) -> JsResult<i32> {
value.try_js_into(_context)
}

fn main() {}