Skip to content

Commit 95722ed

Browse files
committed
remove &str special handling
1 parent bc90962 commit 95722ed

File tree

6 files changed

+19
-42
lines changed

6 files changed

+19
-42
lines changed

src/rust/api/dns.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl DnsUtil {
136136
/// `DnsParserError::InvalidHexString`
137137
/// `DnsParserError::ParseIntError`
138138
#[jsg_method]
139-
pub fn parse_caa_record(&self, record: &str) -> Result<CaaRecord, DnsParserError> {
139+
pub fn parse_caa_record(&self, record: String) -> Result<CaaRecord, DnsParserError> {
140140
// Let's remove "\\#" and the length of data from the beginning of the record
141141
let data = record.split_ascii_whitespace().collect::<Vec<_>>()[2..].to_vec();
142142
let critical = data[0].parse::<u8>()?;
@@ -191,7 +191,7 @@ impl DnsUtil {
191191
/// `DnsParserError::InvalidHexString`
192192
/// `DnsParserError::ParseIntError`
193193
#[jsg_method]
194-
pub fn parse_naptr_record(&self, record: &str) -> jsg::Result<NaptrRecord, DnsParserError> {
194+
pub fn parse_naptr_record(&self, record: String) -> jsg::Result<NaptrRecord, DnsParserError> {
195195
let data = record.split_ascii_whitespace().collect::<Vec<_>>()[1..].to_vec();
196196

197197
let order_str = data[1..3].to_vec();
@@ -262,7 +262,7 @@ mod tests {
262262
_state: ResourceState::default(),
263263
};
264264
let record = dns_util
265-
.parse_caa_record("\\# 15 00 05 69 73 73 75 65 70 6b 69 2e 67 6f 6f 67")
265+
.parse_caa_record("\\# 15 00 05 69 73 73 75 65 70 6b 69 2e 67 6f 6f 67".to_owned())
266266
.unwrap();
267267

268268
assert_eq!(record.critical, 0);
@@ -277,7 +277,8 @@ mod tests {
277277
};
278278
let record = dns_util
279279
.parse_caa_record(
280-
"\\# 21 00 09 69 73 73 75 65 77 69 6c 64 6c 65 74 73 65 6e 63 72 79 70 74",
280+
"\\# 21 00 09 69 73 73 75 65 77 69 6c 64 6c 65 74 73 65 6e 63 72 79 70 74"
281+
.to_owned(),
281282
)
282283
.unwrap();
283284

@@ -291,8 +292,9 @@ mod tests {
291292
let dns_util = DnsUtil {
292293
_state: ResourceState::default(),
293294
};
294-
let result =
295-
dns_util.parse_caa_record("\\# 15 00 05 69 6e 76 61 6c 69 64 70 6b 69 2e 67 6f 6f 67");
295+
let result = dns_util.parse_caa_record(
296+
"\\# 15 00 05 69 6e 76 61 6c 69 64 70 6b 69 2e 67 6f 6f 67".to_owned(),
297+
);
296298

297299
assert!(result.is_err());
298300
}
@@ -303,7 +305,7 @@ mod tests {
303305
_state: ResourceState::default(),
304306
};
305307
let record = dns_util
306-
.parse_naptr_record("\\# 37 15 b3 08 ae 01 73 0a 6d 79 2d 73 65 72 76 69 63 65 06 72 65 67 65 78 70 0b 72 65 70 6c 61 63 65 6d 65 6e 74 00")
308+
.parse_naptr_record("\\# 37 15 b3 08 ae 01 73 0a 6d 79 2d 73 65 72 76 69 63 65 06 72 65 67 65 78 70 0b 72 65 70 6c 61 63 65 6d 65 6e 74 00".to_owned())
307309
.unwrap();
308310

309311
assert_eq!(record.flags, "s");

src/rust/jsg-macros/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ wd_rust_proc_macro(
44
name = "jsg-macros",
55
visibility = ["//visibility:public"],
66
deps = [
7-
"@crates_vendor//:proc-macro2",
87
"@crates_vendor//:quote",
98
"@crates_vendor//:syn",
109
],

src/rust/jsg-macros/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Generates the `jsg::Struct` and `jsg::Type` implementations for data structures.
99
```rust
1010
#[jsg_struct]
1111
pub struct CaaRecord {
12-
pub critical: u8,
12+
pub critical: f64,
1313
pub field: String,
1414
pub value: String,
1515
}
@@ -29,7 +29,7 @@ Parameters and return values are handled via the `jsg::Wrappable` trait. Any typ
2929
```rust
3030
impl DnsUtil {
3131
#[jsg_method(name = "parseCaaRecord")]
32-
pub fn parse_caa_record(&self, record: &str) -> Result<CaaRecord, DnsParserError> {
32+
pub fn parse_caa_record(&self, record: String) -> Result<CaaRecord, DnsParserError> {
3333
// Errors are thrown as JavaScript exceptions
3434
}
3535

@@ -65,7 +65,7 @@ pub struct MyUtil {
6565
#[jsg_resource]
6666
impl DnsUtil {
6767
#[jsg_method]
68-
pub fn parse_caa_record(&self, record: &str) -> Result<CaaRecord, DnsParserError> {
68+
pub fn parse_caa_record(&self, record: String) -> Result<CaaRecord, DnsParserError> {
6969
// implementation
7070
}
7171
}

src/rust/jsg-macros/lib.rs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
mod types;
2-
31
use proc_macro::TokenStream;
4-
use proc_macro2::TokenStream as TokenStream2;
52
use quote::ToTokens;
63
use quote::quote;
74
use syn::Data;
@@ -10,10 +7,8 @@ use syn::Fields;
107
use syn::FnArg;
118
use syn::ItemFn;
129
use syn::ItemImpl;
13-
use syn::Type;
1410
use syn::parse_macro_input;
1511
use syn::spanned::Spanned;
16-
use types::is_str_ref;
1712

1813
/// Generates `jsg::Struct` and `jsg::Type` implementations for data structures.
1914
///
@@ -104,13 +99,15 @@ pub fn jsg_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
10499
})
105100
.collect();
106101

107-
let (unwraps, arg_refs): (Vec<_>, Vec<_>) = params
102+
let (unwraps, arg_names): (Vec<_>, Vec<_>) = params
108103
.iter()
109104
.enumerate()
110105
.map(|(i, ty)| {
111106
let arg = syn::Ident::new(&format!("arg{i}"), fn_name.span());
112-
let (unwrap, arg_ref) = generate_unwrap(&arg, ty, i);
113-
(unwrap, arg_ref)
107+
let unwrap = quote! {
108+
let Some(#arg) = <#ty as jsg::Wrappable>::try_unwrap(&mut lock, args.get(#i)) else { return; };
109+
};
110+
(unwrap, arg)
114111
})
115112
.unzip();
116113

@@ -124,29 +121,13 @@ pub fn jsg_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
124121
#(#unwraps)*
125122
let this = args.this();
126123
let self_ = jsg::unwrap_resource::<Self>(&mut lock, this);
127-
let result = self_.#fn_name(#(#arg_refs),*);
124+
let result = self_.#fn_name(#(#arg_names),*);
128125
jsg::Wrappable::wrap_return(result, &mut lock, &mut args);
129126
}
130127
}
131128
.into()
132129
}
133130

134-
fn generate_unwrap(arg: &syn::Ident, ty: &Type, index: usize) -> (TokenStream2, TokenStream2) {
135-
// &str: unwrap as String and borrow
136-
if is_str_ref(ty) {
137-
let unwrap = quote! {
138-
let Some(#arg) = <String as jsg::Wrappable>::try_unwrap(&mut lock, args.get(#index)) else { return; };
139-
};
140-
return (unwrap, quote! { &#arg });
141-
}
142-
143-
// All types use Wrappable::try_unwrap
144-
let unwrap = quote! {
145-
let Some(#arg) = <#ty as jsg::Wrappable>::try_unwrap(&mut lock, args.get(#index)) else { return; };
146-
};
147-
(unwrap, quote! { #arg })
148-
}
149-
150131
/// Generates boilerplate for JSG resources.
151132
///
152133
/// On structs: generates `jsg::Type` and `ResourceTemplate`.

src/rust/jsg-macros/types.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/rust/jsg-test/tests/resource_callback.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct EchoResource {
2626
#[expect(clippy::unnecessary_wraps)]
2727
impl EchoResource {
2828
#[jsg_method]
29-
pub fn echo(&self, message: &str) -> Result<String, String> {
29+
pub fn echo(&self, message: String) -> Result<String, String> {
3030
Ok(format!("{}{}", self.prefix, message))
3131
}
3232
}

0 commit comments

Comments
 (0)