From bd7b7f1d64534ca14bfa14eb8bada4d7f095b4c6 Mon Sep 17 00:00:00 2001 From: koenichiwa <47349524+koenichiwa@users.noreply.github.com> Date: Tue, 3 Oct 2023 02:04:11 +0200 Subject: [PATCH 1/3] Add documentation --- .../src/generator/builder_generator.rs | 31 ++++++++++++++++++- .../src/generator/target_generator.rs | 4 ++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/const_typed_builder_derive/src/generator/builder_generator.rs b/const_typed_builder_derive/src/generator/builder_generator.rs index e182149..a1df40b 100644 --- a/const_typed_builder_derive/src/generator/builder_generator.rs +++ b/const_typed_builder_derive/src/generator/builder_generator.rs @@ -92,7 +92,13 @@ impl<'a> BuilderGenerator<'a> { let vis = self.target_vis; + let documentation = format!( + "Builder for [`{}`] derived using the `const_typed_builder` crate", + self.target_name + ); + quote!( + #[doc = #documentation] #vis struct #builder_name #impl_generics #where_clause { #data_field: #data_name #type_generics } @@ -121,9 +127,14 @@ impl<'a> BuilderGenerator<'a> { let type_generics = self.generics_gen.const_generics_valued(false); let (impl_generics, _, where_clause) = self.generics_gen.target_generics().split_for_impl(); + let documentation = format!( + "Creates a new [`{}`] without any fields set", + self.builder_name + ); quote!( impl #impl_generics #builder_name #type_generics #where_clause{ + #[doc = #documentation] pub fn new() -> #builder_name #type_generics { Self::default() } @@ -131,6 +142,7 @@ impl<'a> BuilderGenerator<'a> { impl #impl_generics Default for #builder_name #type_generics #where_clause { fn default() -> Self { + #[doc = #documentation] #builder_name { #data_field: #data_name::default(), } @@ -144,6 +156,10 @@ impl<'a> BuilderGenerator<'a> { let builder_name = self.builder_name; let target_name = self.target_name; let data_field = self.data_field_ident(); + let documentation = format!( + "Build an instance of [`{}`], consuming the [`{}`]", + self.target_name, self.builder_name + ); let (impl_generics, target_type_generics, where_clause) = self.generics_gen.target_generics().split_for_impl(); @@ -159,7 +175,7 @@ impl<'a> BuilderGenerator<'a> { quote!( impl #impl_generics #builder_name #type_generics #where_clause{ - + #[doc = #documentation] pub fn build(self) -> #target_name #target_type_generics { self.#data_field.into() } @@ -194,6 +210,7 @@ impl<'a> BuilderGenerator<'a> { #correctness_verifier #correctness_helper_fns + #[doc = #documentation] pub fn build(self) -> #target_name #target_type_generics { #correctness_check self.#data_field.into() @@ -223,8 +240,20 @@ impl<'a> BuilderGenerator<'a> { let input_type = self.field_gen.builder_set_impl_input_type(field); let input_value = self.field_gen.builder_set_impl_input_value(field); + let documentation = format!(r#" +Setter for the [`{}::{field_name}`] field. + +# Arguments + +- `{field_name}`: field to be set + +# Returns + +`Self` with `{field_name}` initialized"#, self.target_name); + let tokens = quote!( impl #const_idents_impl #builder_name #const_idents_type_input #where_clause { + #[doc = #documentation] pub fn #field_name (self, #input_type) -> #builder_name #const_idents_type_output { let mut #data_field = self.#data_field; #data_field.#field_name = #input_value; diff --git a/const_typed_builder_derive/src/generator/target_generator.rs b/const_typed_builder_derive/src/generator/target_generator.rs index be71d25..7545bdd 100644 --- a/const_typed_builder_derive/src/generator/target_generator.rs +++ b/const_typed_builder_derive/src/generator/target_generator.rs @@ -48,14 +48,16 @@ impl<'a> TargetGenerator<'a> { let target_name = self.target_name; let builder_name = self.builder_name; let const_generics = self.generics_gen.const_generics_valued(false); - let _builder_generics = self.generics_gen.builder_struct_generics(); + // let _builder_generics = self.generics_gen.builder_struct_generics(); let (impl_generics, type_generics, where_clause) = self.generics_gen.target_generics().split_for_impl(); + let documentation = format!("Creates an instance of [`{}`]", self.builder_name); quote! { impl #impl_generics Builder for #target_name #type_generics #where_clause { type BuilderImpl = #builder_name #const_generics; + #[doc = #documentation] fn builder() -> Self::BuilderImpl { Self::BuilderImpl::new() } From 0281d5eef4202610fdc788793549d5188abc5e81 Mon Sep 17 00:00:00 2001 From: koenichiwa <47349524+koenichiwa@users.noreply.github.com> Date: Tue, 3 Oct 2023 02:04:20 +0200 Subject: [PATCH 2/3] Hide FooData --- const_typed_builder_derive/src/generator/data_generator.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/const_typed_builder_derive/src/generator/data_generator.rs b/const_typed_builder_derive/src/generator/data_generator.rs index 9722aa1..0a867e3 100644 --- a/const_typed_builder_derive/src/generator/data_generator.rs +++ b/const_typed_builder_derive/src/generator/data_generator.rs @@ -67,6 +67,7 @@ impl<'a> DataGenerator<'a> { let tokens = quote!( impl #impl_generics From<#data_name #type_generics> for #struct_name #type_generics #where_clause { + #[doc(hidden)] fn from(data: #data_name #type_generics) -> #struct_name #type_generics { #struct_name { #(#from_fields),* @@ -75,6 +76,7 @@ impl<'a> DataGenerator<'a> { } impl #impl_generics Default for #data_name #type_generics #where_clause { + #[doc(hidden)] fn default() -> Self { #data_name { #def_fields @@ -94,6 +96,7 @@ impl<'a> DataGenerator<'a> { self.generics_gen.target_generics().split_for_impl(); let tokens = quote!( + #[doc(hidden)] pub struct #data_name #impl_generics #where_clause{ #(#fields),* } From 79e3ab0955b8261631fbaeaa4f291721a57e4394 Mon Sep 17 00:00:00 2001 From: koenichiwa <47349524+koenichiwa@users.noreply.github.com> Date: Tue, 3 Oct 2023 02:05:04 +0200 Subject: [PATCH 3/3] Change a word --- const_typed_builder_derive/src/generator/builder_generator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/const_typed_builder_derive/src/generator/builder_generator.rs b/const_typed_builder_derive/src/generator/builder_generator.rs index a1df40b..86e9d92 100644 --- a/const_typed_builder_derive/src/generator/builder_generator.rs +++ b/const_typed_builder_derive/src/generator/builder_generator.rs @@ -128,7 +128,7 @@ impl<'a> BuilderGenerator<'a> { let type_generics = self.generics_gen.const_generics_valued(false); let (impl_generics, _, where_clause) = self.generics_gen.target_generics().split_for_impl(); let documentation = format!( - "Creates a new [`{}`] without any fields set", + "Creates a new [`{}`] without any fields initialized", self.builder_name );