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

Commit

Permalink
feat(cargo-swagg): print query params struct and type
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Mar 22, 2020
1 parent be20eb5 commit ad6f6ef
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
11 changes: 11 additions & 0 deletions cargo-swagg/out.rs
Expand Up @@ -175,5 +175,16 @@ pub mod paths {
Answer::new(self).status(status).content_type(content_type)
}
}
use super::components::parameters;
#[derive(Debug, Deserialize)]
pub struct QueryParams {
#[doc = "response_type is set to code indicating that you want an authorization code as the response."]
#[serde(rename = "responseType")]
pub response_type: parameters::OauthResponseType,
pub redirect_uri: Option<parameters::OauthRedirectUri>,
#[serde(rename = "GlobalNameOfTheUniverse")]
pub global_name_of_the_universe: Option<parameters::OauthClientId>,
}
pub type Query = actix_web::http::Query<QueryParams>;
}
}
26 changes: 25 additions & 1 deletion cargo-swagg/src/main.rs
Expand Up @@ -9,7 +9,7 @@ use printer::{
parameters::ParametersModule, request_bodies::RequestBodiesModule, responses::ResponsesModule, Component,
ComponentsModule, EnumVariant, Field, FieldType, FormatFloat, FormatInteger, FormatString, NativeType,
},
paths::{ContentType, Path, PathsModule, ResponseEnum, ResponseStatus, StatusVariant},
paths::{ContentType, Path, PathsModule, QueryParam, ResponseEnum, ResponseStatus, StatusVariant},
GeneratedModule, Printable,
};

Expand Down Expand Up @@ -209,6 +209,7 @@ fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {

let p1 = Path {
name: "registerConfirmation".to_owned(),
query_params: vec![],
response: ResponseEnum {
responses: vec![
StatusVariant {
Expand Down Expand Up @@ -238,6 +239,29 @@ fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {

let p2 = Path {
name: "sessionCreate".to_owned(),
query_params: vec![
QueryParam {
name: "responseType".to_owned(),
type_ref: "OAuthResponseType".to_owned(),
description: Some(
"response_type is set to code indicating that you want an authorization code as the response."
.to_owned(),
),
required: true,
},
QueryParam {
name: "redirect_uri".to_owned(),
type_ref: "OAuthRedirectUri".to_owned(),
description: None,
required: false,
},
QueryParam {
name: "GlobalNameOfTheUniverse".to_owned(),
type_ref: "OAuthClientId".to_owned(),
description: None,
required: false,
},
],
response: ResponseEnum {
responses: vec![
StatusVariant {
Expand Down
65 changes: 65 additions & 0 deletions cargo-swagg/src/printer/paths/path.rs
Expand Up @@ -6,6 +6,7 @@ use quote::{format_ident, quote};
pub struct Path {
pub name: String,
pub response: ResponseEnum,
pub query_params: Vec<QueryParam>,
}

impl Path {
Expand Down Expand Up @@ -36,6 +37,25 @@ impl Path {
}
}
}

fn query_params_impl(&self) -> proc_macro2::TokenStream {
if self.query_params.is_empty() {
quote! {}
} else {
let query_params = self.query_params.print();

quote! {
use super::components::parameters;

#[derive(Debug, Deserialize)]
pub struct QueryParams {
#query_params
}

pub type Query = actix_web::http::Query<QueryParams>;
}
}
}
}

impl Printable for Path {
Expand All @@ -44,6 +64,7 @@ impl Printable for Path {
let enum_variants = self.print_enum_variants();
let status_match = self.print_status_variants();
let content_type_match = self.print_content_type_variants();
let query_params = self.query_params_impl();

quote! {
pub mod #module_name {
Expand All @@ -67,6 +88,8 @@ impl Printable for Path {
Answer::new(self).status(status).content_type(content_type)
}
}

#query_params
}
}

Expand Down Expand Up @@ -185,3 +208,45 @@ impl Printable for ContentType {
quote! { #ident }
}
}

pub struct QueryParam {
/// Name of the parameter in the query, can be in any case, will be converted to snake_case
pub name: String,

/// should be reference to type in `components::parameters` module
/// Will be converted to PascalCase
pub type_ref: String,

pub description: Option<String>,

pub required: bool,
}

impl Printable for QueryParam {
fn print(&self) -> proc_macro2::TokenStream {
let name_original = self.name.clone();
let name_snake = name_original.to_snake_case();
let name_ident = format_ident!("{}", name_snake);
let rename = match name_snake != name_original {
true => quote! { #[serde(rename = #name_original)] },
false => quote! {},
};

let type_name = format_ident!("{}", self.type_ref.to_pascal_case());
let description = match &self.description {
Some(description) => quote! { #[doc = #description]},
None => quote! {},
};

let type_result = match self.required {
true => quote! { parameters::#type_name },
false => quote! { Option<parameters::#type_name> },
};

quote! {
#description
#rename
pub #name_ident: #type_result,
}
}
}

0 comments on commit ad6f6ef

Please sign in to comment.