Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
refactor(cargo-swagg): use simplified syntax with quote
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Mar 21, 2020
1 parent b34537c commit f8a26e3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 87 deletions.
18 changes: 5 additions & 13 deletions cargo-swagg/src/printer/api/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ impl ToString for HttpMethod {
}

pub struct BindApiMethod {
pub(crate) method: HttpMethod,
pub(crate) path: String,
pub(crate) name: String,
pub method: HttpMethod,
pub path: String,
pub name: String,
}

impl Printable for BindApiMethod {
Expand Down Expand Up @@ -60,24 +60,16 @@ pub struct ImplApiMethods {
impl Printable for ImplApiMethods {
fn print(&self) -> proc_macro2::TokenStream {
let api_name = format_ident!("{}", to_struct_name(self.api_name.to_owned()));
let mut tokens = quote! {};
let methods = self.methods.print();

for method in &self.methods {
let method_tokens = method.print();
tokens = quote! {
#tokens

#method_tokens
};
}
quote! {
use actix_web::{FromRequest, dev::Factory};
use actix_swagger::{Answer, Method};
use std::future::Future;
use super::paths;

impl #api_name {
#tokens
#methods
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions cargo-swagg/src/printer/components/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,15 @@ impl Printable for FieldType {
fn path_to_stream(path: String) -> proc_macro2::TokenStream {
if path.contains("::") {
let mut parts = path.split("::");

let first = parts.next().expect("Path split to parts requires first element");
let first_ident = format_ident!("{}", first);

let mut stream = quote! { #first_ident };
let rest = parts.map(|p| format_ident!("{}", p));

for item in parts {
let ident = format_ident!("{}", item);
stream = quote! { #stream::#ident };
quote! {
#first_ident #(::#rest)*
}

stream
} else {
// Can panic if identifier is incorrect.
// TODO: add regexp check for input
Expand Down
13 changes: 2 additions & 11 deletions cargo-swagg/src/printer/paths/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@ pub struct PathsModule {

impl Printable for PathsModule {
fn print(&self) -> proc_macro2::TokenStream {
let mut tokens = quote! {};

for path in &self.paths {
let printed = path.print();

tokens = quote! {
#tokens
#printed
};
}
let paths = self.paths.print();

quote! {
pub mod paths {
#tokens
#paths
}
}
}
Expand Down
79 changes: 22 additions & 57 deletions cargo-swagg/src/printer/paths/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,14 @@ pub struct Path {

impl Path {
fn print_enum_variants(&self) -> proc_macro2::TokenStream {
let mut tokens = quote! {};
let variants = self.response.responses.iter().map(|r| r.print_enum_variant());

for status in &self.response.responses {
let variant = status.print_enum_variant();

tokens = quote! {
#tokens
#variant,
};
}

tokens
quote! { #(#variants,)* }
}

fn print_status_variants(&self) -> proc_macro2::TokenStream {
let mut tokens = quote! {};

for status in &self.response.responses {
let variant = status.print_status_variant();

tokens = quote! {
#tokens
#variant,
};
}
let variants = self.response.responses.iter().map(|r| r.print_status_variant());
let tokens = quote! { #(#variants,)* };

quote! {
match self {
Expand All @@ -44,16 +27,8 @@ impl Path {
}

fn print_content_type_variants(&self) -> proc_macro2::TokenStream {
let mut tokens = quote! {};

for status in &self.response.responses {
let variant = status.print_content_type_variant();

tokens = quote! {
#tokens
#variant,
}
}
let variants = self.response.responses.iter().map(|r| r.print_content_type_variant());
let tokens = quote! { #(#variants,)* };

quote! {
match self {
Expand All @@ -77,10 +52,6 @@ impl Printable for Path {
use actix_web::http::StatusCode;
use serde::Serialize;





#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum Response {
Expand Down Expand Up @@ -176,29 +147,19 @@ impl StatusVariant {
let variant_name = self.name();
let status = format_ident!("{}", self.status.to_string().to_constant_case());

if let Some(_) = self.response_type_name {
quote! {
Self::#variant_name(_) => StatusCode::#status
}
} else {
quote! {
Self::#variant_name => StatusCode::#status
}
match self.response_type_name {
Some(_) => quote! { Self::#variant_name(_) => StatusCode::#status },
None => quote! { Self::#variant_name => StatusCode::#status },
}
}

pub fn print_content_type_variant(&self) -> proc_macro2::TokenStream {
let variant_name = self.name();
let content_type = self.content_type();

if let Some(_) = self.response_type_name {
quote! {
Self::#variant_name(_) => #content_type
}
} else {
quote! {
Self::#variant_name => #content_type
}
match self.response_type_name {
Some(_) => quote! { Self::#variant_name(_) => #content_type },
None => quote! { Self::#variant_name => #content_type },
}
}
}
Expand All @@ -208,14 +169,18 @@ pub enum ContentType {
Json,
}

impl ToString for ContentType {
fn to_string(&self) -> String {
match self {
ContentType::Json => "Json",
}
.to_owned()
}
}

impl Printable for ContentType {
fn print(&self) -> proc_macro2::TokenStream {
let ident = format_ident!(
"{}",
match self {
Self::Json => "Json",
}
);
let ident = format_ident!("{}", self.to_string());

quote! { #ident }
}
Expand Down

0 comments on commit f8a26e3

Please sign in to comment.