Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed May 11, 2023
1 parent 1e11212 commit f3bd577
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
17 changes: 15 additions & 2 deletions macros/src/specta.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// inspired by https://github.com/tauri-apps/tauri/blob/2901145c497299f033ba7120af5f2e7ead16c75a/core/tauri-macros/src/command/handler.rs

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

use crate::utils::format_fn_wrapper;

Expand All @@ -23,7 +23,20 @@ pub fn attribute(item: proc_macro::TokenStream) -> syn::Result<proc_macro::Token

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

let arg_signatures = function.sig.inputs.iter().map(|_| quote!(_));
Expand Down
44 changes: 43 additions & 1 deletion tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,48 @@ mod test {
#[specta]
fn e<T: fmt::Debug>(window: T) {}

// https://github.com/oscartbeaumont/tauri-specta/issues/24
#[specta]
#[allow(unused_mut)]
fn f(mut demo: String) -> i32 {
42
}

#[specta]
#[allow(unused_mut)]
fn g(x: std::string::String) {}

macro_rules! special_string {
() => {
String
};
}

#[specta]
#[allow(unused_mut)]
fn h(demo: special_string!()) {}

// TODO: Finish fixing these

// #[derive(Type)]
// pub struct Demo {
// pub demo: String,
// }

// #[specta]
// #[allow(unused_mut)]
// fn i(Demo { demo }: Demo) {}

// macro_rules! special_destructure {
// () => {
// Demo { demo }
// };
// }

// #[specta]
// #[allow(unused_mut)]
// fn j(special_destructure!(): Demo) {}

#[test]
fn test_trailing_comma() {
functions::collect_types![a, b, c].unwrap();
Expand All @@ -31,7 +73,7 @@ mod test {

#[test]
fn test_function_export() {
let (functions, types) = functions::collect_types![a, b, c, d, e::<i32>].unwrap();
let (functions, types) = functions::collect_types![a, b, c, d, e::<i32>, f, g, h].unwrap();

assert_eq!(functions[0].docs, vec![" Multiline", " Docs"]);

Expand Down

0 comments on commit f3bd577

Please sign in to comment.