Skip to content

Commit

Permalink
Handle raw function arguments property (#250)
Browse files Browse the repository at this point in the history
* handle raw function arguments property

* it's good

---------

Co-authored-by: Oscar Beaumont <[email protected]>
  • Loading branch information
Brendonovich and oscartbeaumont authored Jun 24, 2024
1 parent d1bfae8 commit a9022e9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 44 deletions.
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");
}
}
}
60 changes: 30 additions & 30 deletions tests/macro/compile_error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ error[E0277]: the trait bound `UnitExternal: specta::Flatten` is not satisfied
| ^^^^^^^^^^^^ the trait `specta::Flatten` is not implemented for `UnitExternal`
|
= help: the following other types implement trait `specta::Flatten`:
toml::map::Map<K, V>
Arc<T>
BTreeMap<K, V>
Box<T>
Cell<T>
Duration
FlattenExternal
toml_datetime::datetime::Datetime
FlattenUntagged
FlattenInternal
Box<T>
serde_json::map::Map<K, V>
HashMap<K, V>
FlattenUntagged
and $N others
note: required by a bound in `_::<impl specta::Type for FlattenExternal>::inline::validate_flatten`
--> tests/macro/compile_error.rs:29:10
Expand All @@ -106,14 +106,14 @@ error[E0277]: the trait bound `UnnamedMultiExternal: specta::Flatten` is not sat
| ^^^^^^^^^^^^^^^^^^^^ the trait `specta::Flatten` is not implemented for `UnnamedMultiExternal`
|
= help: the following other types implement trait `specta::Flatten`:
toml::map::Map<K, V>
Arc<T>
BTreeMap<K, V>
Box<T>
Cell<T>
Duration
FlattenExternal
toml_datetime::datetime::Datetime
FlattenUntagged
FlattenInternal
Box<T>
serde_json::map::Map<K, V>
HashMap<K, V>
FlattenUntagged
and $N others
note: required by a bound in `_::<impl specta::Type for FlattenExternal>::inline::validate_flatten`
--> tests/macro/compile_error.rs:29:10
Expand All @@ -129,14 +129,14 @@ error[E0277]: the trait bound `UnnamedUntagged: specta::Flatten` is not satisfie
| ^^^^^^^^^^^^^^^ the trait `specta::Flatten` is not implemented for `UnnamedUntagged`
|
= help: the following other types implement trait `specta::Flatten`:
toml::map::Map<K, V>
Arc<T>
BTreeMap<K, V>
Box<T>
Cell<T>
Duration
FlattenExternal
toml_datetime::datetime::Datetime
FlattenUntagged
FlattenInternal
Box<T>
serde_json::map::Map<K, V>
HashMap<K, V>
FlattenUntagged
and $N others
note: required by a bound in `_::<impl specta::Type for FlattenUntagged>::inline::validate_flatten`
--> tests/macro/compile_error.rs:49:10
Expand All @@ -152,14 +152,14 @@ error[E0277]: the trait bound `UnnamedMultiUntagged: specta::Flatten` is not sat
| ^^^^^^^^^^^^^^^^^^^^ the trait `specta::Flatten` is not implemented for `UnnamedMultiUntagged`
|
= help: the following other types implement trait `specta::Flatten`:
toml::map::Map<K, V>
Arc<T>
BTreeMap<K, V>
Box<T>
Cell<T>
Duration
FlattenExternal
toml_datetime::datetime::Datetime
FlattenUntagged
FlattenInternal
Box<T>
serde_json::map::Map<K, V>
HashMap<K, V>
FlattenUntagged
and $N others
note: required by a bound in `_::<impl specta::Type for FlattenUntagged>::inline::validate_flatten`
--> tests/macro/compile_error.rs:49:10
Expand All @@ -175,14 +175,14 @@ error[E0277]: the trait bound `UnnamedInternal: specta::Flatten` is not satisfie
| ^^^^^^^^^^^^^^^ the trait `specta::Flatten` is not implemented for `UnnamedInternal`
|
= help: the following other types implement trait `specta::Flatten`:
toml::map::Map<K, V>
Arc<T>
BTreeMap<K, V>
Box<T>
Cell<T>
Duration
FlattenExternal
toml_datetime::datetime::Datetime
FlattenUntagged
FlattenInternal
Box<T>
serde_json::map::Map<K, V>
HashMap<K, V>
FlattenUntagged
and $N others
note: required by a bound in `_::<impl specta::Type for FlattenInternal>::inline::validate_flatten`
--> tests/macro/compile_error.rs:67:10
Expand Down
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

0 comments on commit a9022e9

Please sign in to comment.