Skip to content

Commit b3f2cf0

Browse files
committed
refactor: Rename renamed action to changed
1 parent 26fb9a9 commit b3f2cf0

File tree

11 files changed

+113
-95
lines changed

11 files changed

+113
-95
lines changed

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

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use darling::{util::SpannedValue, Error, FromMeta};
22
use k8s_version::Version;
33
use proc_macro2::Span;
4-
use syn::{spanned::Spanned, Attribute, Ident, Path};
4+
use syn::{spanned::Spanned, Attribute, Ident, Path, Type};
55

66
use crate::{
77
attrs::common::ContainerAttributes,
@@ -55,14 +55,14 @@ where
5555
}
5656
}
5757

58-
for rename in &*self.common_attributes().renames {
58+
for change in &*self.common_attributes().changes {
5959
if !container_attrs
6060
.versions
6161
.iter()
62-
.any(|v| v.name == *rename.since)
62+
.any(|v| v.name == *change.since)
6363
{
6464
errors.push(
65-
Error::custom("variant action `renamed` uses version which was not declared via #[versioned(version)]")
65+
Error::custom("variant action `changed` uses version which was not declared via #[versioned(version)]")
6666
.with_span(item)
6767
);
6868
}
@@ -104,8 +104,8 @@ pub(crate) enum ItemType {
104104
/// ### Shared Item Rules
105105
///
106106
/// - An item can only ever be added once at most. An item not marked as 'added'
107-
/// is part of the container in every version until renamed or deprecated.
108-
/// - An item can be renamed many times. That's why renames are stored in a
107+
/// is part of the container in every version until changed or deprecated.
108+
/// - An item can be changed many times. That's why changes are stored in a
109109
/// [`Vec`].
110110
/// - An item can only be deprecated once. A field or variant not marked as
111111
/// 'deprecated' will be included up until the latest version.
@@ -115,10 +115,10 @@ pub(crate) struct ItemAttributes {
115115
/// only be present at most once.
116116
pub(crate) added: Option<AddedAttributes>,
117117

118-
/// This parses the `renamed` attribute on items (fields or variants). It
118+
/// This parses the `changed` attribute on items (fields or variants). It
119119
/// can be present 0..n times.
120-
#[darling(multiple, rename = "renamed")]
121-
pub(crate) renames: Vec<RenamedAttributes>,
120+
#[darling(multiple, rename = "changed")]
121+
pub(crate) changes: Vec<ChangedAttributes>,
122122

123123
/// This parses the `deprecated` attribute on items (fields or variants). It
124124
/// can only be present at most once.
@@ -175,34 +175,34 @@ impl ItemAttributes {
175175
/// cannot be marked as added in a particular version and then marked as
176176
/// deprecated immediately after. Fields and variants must be included for
177177
/// at least one version before being marked deprecated.
178-
/// - `added` and `renamed` using the same version: The same reasoning from
178+
/// - `added` and `changed` using the same version: The same reasoning from
179179
/// above applies here as well. Fields and variants must be included for
180-
/// at least one version before being renamed.
181-
/// - `renamed` and `deprecated` using the same version: Again, the same
180+
/// at least one version before being changed.
181+
/// - `changed` and `deprecated` using the same version: Again, the same
182182
/// rules from above apply here as well.
183183
fn validate_action_combinations(
184184
&self,
185185
item_ident: &Ident,
186186
item_type: &ItemType,
187187
) -> Result<(), Error> {
188-
match (&self.added, &self.renames, &self.deprecated) {
188+
match (&self.added, &self.changes, &self.deprecated) {
189189
(Some(added), _, Some(deprecated)) if *added.since == *deprecated.since => {
190190
Err(Error::custom(format!(
191191
"{item_type} cannot be marked as `added` and `deprecated` in the same version"
192192
))
193193
.with_span(item_ident))
194194
}
195-
(Some(added), renamed, _) if renamed.iter().any(|r| *r.since == *added.since) => {
195+
(Some(added), changed, _) if changed.iter().any(|r| *r.since == *added.since) => {
196196
Err(Error::custom(format!(
197-
"{item_type} cannot be marked as `added` and `renamed` in the same version"
197+
"{item_type} cannot be marked as `added` and `changed` in the same version"
198198
))
199199
.with_span(item_ident))
200200
}
201-
(_, renamed, Some(deprecated))
202-
if renamed.iter().any(|r| *r.since == *deprecated.since) =>
201+
(_, changed, Some(deprecated))
202+
if changed.iter().any(|r| *r.since == *deprecated.since) =>
203203
{
204204
Err(Error::custom(
205-
format!("{item_type} cannot be marked as `deprecated` and `renamed` in the same version"),
205+
format!("{item_type} cannot be marked as `deprecated` and `changed` in the same version"),
206206
)
207207
.with_span(item_ident))
208208
}
@@ -220,7 +220,7 @@ impl ItemAttributes {
220220
/// ensures that these versions are chronologically sound, that means,
221221
/// that the version of the deprecated action must be greater than the
222222
/// version of the added action.
223-
/// - All `renamed` actions must use a greater version than `added` but a
223+
/// - All `changed` actions must use a greater version than `added` but a
224224
/// lesser version than `deprecated`.
225225
fn validate_action_order(&self, item_ident: &Ident, item_type: &ItemType) -> Result<(), Error> {
226226
let added_version = self.added.as_ref().map(|a| *a.since);
@@ -238,14 +238,14 @@ impl ItemAttributes {
238238
}
239239
}
240240

241-
// Now, iterate over all renames and ensure that their versions are
241+
// Now, iterate over all changes and ensure that their versions are
242242
// between the added and deprecated version.
243-
if !self.renames.iter().all(|r| {
243+
if !self.changes.iter().all(|r| {
244244
added_version.map_or(true, |a| a < *r.since)
245245
&& deprecated_version.map_or(true, |d| d > *r.since)
246246
}) {
247247
return Err(Error::custom(
248-
"all renames must use versions higher than `added` and lower than `deprecated`",
248+
"all changes must use versions higher than `added` and lower than `deprecated`",
249249
)
250250
.with_span(item_ident));
251251
}
@@ -320,19 +320,24 @@ pub(crate) struct AddedAttributes {
320320

321321
fn default_default_fn() -> SpannedValue<Path> {
322322
SpannedValue::new(
323-
syn::parse_str("std::default::Default::default").expect("internal error: path must parse"),
323+
syn::parse_str("::std::default::Default::default")
324+
.expect("internal error: path must parse"),
324325
Span::call_site(),
325326
)
326327
}
327328

328-
/// For the renamed() action
329+
/// For the changed() action
329330
///
330331
/// Example usage:
331-
/// - `renamed(since = "...", from = "...")`
332+
/// - `changed(since = "...", from_name = "...")`
333+
/// - `changed(since = "...", from_name = "..." from_type="...")`
332334
#[derive(Clone, Debug, FromMeta)]
333-
pub(crate) struct RenamedAttributes {
335+
pub(crate) struct ChangedAttributes {
334336
pub(crate) since: SpannedValue<Version>,
335-
pub(crate) from: SpannedValue<String>,
337+
pub(crate) from_name: Option<SpannedValue<String>>,
338+
339+
#[allow(dead_code)]
340+
pub(crate) from_type: Option<SpannedValue<Type>>,
336341
}
337342

338343
/// For the deprecated() action

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ impl FieldAttributes {
5050
.ident
5151
.as_ref()
5252
.expect("internal error: field must have an ident");
53-
self.common.validate(ident, &ItemType::Field, &self.attrs)?;
5453

54+
self.common.validate(ident, &ItemType::Field, &self.attrs)?;
5555
Ok(self)
5656
}
5757
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ impl VariantAttributes {
5555
);
5656

5757
// Validate names of renames
58-
for rename in &self.common.renames {
59-
if !rename.from.is_case(Case::Pascal) {
60-
errors.push(
61-
Error::custom("renamed variant must use PascalCase")
62-
.with_span(&rename.from.span()),
63-
)
58+
for change in &self.common.changes {
59+
if let Some(from_name) = &change.from_name {
60+
if !from_name.is_case(Case::Pascal) {
61+
errors.push(
62+
Error::custom("renamed variant must use PascalCase")
63+
.with_span(&from_name.span()),
64+
)
65+
}
6466
}
6567
}
6668

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ mod test {
9191
#[case(2, (Some(&"test1"), Some(&"test3")))]
9292
#[case(3, (Some(&"test1"), None))]
9393
#[case(4, (Some(&"test3"), None))]
94-
fn test(#[case] key: i32, #[case] expected: (Option<&&str>, Option<&&str>)) {
94+
fn neighbors(#[case] key: i32, #[case] expected: (Option<&&str>, Option<&&str>)) {
9595
let map = BTreeMap::from([(1, "test1"), (3, "test3")]);
9696
let neigbors = map.get_neighbors(&key);
9797

0 commit comments

Comments
 (0)