Skip to content

Commit

Permalink
Backport good stuff from #252
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Jul 9, 2024
1 parent 7839347 commit ca9aa4b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
21 changes: 16 additions & 5 deletions specta-macros/src/specta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,27 @@ pub fn attribute(item: proc_macro::TokenStream) -> syn::Result<proc_macro::Token
None => false,
};

Check warning on line 43 in specta-macros/src/specta.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant pattern matching, consider using `is_some()`

warning: redundant pattern matching, consider using `is_some()` --> specta-macros/src/specta.rs:40:30 | 40 | let function_asyncness = match function.sig.asyncness { | ______________________________^ 41 | | Some(_) => true, 42 | | None => false, 43 | | }; | |_____^ help: try: `function.sig.asyncness.is_some()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching = note: `-W clippy::redundant-pattern-matching` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::redundant_pattern_matching)]`

Check warning on line 43 in specta-macros/src/specta.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant pattern matching, consider using `is_some()`

warning: redundant pattern matching, consider using `is_some()` --> specta-macros/src/specta.rs:40:30 | 40 | let function_asyncness = match function.sig.asyncness { | ______________________________^ 41 | | Some(_) => true, 42 | | None => false, 43 | | }; | |_____^ help: try: `function.sig.asyncness.is_some()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching = note: `-W clippy::redundant-pattern-matching` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::redundant_pattern_matching)]`

let arg_names = function.sig.inputs.iter().map(|input| {
let mut arg_names = Vec::new();
for input in function.sig.inputs.iter() {
let arg = match input {
FnArg::Receiver(_) => unreachable!("Commands cannot take 'self'"),
FnArg::Receiver(_) => {
return Err(syn::Error::new_spanned(
input,
"functions with `#[specta]` 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"),
_ => {
return Err(syn::Error::new_spanned(
input,
"functions with `#[specta]` must take named arguments",
))
}
},
};

Expand All @@ -63,8 +74,8 @@ pub fn attribute(item: proc_macro::TokenStream) -> syn::Result<proc_macro::Token
s
};

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

Check warning on line 77 in specta-macros/src/specta.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

warning: used `unwrap()` on a `Result` value --> specta-macros/src/specta.rs:77:24 | 77 | arg_names.push(TokenStream::from_str(&s).unwrap()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is an `Err`, it will panic = help: consider using `expect()` to provide a better panic message = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used = note: requested on the command line with `-W clippy::unwrap-used`

Check warning on line 77 in specta-macros/src/specta.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

warning: used `unwrap()` on a `Result` value --> specta-macros/src/specta.rs:77:24 | 77 | arg_names.push(TokenStream::from_str(&s).unwrap()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is an `Err`, it will panic = help: consider using `expect()` to provide a better panic message = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used = note: requested on the command line with `-W clippy::unwrap-used`
}

let arg_signatures = function.sig.inputs.iter().map(|_| quote!(_));

Expand Down
12 changes: 6 additions & 6 deletions specta-typescript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,23 @@ pub fn export_function_header(
.comment_exporter
.map(|v| {
v(CommentFormatterArgs {
docs: &dt.docs,
deprecated: dt.deprecated.as_ref(),
docs: &dt.docs(),

Check warning on line 150 in specta-typescript/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> specta-typescript/src/lib.rs:150:23 | 150 | docs: &dt.docs(), | ^^^^^^^^^^ help: change this to: `dt.docs()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-W clippy::needless-borrow` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::needless_borrow)]`
deprecated: dt.deprecated(),
})
})
.unwrap_or_default();

s.push_str("export ");

if dt.asyncness {
if dt.asyncness() {
s.push_str("async ");
}

s.push_str("function ");

s.push_str(&dt.name);
s.push_str(&dt.name());

Check warning on line 164 in specta-typescript/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> specta-typescript/src/lib.rs:164:16 | 164 | s.push_str(&dt.name()); | ^^^^^^^^^^ help: change this to: `dt.name()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
s.push_str("(");

Check warning on line 165 in specta-typescript/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

calling `push_str()` using a single-character string literal

warning: calling `push_str()` using a single-character string literal --> specta-typescript/src/lib.rs:165:5 | 165 | s.push_str("("); | ^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `s.push('(')` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str = note: `-W clippy::single-char-add-str` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::single_char_add_str)]`
for (i, (name, ty)) in dt.args.into_iter().enumerate() {
for (i, (name, ty)) in dt.args().enumerate() {
if i != 0 {
s.push_str(", ");
}
Expand All @@ -174,7 +174,7 @@ pub fn export_function_header(
}
s.push_str(")");

Check warning on line 175 in specta-typescript/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

calling `push_str()` using a single-character string literal

warning: calling `push_str()` using a single-character string literal --> specta-typescript/src/lib.rs:175:5 | 175 | s.push_str(")"); | ^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `s.push(')')` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str

if let Some(ty) = dt.result {
if let Some(ty) = dt.result() {
s.push_str(": ");
s.push_str(&datatype(config, &ty, &type_map)?);

Check warning on line 179 in specta-typescript/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> specta-typescript/src/lib.rs:179:38 | 179 | s.push_str(&datatype(config, &ty, &type_map)?); | ^^^ help: change this to: `ty` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
}
Expand Down
44 changes: 37 additions & 7 deletions specta/src/function/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! Support for exporting Rust function.
//!
//! TODO: Docs. Talk about how Specta doesn't export functions but it helps you to.

mod arg;
pub(crate) mod result;

Expand Down Expand Up @@ -88,23 +92,49 @@ pub type CollectFunctionsResult = (Vec<FunctionDataType>, TypeMap);
pub use crate::collect_functions;
use crate::{DataType, DeprecatedType, TypeMap};

// TODO: Probs move this into the `DataType` module???
// TODO: Probs move this into the `DataType` module??? // TODO
/// Contains type information about a function annotated with [`specta`](macro@crate::specta).
/// Returned by [`fn_datatype`].
#[derive(Debug, Clone)]
pub struct FunctionDataType {
/// Whether the function is async.
pub asyncness: bool,
asyncness: bool,
/// The function's name.
pub name: Cow<'static, str>,
name: Cow<'static, str>,
/// The name and type of each of the function's arguments.
pub args: Vec<(Cow<'static, str>, DataType)>,
args: Vec<(Cow<'static, str>, DataType)>,
/// The return type of the function.
pub result: Option<DataType>,
result: Option<DataType>,
/// The function's documentation. Detects both `///` and `#[doc = ...]` style documentation.
pub docs: Cow<'static, str>,
docs: Cow<'static, str>,
/// The deprecated status of the function.
pub deprecated: Option<DeprecatedType>,
deprecated: Option<DeprecatedType>,
}

impl FunctionDataType {
pub fn asyncness(&self) -> bool {
self.asyncness
}

pub fn name(&self) -> &Cow<'static, str> {
&self.name
}

pub fn args(&self) -> impl Iterator<Item = &(Cow<'static, str>, DataType)> {
self.args.iter()
}

pub fn result(&self) -> Option<&DataType> {
self.result.as_ref()
}

pub fn docs(&self) -> &Cow<'static, str> {
&self.docs
}

pub fn deprecated(&self) -> Option<&DeprecatedType> {
self.deprecated.as_ref()
}
}

/// Implemented by functions that can be annoatated with [`specta`](crate::specta).
Expand Down

0 comments on commit ca9aa4b

Please sign in to comment.