Skip to content

Commit ece7410

Browse files
authored
feat(stackable-versioned): Forward container visibility (#850)
* feat(stackable-versioned): Forward container visibility * docs: Add doc comments * chore: Update changelog
1 parent e689cee commit ece7410

File tree

5 files changed

+78
-28
lines changed

5 files changed

+78
-28
lines changed

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

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ops::Deref;
22

33
use proc_macro2::TokenStream;
4-
use syn::{Attribute, Ident};
4+
use syn::{Attribute, Ident, Visibility};
55

66
use crate::{attrs::common::ContainerAttributes, codegen::common::ContainerVersion};
77

@@ -21,12 +21,7 @@ where
2121
Self: Sized + Deref<Target = VersionedContainer<I>>,
2222
{
2323
/// Creates a new versioned container.
24-
fn new(
25-
ident: Ident,
26-
data: D,
27-
attributes: ContainerAttributes,
28-
original_attributes: Vec<Attribute>,
29-
) -> syn::Result<Self>;
24+
fn new(input: ContainerInput, data: D, attributes: ContainerAttributes) -> syn::Result<Self>;
3025

3126
/// This generates the complete code for a single versioned container.
3227
///
@@ -37,6 +32,21 @@ where
3732
fn generate_tokens(&self) -> TokenStream;
3833
}
3934

35+
/// This struct bundles values from [`DeriveInput`][1].
36+
///
37+
/// [`DeriveInput`][1] cannot be used directly when constructing a
38+
/// [`VersionedStruct`][2] or [`VersionedEnum`][3] because we run into borrow
39+
/// issues caused by the match statement which extracts the data.
40+
///
41+
/// [1]: syn::DeriveInput
42+
/// [2]: crate::codegen::vstruct::VersionedStruct
43+
/// [3]: crate::codegen::venum::VersionedEnum
44+
pub(crate) struct ContainerInput {
45+
pub(crate) original_attributes: Vec<Attribute>,
46+
pub(crate) visibility: Visibility,
47+
pub(crate) ident: Ident,
48+
}
49+
4050
/// Stores individual versions of a single container.
4151
///
4252
/// Each version tracks item actions, which describe if the item was added,
@@ -55,13 +65,17 @@ pub(crate) struct VersionedContainer<I> {
5565
/// The ident, or name, of the versioned container.
5666
pub(crate) ident: Ident,
5767

68+
/// The visibility of the versioned container. Used to forward the
69+
/// visibility during code generation.
70+
pub(crate) visibility: Visibility,
71+
72+
/// The original attributes that were added to the container.
73+
pub(crate) original_attributes: Vec<Attribute>,
74+
5875
/// The name of the container used in `From` implementations.
5976
pub(crate) from_ident: Ident,
6077

6178
/// Whether the [`From`] implementation generation should be skipped for all
6279
/// versions of this container.
6380
pub(crate) skip_from: bool,
64-
65-
/// The original attributes that were added to the container.
66-
pub(crate) original_attributes: Vec<Attribute>,
6781
}

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

+19-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ use syn::{spanned::Spanned, Data, DeriveInput, Error, Result};
33

44
use crate::{
55
attrs::common::ContainerAttributes,
6-
codegen::{common::Container, venum::VersionedEnum, vstruct::VersionedStruct},
6+
codegen::{
7+
common::{Container, ContainerInput},
8+
venum::VersionedEnum,
9+
vstruct::VersionedStruct,
10+
},
711
};
812

913
pub(crate) mod chain;
@@ -26,10 +30,22 @@ pub(crate) mod vstruct;
2630
pub(crate) fn expand(attributes: ContainerAttributes, input: DeriveInput) -> Result<TokenStream> {
2731
let expanded = match input.data {
2832
Data::Struct(data) => {
29-
VersionedStruct::new(input.ident, data, attributes, input.attrs)?.generate_tokens()
33+
let input = ContainerInput {
34+
original_attributes: input.attrs,
35+
visibility: input.vis,
36+
ident: input.ident,
37+
};
38+
39+
VersionedStruct::new(input, data, attributes)?.generate_tokens()
3040
}
3141
Data::Enum(data) => {
32-
VersionedEnum::new(input.ident, data, attributes, input.attrs)?.generate_tokens()
42+
let input = ContainerInput {
43+
original_attributes: input.attrs,
44+
visibility: input.vis,
45+
ident: input.ident,
46+
};
47+
48+
VersionedEnum::new(input, data, attributes)?.generate_tokens()
3349
}
3450
_ => {
3551
return Err(Error::new(

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

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ use std::ops::Deref;
33
use itertools::Itertools;
44
use proc_macro2::TokenStream;
55
use quote::quote;
6-
use syn::{Attribute, DataEnum, Error, Ident};
6+
use syn::{DataEnum, Error};
77

88
use crate::{
99
attrs::common::ContainerAttributes,
1010
codegen::{
1111
common::{
12-
format_container_from_ident, Container, ContainerVersion, Item, VersionedContainer,
12+
format_container_from_ident, Container, ContainerInput, ContainerVersion, Item,
13+
VersionedContainer,
1314
},
1415
venum::variant::VersionedVariant,
1516
},
@@ -34,11 +35,16 @@ impl Deref for VersionedEnum {
3435

3536
impl Container<DataEnum, VersionedVariant> for VersionedEnum {
3637
fn new(
37-
ident: Ident,
38+
input: ContainerInput,
3839
data: DataEnum,
3940
attributes: ContainerAttributes,
40-
original_attributes: Vec<Attribute>,
4141
) -> syn::Result<Self> {
42+
let ContainerInput {
43+
original_attributes,
44+
visibility,
45+
ident,
46+
} = input;
47+
4248
// Convert the raw version attributes into a container version.
4349
let versions: Vec<_> = (&attributes).into();
4450

@@ -78,11 +84,12 @@ impl Container<DataEnum, VersionedVariant> for VersionedEnum {
7884
.options
7985
.skip
8086
.map_or(false, |s| s.from.is_present()),
87+
original_attributes,
88+
visibility,
8189
from_ident,
8290
versions,
8391
items,
8492
ident,
85-
original_attributes,
8693
}))
8794
}
8895

@@ -105,8 +112,10 @@ impl VersionedEnum {
105112
next_version: Option<&ContainerVersion>,
106113
) -> TokenStream {
107114
let mut token_stream = TokenStream::new();
108-
let enum_name = &self.ident;
115+
109116
let original_attributes = &self.original_attributes;
117+
let visibility = &self.visibility;
118+
let enum_name = &self.ident;
110119

111120
// Generate variants of the enum for `version`.
112121
let variants = self.generate_enum_variants(version);
@@ -140,7 +149,7 @@ impl VersionedEnum {
140149
token_stream.extend(quote! {
141150
#[automatically_derived]
142151
#deprecated_attr
143-
pub mod #version_ident {
152+
#visibility mod #version_ident {
144153
#(#original_attributes)*
145154
#version_specific_docs
146155
pub enum #enum_name {

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

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ use std::ops::Deref;
33
use itertools::Itertools;
44
use proc_macro2::TokenStream;
55
use quote::quote;
6-
use syn::{Attribute, DataStruct, Error, Ident};
6+
use syn::{DataStruct, Error, Ident};
77

88
use crate::{
99
attrs::common::ContainerAttributes,
1010
codegen::{
1111
common::{
12-
format_container_from_ident, Container, ContainerVersion, Item, VersionedContainer,
12+
format_container_from_ident, Container, ContainerInput, ContainerVersion, Item,
13+
VersionedContainer,
1314
},
1415
vstruct::field::VersionedField,
1516
},
@@ -34,11 +35,16 @@ impl Deref for VersionedStruct {
3435

3536
impl Container<DataStruct, VersionedField> for VersionedStruct {
3637
fn new(
37-
ident: Ident,
38+
input: ContainerInput,
3839
data: DataStruct,
3940
attributes: ContainerAttributes,
40-
original_attributes: Vec<Attribute>,
4141
) -> syn::Result<Self> {
42+
let ContainerInput {
43+
original_attributes,
44+
visibility,
45+
ident,
46+
} = input;
47+
4248
// Convert the raw version attributes into a container version.
4349
let versions: Vec<_> = (&attributes).into();
4450

@@ -78,11 +84,12 @@ impl Container<DataStruct, VersionedField> for VersionedStruct {
7884
.options
7985
.skip
8086
.map_or(false, |s| s.from.is_present()),
87+
original_attributes,
88+
visibility,
8189
from_ident,
8290
versions,
8391
items,
8492
ident,
85-
original_attributes,
8693
}))
8794
}
8895

@@ -105,8 +112,10 @@ impl VersionedStruct {
105112
next_version: Option<&ContainerVersion>,
106113
) -> TokenStream {
107114
let mut token_stream = TokenStream::new();
108-
let struct_name = &self.ident;
115+
109116
let original_attributes = &self.original_attributes;
117+
let visibility = &self.visibility;
118+
let struct_name = &self.ident;
110119

111120
// Generate fields of the struct for `version`.
112121
let fields = self.generate_struct_fields(version);
@@ -140,7 +149,7 @@ impl VersionedStruct {
140149
token_stream.extend(quote! {
141150
#[automatically_derived]
142151
#deprecated_attr
143-
pub mod #version_ident {
152+
#visibility mod #version_ident {
144153
#(#original_attributes)*
145154
#version_specific_docs
146155
pub struct #struct_name {

crates/stackable-versioned/CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ All notable changes to this project will be documented in this file.
77
### Added
88

99
- Pass through container and item attributes (including doc-comments). Add
10-
attribute for version specific docs. ([#847])
10+
attribute for version specific docs ([#847]).
11+
- Forward container visibility to generated modules ([#850]).
1112

1213
### Fixed
1314

@@ -16,6 +17,7 @@ All notable changes to this project will be documented in this file.
1617

1718
[#842]: https://github.com/stackabletech/operator-rs/pull/842
1819
[#847]: https://github.com/stackabletech/operator-rs/pull/847
20+
[#850]: https://github.com/stackabletech/operator-rs/pull/850
1921

2022
## [0.1.1] - 2024-07-10
2123

0 commit comments

Comments
 (0)