From 4a862d6e36c207aa014585b7641dd5221f05e462 Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Thu, 14 Jul 2022 23:04:55 +0000 Subject: [PATCH] Simplify parameter handling --- .../group/method/method_with_params.rs | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs b/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs index a41e3c1..2c72049 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs @@ -147,27 +147,26 @@ impl<'a> Parameters<'a> { #[derive(Debug)] struct MandatoryParams<'a> { - params: &'a ApiParameters, + params: Vec>, } impl<'a> MandatoryParams<'a> { fn new(params: &'a ApiParameters) -> Self { - Self { params } + Self { + params: Parameter::from(¶ms.mandatory), + } } fn generate_params(&self) -> Vec { self.params - .mandatory .iter() - .map(|p| p.to_parameter().generate_param_with_name()) + .map(|p| p.generate_param_with_name()) .collect() } fn form_builder(&self) -> Vec { self.params - .mandatory .iter() - .map(|p| p.to_parameter()) .map(|param| { let name_ident = param.name_ident(); let name = param.name(); @@ -178,9 +177,8 @@ impl<'a> MandatoryParams<'a> { fn names(&self) -> Vec { self.params - .mandatory .iter() - .map(|p| p.to_parameter().name_ident()) + .map(|p| p.name_ident()) .map(|name_ident| quote! { #name_ident }) .collect() } @@ -188,37 +186,37 @@ impl<'a> MandatoryParams<'a> { #[derive(Debug)] struct OptionalParams<'a> { - params: &'a ApiParameters, + params: Vec>, } impl<'a> OptionalParams<'a> { fn new(params: &'a ApiParameters) -> Self { - Self { params } + Self { + params: Parameter::from(¶ms.optional), + } } fn generate_builder_methods(&self) -> Vec { self.params - .optional .iter() .map(Self::generate_builder_method) .collect() } - fn generate_builder_method(param: &types::Type) -> TokenStream { - let parameter = param.to_parameter(); - let name = parameter.name(); - let name_ident = parameter.name_ident(); + fn generate_builder_method(param: &Parameter) -> TokenStream { + let name = param.name(); + let name_ident = param.name_ident(); - let param_type = util::to_ident(¶m.to_borrowed_type()); + let param_type = util::to_ident(¶m.p_type.to_borrowed_type()); - let builder_param = if param.should_borrow() { + let builder_param = if param.p_type.should_borrow() { quote! { &#param_type } } else { quote! { #param_type } }; util::add_docs( - ¶m.get_type_info().description, + ¶m.p_type.get_type_info().description, quote! { pub fn #name_ident(mut self, value: #builder_param) -> Self { self.form = self.form.text(#name, value.to_string()); @@ -239,6 +237,10 @@ impl<'a> Parameter<'a> { Self { p_type } } + fn from(parameters: &[types::Type]) -> Vec> { + parameters.iter().map(Parameter::new).collect() + } + fn name(&self) -> String { self.p_type.get_type_info().name.to_snake() } @@ -260,9 +262,3 @@ impl<'a> Parameter<'a> { quote! { #name_ident: #t } } } - -impl types::Type { - fn to_parameter(&self) -> Parameter<'_> { - Parameter::new(self) - } -}