Skip to content

Commit 63ec18c

Browse files
committed
chore: Add doc comments
1 parent ad3f460 commit 63ec18c

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

crates/stackable-versioned-macros/src/codegen/container/enum.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ops::Not;
22

3-
use darling::{FromAttributes, Result};
3+
use darling::{util::IdentString, FromAttributes, Result};
44
use proc_macro2::TokenStream;
55
use quote::quote;
66
use syn::ItemEnum;
@@ -88,14 +88,19 @@ impl Container {
8888
}
8989
}
9090

91+
/// A versioned enum.
9192
pub(crate) struct Enum {
9293
/// List of variants defined in the original enum. How, and if, an item
9394
/// should generate code, is decided by the currently generated version.
9495
pub(crate) variants: Vec<VersionedVariant>,
96+
97+
/// Common container data which is shared between enums and structs.
9598
pub(crate) common: CommonContainerData,
9699
}
97100

101+
// Common token generation
98102
impl Enum {
103+
/// Generates code for the enum definition.
99104
pub(crate) fn generate_definition(&self, version: &VersionDefinition) -> TokenStream {
100105
let original_attributes = &self.common.original_attributes;
101106
let ident = &self.common.idents.original;
@@ -115,6 +120,7 @@ impl Enum {
115120
}
116121
}
117122

123+
/// Generates code for the `From<Version> for NextVersion` implementation.
118124
pub(crate) fn generate_from_impl(
119125
&self,
120126
version: &VersionDefinition,
@@ -133,14 +139,7 @@ impl Enum {
133139
let next_version_ident = &next_version.ident;
134140
let version_ident = &version.ident;
135141

136-
let mut variants = TokenStream::new();
137-
for variant in &self.variants {
138-
variants.extend(variant.generate_for_from_impl(
139-
version,
140-
next_version,
141-
enum_ident,
142-
));
143-
}
142+
let variants = self.generate_from_variants(version, next_version, enum_ident);
144143

145144
// Include allow(deprecated) only when this or the next version is
146145
// deprecated. Also include it, when a variant in this or the next
@@ -172,13 +171,29 @@ impl Enum {
172171
}
173172
}
174173

174+
/// Generates code for enum variants used in `From` implementations.
175+
fn generate_from_variants(
176+
&self,
177+
version: &VersionDefinition,
178+
next_version: &VersionDefinition,
179+
enum_ident: &IdentString,
180+
) -> TokenStream {
181+
let mut tokens = TokenStream::new();
182+
183+
for variant in &self.variants {
184+
tokens.extend(variant.generate_for_from_impl(version, next_version, enum_ident));
185+
}
186+
187+
tokens
188+
}
189+
175190
/// Returns whether any variant is deprecated in the provided `version`.
176191
fn is_any_variant_deprecated(&self, version: &VersionDefinition) -> bool {
177-
// First, iterate over all variants. The `any` function will return true if any of the
178-
// function invocations return true. If a field doesn't have a chain,
179-
// we can safely default to false (unversioned fields cannot be
180-
// deprecated). Then we retrieve the status of the field and ensure it
181-
// is deprecated.
192+
// First, iterate over all variants. The `any` function will return true
193+
// if any of the function invocations return true. If a variant doesn't
194+
// have a chain, we can safely default to false (unversioned variants
195+
// cannot be deprecated). Then we retrieve the status of the variant and
196+
// ensure it is deprecated.
182197
self.variants.iter().any(|f| {
183198
f.changes.as_ref().map_or(false, |c| {
184199
c.value_is(&version.inner, |a| {

crates/stackable-versioned-macros/src/codegen/container/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
mod r#enum;
1616
mod r#struct;
1717

18+
/// Contains common container data shared between structs and enums.
1819
pub(crate) struct CommonContainerData {
1920
/// Original attributes placed on the container, like `#[derive()]` or `#[cfg()]`.
2021
pub(crate) original_attributes: Vec<Attribute>,
@@ -26,19 +27,26 @@ pub(crate) struct CommonContainerData {
2627
pub(crate) idents: ContainerIdents,
2728
}
2829

30+
/// Supported types of containers, structs and enums.
31+
///
32+
/// Abstracting away with kind of container is generated makes it possible to create a list of
33+
/// containers when the macro is used on modules. This enum provides functions to generate code
34+
/// which then internally call the appropriate function based on the variant.
2935
pub(crate) enum Container {
3036
Struct(Struct),
3137
Enum(Enum),
3238
}
3339

3440
impl Container {
41+
/// Generates the container definition for the specified `version`.
3542
pub(crate) fn generate_definition(&self, version: &VersionDefinition) -> TokenStream {
3643
match self {
3744
Container::Struct(s) => s.generate_definition(version),
3845
Container::Enum(e) => e.generate_definition(version),
3946
}
4047
}
4148

49+
/// Generates the container `From<Version> for NextVersion` implementation.
4250
pub(crate) fn generate_from_impl(
4351
&self,
4452
version: &VersionDefinition,
@@ -51,6 +59,16 @@ impl Container {
5159
}
5260
}
5361

62+
/// Generates Kubernetes specific code snippets.
63+
///
64+
/// This function returns three values:
65+
///
66+
/// - an enum variant ident,
67+
/// - an enum variant display string,
68+
/// - and a `CustomResource::crd()` call
69+
///
70+
/// This function only returns `Some` if it is a struct. Enums cannot be used to define
71+
/// Kubernetes custom resources.
5472
pub(crate) fn generate_kubernetes_item(
5573
&self,
5674
version: &VersionDefinition,
@@ -61,6 +79,10 @@ impl Container {
6179
}
6280
}
6381

82+
/// Generates Kubernetes specific code to merge two or more CRDs into one.
83+
///
84+
/// This function only returns `Some` if it is a struct. Enums cannot be used to define
85+
/// Kubernetes custom resources.
6486
pub(crate) fn generate_kubernetes_merge_crds(
6587
&self,
6688
enum_variant_idents: Vec<IdentString>,
@@ -80,13 +102,20 @@ impl Container {
80102
}
81103
}
82104

105+
/// A versioned standalone container.
106+
///
107+
/// A standalone container is a container defined outside of a versioned module. See [`Module`][1]
108+
/// for more information about versioned modules.
109+
///
110+
/// [1]: crate::codegen::module::Module
83111
pub(crate) struct StandaloneContainer {
84112
versions: Vec<VersionDefinition>,
85113
container: Container,
86114
vis: Visibility,
87115
}
88116

89117
impl StandaloneContainer {
118+
/// Creates a new versioned standalone struct.
90119
pub(crate) fn new_struct(
91120
item_struct: ItemStruct,
92121
attributes: StandaloneContainerAttributes,
@@ -103,6 +132,7 @@ impl StandaloneContainer {
103132
})
104133
}
105134

135+
/// Creates a new versioned standalone enum.
106136
pub(crate) fn new_enum(
107137
item_enum: ItemEnum,
108138
attributes: StandaloneContainerAttributes,
@@ -119,6 +149,7 @@ impl StandaloneContainer {
119149
})
120150
}
121151

152+
/// Generate tokens containing every piece of code required for a standalone container.
122153
pub(crate) fn generate_tokens(&self) -> TokenStream {
123154
let vis = &self.vis;
124155

crates/stackable-versioned-macros/src/codegen/container/struct.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,19 @@ impl Container {
119119
}
120120
}
121121

122+
/// A versioned struct.
122123
pub(crate) struct Struct {
123124
/// List of fields defined in the original struct. How, and if, an item
124125
/// should generate code, is decided by the currently generated version.
125126
pub(crate) fields: Vec<VersionedField>,
127+
128+
/// Common container data which is shared between structs and enums.
126129
pub(crate) common: CommonContainerData,
127130
}
128131

129132
// Common token generation
130133
impl Struct {
134+
/// Generates code for the struct definition.
131135
pub(crate) fn generate_definition(&self, version: &VersionDefinition) -> TokenStream {
132136
let original_attributes = &self.common.original_attributes;
133137
let ident = &self.common.idents.original;
@@ -151,6 +155,7 @@ impl Struct {
151155
}
152156
}
153157

158+
/// Generates code for the `From<Version> for NextVersion` implementation.
154159
pub(crate) fn generate_from_impl(
155160
&self,
156161
version: &VersionDefinition,
@@ -202,6 +207,7 @@ impl Struct {
202207
}
203208
}
204209

210+
/// Generates code for struct fields used in `From` implementations.
205211
fn generate_from_fields(
206212
&self,
207213
version: &VersionDefinition,
@@ -217,7 +223,13 @@ impl Struct {
217223
tokens
218224
}
219225

226+
/// Returns whether any field is deprecated in the provided `version`.
220227
fn is_any_field_deprecated(&self, version: &VersionDefinition) -> bool {
228+
// First, iterate over all fields. The `any` function will return true
229+
// if any of the function invocations return true. If a field doesn't
230+
// have a chain, we can safely default to false (unversioned fields
231+
// cannot be deprecated). Then we retrieve the status of the field and
232+
// ensure it is deprecated.
221233
self.fields.iter().any(|f| {
222234
f.changes.as_ref().map_or(false, |c| {
223235
c.value_is(&version.inner, |a| {

crates/stackable-versioned-macros/src/codegen/module.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ pub(crate) struct ModuleInput {
1212
pub(crate) ident: Ident,
1313
}
1414

15+
/// A versioned module.
16+
///
17+
/// Versioned modules allow versioning multiple containers at once without introducing conflicting
18+
/// version module definitions.
1519
pub(crate) struct Module {
1620
versions: Vec<VersionDefinition>,
1721
containers: Vec<Container>,
@@ -21,6 +25,7 @@ pub(crate) struct Module {
2125
}
2226

2327
impl Module {
28+
/// Creates a new versioned module containing versioned containers.
2429
pub(crate) fn new(
2530
ModuleInput { ident, vis, .. }: ModuleInput,
2631
preserve_module: bool,
@@ -36,6 +41,7 @@ impl Module {
3641
}
3742
}
3843

44+
/// Generates tokens for all versioned containers.
3945
pub(crate) fn generate_tokens(&self) -> TokenStream {
4046
if self.containers.is_empty() {
4147
return quote! {};

0 commit comments

Comments
 (0)