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

Handle raw function arguments property #250

Merged
merged 2 commits into from
Jun 24, 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
35 changes: 25 additions & 10 deletions macros/src/specta.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// inspired by https://github.com/tauri-apps/tauri/blob/2901145c497299f033ba7120af5f2e7ead16c75a/core/tauri-macros/src/command/handler.rs

use std::str::FromStr;

use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse_macro_input, FnArg, ItemFn, Pat, Visibility};

Expand Down Expand Up @@ -39,16 +42,28 @@ pub fn attribute(item: proc_macro::TokenStream) -> syn::Result<proc_macro::Token
None => false,
};

let arg_names = function.sig.inputs.iter().map(|input| match input {
FnArg::Receiver(_) => unreachable!("Commands cannot take 'self'"),
FnArg::Typed(arg) => match &*arg.pat {
Pat::Ident(ident) => ident.ident.to_token_stream(),
Pat::Macro(m) => m.mac.tokens.to_token_stream(),
Pat::Struct(s) => s.path.to_token_stream(),
Pat::Slice(s) => s.attrs[0].to_token_stream(),
Pat::Tuple(s) => s.elems[0].to_token_stream(),
_ => unreachable!("Commands must take named arguments"),
},
let arg_names = function.sig.inputs.iter().map(|input| {
let arg = match input {
FnArg::Receiver(_) => unreachable!("Commands cannot take 'self'"),
FnArg::Typed(arg) => match &*arg.pat {
Pat::Ident(ident) => ident.ident.to_token_stream(),
Pat::Macro(m) => m.mac.tokens.to_token_stream(),
Pat::Struct(s) => s.path.to_token_stream(),
Pat::Slice(s) => s.attrs[0].to_token_stream(),
Pat::Tuple(s) => s.elems[0].to_token_stream(),
_ => unreachable!("Commands must take named arguments"),
},
};

let mut s = arg.to_string();

let s = if s.starts_with("r#") {
s.split_off(2)
} else {
s
};

TokenStream::from_str(&s).unwrap()
});

let arg_signatures = function.sig.inputs.iter().map(|_| quote!(_));
Expand Down
9 changes: 9 additions & 0 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ mod test {
pub fn nested() {}
}

#[specta]
fn raw(r#type: i32) {}

// TODO: Finish fixing these

#[test]
Expand Down Expand Up @@ -319,5 +322,11 @@ mod test {
assert_eq!(def.result, None);
assert_eq!(def.docs, Cow::Borrowed(" Testing Doc Comment"));
}

{
let mut type_map = &mut specta::TypeMap::default();
let def: function::FunctionDataType = specta::fn_datatype!(type_map; raw);
assert_eq!(def.args[0].0, "type");
}
}
}
8 changes: 4 additions & 4 deletions tests/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ fn typescript_types() {
ExportError::InvalidName(
NamedLocation::Type,
#[cfg(not(windows))]
ExportPath::new_unsafe("tests/ts.rs:619:10"),
ExportPath::new_unsafe("tests/ts.rs:630:10"),
#[cfg(windows)]
ExportPath::new_unsafe("tests\ts.rs:619:10"),
ExportPath::new_unsafe("tests\ts.rs:630:10"),
r#"@odata.context"#.to_string()
)
);
Expand All @@ -305,9 +305,9 @@ fn typescript_types() {
ExportError::InvalidName(
NamedLocation::Type,
#[cfg(not(windows))]
ExportPath::new_unsafe("tests/ts.rs:623:10"),
ExportPath::new_unsafe("tests/ts.rs:634:10"),
#[cfg(windows)]
ExportPath::new_unsafe("tests\ts.rs:623:10"),
ExportPath::new_unsafe("tests\ts.rs:634:10"),
r#"@odata.context"#.to_string()
)
);
Expand Down
Loading