Skip to content

Commit

Permalink
Update/syn 2.0 (#880)
Browse files Browse the repository at this point in the history
* update syn to 2.0

* feat: codegen upgraded to use syn v2 (close #875)

---
Co-Authored-by: John Vandenberg <jayvdb@gmail.com>
Co-Authored-by: Yatin Maan

* fix: appropriate expect statement (#875)
  • Loading branch information
SyedFasiuddin authored May 9, 2023
1 parent 58068ac commit 8af0b46
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ proc-macro = true
proc-macro-error = "1.0.4"
proc-macro2 = "1.0.47"
quote = "1.0.21"
syn = { version = "1.0.104", features = ["full", "extra-traits"] }
syn = { version = "2.0", features = ["full", "extra-traits"] }

[dev-dependencies]
pretty_assertions = "1.3.0"
Expand Down
30 changes: 16 additions & 14 deletions codegen/src/next/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::collections::HashMap;
use proc_macro_error::emit_error;
use quote::{quote, ToTokens};
use syn::{
parenthesized, parse::Parse, parse2, punctuated::Punctuated, token::Paren, Expr, ExprLit, File,
Ident, Item, ItemFn, Lit, LitStr, Token,
parse::Parse, punctuated::Punctuated, Expr, ExprLit, File, Ident, Item, ItemFn, Lit, LitStr,
Token,
};

#[derive(Debug, Eq, PartialEq)]
Expand All @@ -24,7 +24,6 @@ struct Parameter {
#[derive(Debug, Eq, PartialEq)]
struct Params {
params: Punctuated<Parameter, Token![,]>,
paren_token: Paren,
}

impl Parse for Parameter {
Expand All @@ -39,10 +38,8 @@ impl Parse for Parameter {

impl Parse for Params {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let content;
Ok(Self {
paren_token: parenthesized!(content in input),
params: content.parse_terminated(Parameter::parse)?,
params: input.parse_terminated(Parameter::parse, Token![,])?,
})
}
}
Expand All @@ -56,7 +53,7 @@ impl Endpoint {
// Find the index of an attribute that is an endpoint
for index in 0..item.attrs.len() {
// The endpoint ident should be the last segment in the path
if let Some(segment) = item.attrs[index].path.segments.last() {
if let Some(segment) = item.attrs[index].path().segments.last() {
if segment.ident.to_string().as_str() == "endpoint" {
// TODO: we should allow multiple endpoint attributes per handler.
// We could refactor this to return a Vec<Endpoint> and then check
Expand Down Expand Up @@ -85,7 +82,7 @@ impl Endpoint {
};

// Parse the endpoint's parameters
let params: Params = match parse2(endpoint.tokens) {
let params: Params = match endpoint.parse_args() {
Ok(params) => params,
Err(err) => {
// This will error on invalid parameter syntax
Expand All @@ -95,11 +92,17 @@ impl Endpoint {
};

// We'll use the paren span for errors later
let paren = params.paren_token;
let endpoint_delim_span = &endpoint
.meta
.require_list()
.expect("Endpoint meta should be a list")
.delimiter
.span()
.join();

if params.params.is_empty() {
emit_error!(
paren.span,
endpoint_delim_span,
"missing endpoint arguments";
hint = "The endpoint takes two arguments: `endpoint(method = get, route = \"/hello\")`"
);
Expand Down Expand Up @@ -175,7 +178,7 @@ impl Endpoint {

if route.is_none() {
emit_error!(
paren.span,
endpoint_delim_span,
"no route provided";
hint = "Add a route to your endpoint: `route = \"/hello\")`"
);
Expand All @@ -184,7 +187,7 @@ impl Endpoint {

if method.is_none() {
emit_error!(
paren.span,
endpoint_delim_span,
"no method provided";
hint = "Add a method to your endpoint: `method = get`"
);
Expand Down Expand Up @@ -588,11 +591,10 @@ mod tests {

#[test]
fn parse_params() {
let actual: Params = parse_quote![(method = get, route = "/hello")];
let actual: Params = parse_quote![method = get, route = "/hello"];

let mut expected = Params {
params: Default::default(),
paren_token: Default::default(),
};
expected.params.push(parse_quote!(method = get));
expected.params.push(parse_quote!(route = "/hello"));
Expand Down
24 changes: 9 additions & 15 deletions codegen/src/shuttle_main/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use proc_macro::TokenStream;
use proc_macro_error::emit_error;
use quote::{quote, ToTokens};
use syn::{
parenthesized, parse::Parse, parse2, parse_macro_input, parse_quote, punctuated::Punctuated,
spanned::Spanned, token::Paren, Attribute, Expr, ExprLit, FnArg, Ident, ItemFn, Lit, Pat,
PatIdent, Path, ReturnType, Signature, Stmt, Token, Type, TypePath,
parse::Parse, parse_macro_input, parse_quote, punctuated::Punctuated, spanned::Spanned,
Attribute, Expr, ExprLit, FnArg, Ident, ItemFn, Lit, Pat, PatIdent, Path, ReturnType,
Signature, Stmt, Token, Type, TypePath,
};

pub(crate) fn r#impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -52,9 +52,6 @@ struct Builder {

#[derive(Debug, Default, PartialEq)]
struct BuilderOptions {
/// Parenthesize around options
paren_token: Paren,

/// The actual options
options: Punctuated<BuilderOption, Token![,]>,
}
Expand All @@ -70,11 +67,8 @@ struct BuilderOption {

impl Parse for BuilderOptions {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let content;

Ok(Self {
paren_token: parenthesized!(content in input),
options: content.parse_terminated(BuilderOption::parse)?,
options: input.parse_terminated(BuilderOption::parse, Token![,])?,
})
}
}
Expand Down Expand Up @@ -169,14 +163,14 @@ fn attribute_to_builder(pat_ident: &PatIdent, attrs: Vec<Attribute>) -> syn::Res
));
}

let options = if attrs[0].tokens.is_empty() {
let options = if attrs[0].meta.require_list().is_err() {
Default::default()
} else {
parse2(attrs[0].tokens.clone())?
attrs[0].parse_args()?
};

let builder = Builder {
path: attrs[0].path.clone(),
path: attrs[0].path().clone(),
options,
};

Expand Down Expand Up @@ -437,14 +431,14 @@ mod tests {

#[test]
fn parse_builder_options() {
let input: BuilderOptions = parse_quote!((
let input: BuilderOptions = parse_quote!(
string = "string_val",
boolean = true,
integer = 5,
float = 2.65,
enum_variant = SomeEnum::Variant1,
sensitive = "user:{secrets.password}"
));
);

let mut expected: BuilderOptions = Default::default();
expected.options.push(parse_quote!(string = "string_val"));
Expand Down

0 comments on commit 8af0b46

Please sign in to comment.