Skip to content

Feature unsynn quote #759

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 1 addition & 3 deletions facet-macros-parse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ keywords = [
categories = ["development-tools", "encoding"]

[dependencies]
unsynn = "0.1.0"
proc-macro2 = "1.0.95"
quote = "1.0.40"
unsynn = "0.2"

[features]
function = []
2 changes: 0 additions & 2 deletions facet-macros-parse/src/function/fn_shape_input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use proc_macro2::TokenStream;
use unsynn::*;

// Re-use the generics parser from our other module
Expand Down Expand Up @@ -42,7 +41,6 @@ pub fn parse_fn_shape_input(input: TokenStream) -> ParsedFnShapeInput {
#[cfg(test)]
mod tests {
use super::*;
use quote::quote;

#[test]
fn test_simple_function_name() {
Expand Down
5 changes: 1 addition & 4 deletions facet-macros-parse/src/function/func_body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::VerbatimUntil;
#[cfg(test)]
use proc_macro2::TokenStream;
use unsynn::*;

unsynn! {
Expand Down Expand Up @@ -29,15 +27,14 @@ pub fn parse_function_body(tokens: &[TokenTree]) -> TokenStream {
Ok(func_with_body) => func_with_body.body.to_token_stream(),
Err(_) => {
// No function body found, return empty braces
quote::quote! { {} }
quote! { {} }
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use quote::quote;

#[test]
fn test_simple_function_body() {
Expand Down
2 changes: 0 additions & 2 deletions facet-macros-parse/src/function/func_params.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::VerbatimUntil;
use proc_macro2::TokenStream;
use unsynn::*;

unsynn! {
Expand Down Expand Up @@ -36,7 +35,6 @@ pub fn parse_fn_parameters(params_ts: TokenStream) -> Vec<Parameter> {
#[cfg(test)]
mod tests {
use super::*;
use quote::quote;

#[test]
fn test_empty_parameters() {
Expand Down
4 changes: 1 addition & 3 deletions facet-macros-parse/src/function/func_sig.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use proc_macro2::TokenStream;
use unsynn::*;

// Re-use the types from our other modules
Expand Down Expand Up @@ -100,7 +99,7 @@ pub fn parse_function_signature(input: TokenStream) -> FunctionSignature {
let return_type = sig
.return_type
.map(|rt| rt.return_type.to_token_stream())
.unwrap_or_else(|| quote::quote! { () });
.unwrap_or_else(|| quote! { () });

// Extract body
let body = sig.body.to_token_stream();
Expand All @@ -126,7 +125,6 @@ pub fn parse_function_signature(input: TokenStream) -> FunctionSignature {
#[cfg(test)]
mod tests {
use super::*;
use quote::quote;

#[test]
fn test_simple_function() {
Expand Down
3 changes: 0 additions & 3 deletions facet-macros-parse/src/function/generics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::VerbatimUntil;
#[cfg(test)]
use proc_macro2::TokenStream;
use unsynn::*;

unsynn! {
Expand Down Expand Up @@ -41,7 +39,6 @@ pub fn parse_generics_for_test(input: TokenStream) -> Option<GenericParams> {
#[cfg(test)]
mod tests {
use super::*;
use quote::quote;

#[test]
fn test_no_generics() {
Expand Down
5 changes: 1 addition & 4 deletions facet-macros-parse/src/function/ret_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::VerbatimUntil;
#[cfg(test)]
use proc_macro2::TokenStream;
use unsynn::*;

unsynn! {
Expand Down Expand Up @@ -28,15 +26,14 @@ pub fn parse_return_type(tokens: Vec<TokenTree>) -> TokenStream {
Ok(ret_type) => ret_type.return_type.to_token_stream(),
Err(_) => {
// No return type found, default to unit type
quote::quote! { () }
quote! { () }
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use quote::quote;

#[test]
fn test_no_return_type() {
Expand Down
13 changes: 5 additions & 8 deletions facet-macros-parse/src/function/type_params.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use proc_macro2::TokenStream;
use unsynn::*;

// Re-use the generics parser
Expand All @@ -11,33 +10,31 @@ pub fn extract_type_params(generics_ts: TokenStream) -> TokenStream {

match it.parse::<GenericParams>() {
Ok(generics) => {
let type_param_names: Vec<_> = generics
let type_param_names: CommaDelimitedVec<_> = generics
.params
.0
.into_iter()
.map(|delim| delim.value.name)
.collect();

if type_param_names.is_empty() {
quote::quote! { () }
quote! { () }
} else if type_param_names.len() == 1 {
let param = &type_param_names[0];
quote::quote! { #param }
quote! { #type_param_names }
} else {
quote::quote! { ( #( #type_param_names ),* ) }
quote! { ( #type_param_names ) }
}
}
Err(_) => {
// Fallback to unit type if parsing fails
quote::quote! { () }
quote! { () }
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use quote::quote;

#[test]
fn test_single_type_param() {
Expand Down
1 change: 0 additions & 1 deletion facet-macros-parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,6 @@ impl Struct {
#[cfg(test)]
mod tests {
use super::*;
use quote::quote;

#[test]
fn test_struct_with_field_doc_comments() {
Expand Down
Loading