Skip to content

Commit d43ccc1

Browse files
refactor(stackable-versioned): Introduce idents for fields/variants (#1088)
* refactor(stackable-versioned): Introduce idents for fields/variants * chore(stackable-versioned): Add doc comments * chore(stackable-versioned): Use named format arguments Co-authored-by: Nick <[email protected]> --------- Co-authored-by: Nick <[email protected]>
1 parent 6a5a2ac commit d43ccc1

File tree

6 files changed

+172
-143
lines changed

6 files changed

+172
-143
lines changed

crates/stackable-versioned-macros/src/attrs/item/field.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use darling::{Error, FromField, Result, util::Flag};
22
use syn::{Attribute, Ident};
33

4-
use crate::{attrs::item::CommonItemAttributes, codegen::VersionDefinition, utils::FieldIdent};
4+
use crate::{
5+
attrs::item::CommonItemAttributes,
6+
codegen::{VersionDefinition, item::FieldIdents},
7+
};
58

69
/// This struct describes all available field attributes, as well as the field
710
/// name to display better diagnostics.
@@ -57,7 +60,9 @@ impl FieldAttributes {
5760
.expect("internal error: field must have an ident")
5861
.clone();
5962

60-
self.common.validate(FieldIdent::from(ident), &self.attrs)?;
63+
self.common
64+
.validate(FieldIdents::from(ident), &self.attrs)?;
65+
6166
Ok(self)
6267
}
6368

crates/stackable-versioned-macros/src/attrs/item/mod.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use syn::{Attribute, Path, Type, spanned::Spanned};
88

99
use crate::{
1010
codegen::{VersionDefinition, item::ItemStatus},
11-
utils::ItemIdentExt,
11+
utils::ItemIdents,
1212
};
1313

1414
mod field;
@@ -50,14 +50,14 @@ pub struct CommonItemAttributes {
5050
// it contains functions which can only be called after the initial parsing and validation because
5151
// they need additional context, namely the list of versions defined on the container or module.
5252
impl CommonItemAttributes {
53-
pub fn validate(&self, item_ident: impl ItemIdentExt, item_attrs: &[Attribute]) -> Result<()> {
53+
pub fn validate(&self, item_idents: impl ItemIdents, item_attrs: &[Attribute]) -> Result<()> {
5454
let mut errors = Error::accumulator();
5555

56-
errors.handle(self.validate_action_combinations(&item_ident));
57-
errors.handle(self.validate_action_order(&item_ident));
58-
errors.handle(self.validate_item_name(&item_ident));
56+
errors.handle(self.validate_action_combinations(&item_idents));
57+
errors.handle(self.validate_action_order(&item_idents));
58+
errors.handle(self.validate_item_name(&item_idents));
5959
errors.handle(self.validate_added_action());
60-
errors.handle(self.validate_changed_action(&item_ident));
60+
errors.handle(self.validate_changed_action(&item_idents));
6161
errors.handle(self.validate_item_attributes(item_attrs));
6262

6363
errors.finish()
@@ -106,23 +106,23 @@ impl CommonItemAttributes {
106106
/// at least one version before being changed.
107107
/// - `changed` and `deprecated` using the same version: Again, the same
108108
/// rules from above apply here as well.
109-
fn validate_action_combinations(&self, item_ident: &impl ItemIdentExt) -> Result<()> {
109+
fn validate_action_combinations(&self, item_idents: &impl ItemIdents) -> Result<()> {
110110
match (&self.added, &self.changes, &self.deprecated) {
111111
(Some(added), _, Some(deprecated)) if *added.since == *deprecated.since => Err(
112112
Error::custom("cannot be marked as `added` and `deprecated` in the same version")
113-
.with_span(item_ident),
113+
.with_span(item_idents.original()),
114114
),
115115
(Some(added), changed, _) if changed.iter().any(|r| *r.since == *added.since) => Err(
116116
Error::custom("cannot be marked as `added` and `changed` in the same version")
117-
.with_span(item_ident),
117+
.with_span(item_idents.original()),
118118
),
119119
(_, changed, Some(deprecated))
120120
if changed.iter().any(|r| *r.since == *deprecated.since) =>
121121
{
122122
Err(Error::custom(
123123
"cannot be marked as `deprecated` and `changed` in the same version",
124124
)
125-
.with_span(item_ident))
125+
.with_span(item_idents.original()))
126126
}
127127
_ => Ok(()),
128128
}
@@ -140,7 +140,7 @@ impl CommonItemAttributes {
140140
/// version of the added action.
141141
/// - All `changed` actions must use a greater version than `added` but a
142142
/// lesser version than `deprecated`.
143-
fn validate_action_order(&self, item_ident: &impl ItemIdentExt) -> Result<()> {
143+
fn validate_action_order(&self, item_idents: &impl ItemIdents) -> Result<()> {
144144
let added_version = self.added.as_ref().map(|a| *a.since);
145145
let deprecated_version = self.deprecated.as_ref().map(|d| *d.since);
146146

@@ -152,7 +152,7 @@ impl CommonItemAttributes {
152152
if added_version > deprecated_version {
153153
return Err(Error::custom(format!(
154154
"cannot marked as `added` in version `{added_version}` while being marked as `deprecated` in an earlier version `{deprecated_version}`"
155-
)).with_span(item_ident));
155+
)).with_span(item_idents.original()));
156156
}
157157
}
158158

@@ -165,7 +165,7 @@ impl CommonItemAttributes {
165165
return Err(Error::custom(
166166
"all changes must use versions higher than `added` and lower than `deprecated`",
167167
)
168-
.with_span(item_ident));
168+
.with_span(item_idents.original()));
169169
}
170170

171171
Ok(())
@@ -180,21 +180,22 @@ impl CommonItemAttributes {
180180
/// - Fields or variants marked as deprecated need to include the
181181
/// deprecation prefix in their name. The prefix must not be included for
182182
/// fields or variants which are not deprecated.
183-
fn validate_item_name(&self, item_ident: &impl ItemIdentExt) -> Result<()> {
184-
let starts_with_deprecated = item_ident.starts_with_deprecated_prefix();
183+
fn validate_item_name(&self, item_idents: &impl ItemIdents) -> Result<()> {
184+
let starts_with_deprecated = item_idents.starts_with_deprecation_prefix();
185185

186186
if self.deprecated.is_some() && !starts_with_deprecated {
187187
return Err(Error::custom(format!(
188-
"marked as `deprecated` and thus must include the `{deprecated_prefix}` prefix",
189-
deprecated_prefix = item_ident.deprecated_prefix()
188+
"marked as `deprecated` and thus must include the `{deprecation_prefix}` prefix",
189+
deprecation_prefix = item_idents.deprecation_prefix()
190190
))
191-
.with_span(item_ident));
191+
.with_span(item_idents.original()));
192192
}
193193

194194
if self.deprecated.is_none() && starts_with_deprecated {
195-
return Err(Error::custom(
196-
format!("not marked as `deprecated` and thus must not include the `{deprecated_prefix}` prefix", deprecated_prefix = item_ident.deprecated_prefix())
197-
).with_span(item_ident));
195+
return Err(Error::custom(format!(
196+
"not marked as `deprecated` and thus must not include the `{deprecation_prefix}` prefix",
197+
deprecation_prefix = item_idents.deprecation_prefix()
198+
)).with_span(item_idents.original()));
198199
}
199200

200201
Ok(())
@@ -218,7 +219,7 @@ impl CommonItemAttributes {
218219
/// This associated function is called by the top-level validation function
219220
/// and validates that parameters provided to the `changed` actions are
220221
/// valid.
221-
fn validate_changed_action(&self, item_ident: &impl ItemIdentExt) -> Result<()> {
222+
fn validate_changed_action(&self, item_ident: &impl ItemIdents) -> Result<()> {
222223
let mut errors = Error::accumulator();
223224

224225
for change in &self.changes {
@@ -230,7 +231,7 @@ impl CommonItemAttributes {
230231

231232
// This ensures that `from_name` doesn't include the deprecation prefix.
232233
if let Some(from_name) = change.from_name.as_ref() {
233-
if from_name.starts_with(item_ident.deprecated_prefix()) {
234+
if from_name.starts_with(item_ident.deprecation_prefix()) {
234235
errors.push(
235236
Error::custom(
236237
"the previous name must not start with the deprecation prefix",
@@ -291,17 +292,17 @@ impl CommonItemAttributes {
291292
impl CommonItemAttributes {
292293
pub fn into_changeset(
293294
self,
294-
ident: &impl ItemIdentExt,
295+
idents: &impl ItemIdents,
295296
ty: Type,
296297
) -> Option<BTreeMap<Version, ItemStatus>> {
297298
// TODO (@Techassi): Use Change instead of ItemStatus
298299
if let Some(deprecated) = self.deprecated {
299-
let deprecated_ident = ident.deref();
300+
let deprecated_ident = idents.original();
300301

301302
// When the item is deprecated, any change which occurred beforehand
302303
// requires access to the item ident to infer the item ident for
303304
// the latest change.
304-
let mut ident = ident.as_cleaned_ident();
305+
let mut ident = idents.cleaned().clone();
305306
let mut ty = ty;
306307

307308
let mut actions = BTreeMap::new();
@@ -361,7 +362,7 @@ impl CommonItemAttributes {
361362

362363
Some(actions)
363364
} else if !self.changes.is_empty() {
364-
let mut ident = ident.deref().clone();
365+
let mut ident = idents.original().clone();
365366
let mut ty = ty;
366367

367368
let mut actions = BTreeMap::new();
@@ -419,7 +420,7 @@ impl CommonItemAttributes {
419420
*added.since,
420421
ItemStatus::Addition {
421422
default_fn: added.default_fn.deref().clone(),
422-
ident: ident.deref().clone(),
423+
ident: idents.original().clone(),
423424
ty: Box::new(ty),
424425
},
425426
);

crates/stackable-versioned-macros/src/attrs/item/variant.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use convert_case::{Case, Casing};
22
use darling::{Error, FromVariant, Result};
33
use syn::{Attribute, Ident};
44

5-
use crate::{attrs::item::CommonItemAttributes, codegen::VersionDefinition, utils::VariantIdent};
5+
use crate::{
6+
attrs::item::CommonItemAttributes,
7+
codegen::{VersionDefinition, item::VariantIdents},
8+
};
69

710
/// This struct describes all available variant attributes, as well as the
811
/// variant name to display better diagnostics.
@@ -51,7 +54,7 @@ impl VariantAttributes {
5154

5255
errors.handle(
5356
self.common
54-
.validate(VariantIdent::from(self.ident.clone()), &self.attrs),
57+
.validate(VariantIdents::from(self.ident.clone()), &self.attrs),
5558
);
5659

5760
// Validate names of renames

0 commit comments

Comments
 (0)