Simplify parameter handling

This commit is contained in:
Joel Wachsler 2022-07-14 23:04:55 +00:00
parent 72074f60c1
commit 4a862d6e36

View File

@ -147,27 +147,26 @@ impl<'a> Parameters<'a> {
#[derive(Debug)] #[derive(Debug)]
struct MandatoryParams<'a> { struct MandatoryParams<'a> {
params: &'a ApiParameters, params: Vec<Parameter<'a>>,
} }
impl<'a> MandatoryParams<'a> { impl<'a> MandatoryParams<'a> {
fn new(params: &'a ApiParameters) -> Self { fn new(params: &'a ApiParameters) -> Self {
Self { params } Self {
params: Parameter::from(&params.mandatory),
}
} }
fn generate_params(&self) -> Vec<TokenStream> { fn generate_params(&self) -> Vec<TokenStream> {
self.params self.params
.mandatory
.iter() .iter()
.map(|p| p.to_parameter().generate_param_with_name()) .map(|p| p.generate_param_with_name())
.collect() .collect()
} }
fn form_builder(&self) -> Vec<TokenStream> { fn form_builder(&self) -> Vec<TokenStream> {
self.params self.params
.mandatory
.iter() .iter()
.map(|p| p.to_parameter())
.map(|param| { .map(|param| {
let name_ident = param.name_ident(); let name_ident = param.name_ident();
let name = param.name(); let name = param.name();
@ -178,9 +177,8 @@ impl<'a> MandatoryParams<'a> {
fn names(&self) -> Vec<TokenStream> { fn names(&self) -> Vec<TokenStream> {
self.params self.params
.mandatory
.iter() .iter()
.map(|p| p.to_parameter().name_ident()) .map(|p| p.name_ident())
.map(|name_ident| quote! { #name_ident }) .map(|name_ident| quote! { #name_ident })
.collect() .collect()
} }
@ -188,37 +186,37 @@ impl<'a> MandatoryParams<'a> {
#[derive(Debug)] #[derive(Debug)]
struct OptionalParams<'a> { struct OptionalParams<'a> {
params: &'a ApiParameters, params: Vec<Parameter<'a>>,
} }
impl<'a> OptionalParams<'a> { impl<'a> OptionalParams<'a> {
fn new(params: &'a ApiParameters) -> Self { fn new(params: &'a ApiParameters) -> Self {
Self { params } Self {
params: Parameter::from(&params.optional),
}
} }
fn generate_builder_methods(&self) -> Vec<TokenStream> { fn generate_builder_methods(&self) -> Vec<TokenStream> {
self.params self.params
.optional
.iter() .iter()
.map(Self::generate_builder_method) .map(Self::generate_builder_method)
.collect() .collect()
} }
fn generate_builder_method(param: &types::Type) -> TokenStream { fn generate_builder_method(param: &Parameter) -> TokenStream {
let parameter = param.to_parameter(); let name = param.name();
let name = parameter.name(); let name_ident = param.name_ident();
let name_ident = parameter.name_ident();
let param_type = util::to_ident(&param.to_borrowed_type()); let param_type = util::to_ident(&param.p_type.to_borrowed_type());
let builder_param = if param.should_borrow() { let builder_param = if param.p_type.should_borrow() {
quote! { &#param_type } quote! { &#param_type }
} else { } else {
quote! { #param_type } quote! { #param_type }
}; };
util::add_docs( util::add_docs(
&param.get_type_info().description, &param.p_type.get_type_info().description,
quote! { quote! {
pub fn #name_ident(mut self, value: #builder_param) -> Self { pub fn #name_ident(mut self, value: #builder_param) -> Self {
self.form = self.form.text(#name, value.to_string()); self.form = self.form.text(#name, value.to_string());
@ -239,6 +237,10 @@ impl<'a> Parameter<'a> {
Self { p_type } Self { p_type }
} }
fn from(parameters: &[types::Type]) -> Vec<Parameter<'_>> {
parameters.iter().map(Parameter::new).collect()
}
fn name(&self) -> String { fn name(&self) -> String {
self.p_type.get_type_info().name.to_snake() self.p_type.get_type_info().name.to_snake()
} }
@ -260,9 +262,3 @@ impl<'a> Parameter<'a> {
quote! { #name_ident: #t } quote! { #name_ident: #t }
} }
} }
impl types::Type {
fn to_parameter(&self) -> Parameter<'_> {
Parameter::new(self)
}
}