Skip to content

Commit

Permalink
fix(stackable-versioned): Validation error reporting (#842)
Browse files Browse the repository at this point in the history
* fix: Report variant name validation error at the correct span

* fix: Remove leading underscore for variant not using PascalCase

* chore: Update changelog

* chore: Update PR link in changelog

* style: Update format_ident call

* chore: Apply suggestion

Co-authored-by: Nick <[email protected]>

* chore: Apply suggestions

This snuck in because rust-analyzer doesn't remove trailing commas the same way it does for the format! macro.

Co-authored-by: Nick <[email protected]>

---------

Co-authored-by: Nick <[email protected]>
  • Loading branch information
Techassi and NickLarsenNZ authored Aug 21, 2024
1 parent cee9bd5 commit 79252b2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
3 changes: 2 additions & 1 deletion crates/stackable-versioned-macros/src/attrs/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ impl VariantAttributes {
.iter()
.all(|r| r.from.is_case(Case::Pascal))
{
errors.push(Error::custom("renamed variants must use PascalCase"));
errors
.push(Error::custom("renamed variants must use PascalCase").with_span(&self.ident));
}

errors.finish()?;
Expand Down
20 changes: 13 additions & 7 deletions crates/stackable-versioned-macros/src/codegen/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,24 @@ pub(crate) fn format_container_from_ident(ident: &Ident) -> Ident {
///
/// See [`DEPRECATED_FIELD_PREFIX`].
pub(crate) fn remove_deprecated_field_prefix(ident: &Ident) -> Ident {
remove_ident_prefix(ident, DEPRECATED_FIELD_PREFIX)
let ident = ident.to_string();
let ident = ident.trim_start_matches(DEPRECATED_FIELD_PREFIX);

format_ident!("{ident}")
}

/// Removes the deprecated prefix from a variant ident.
///
/// See [`DEPRECATED_VARIANT_PREFIX`].
pub(crate) fn remove_deprecated_variant_prefix(ident: &Ident) -> Ident {
remove_ident_prefix(ident, DEPRECATED_VARIANT_PREFIX)
}
// NOTE (@Techassi): Currently Clippy only issues a warning for variants
// with underscores in their name. That's why we additionally remove the
// leading underscore from the ident to use the expected name during code
// generation.
let ident = ident.to_string();
let ident = ident
.trim_start_matches(DEPRECATED_VARIANT_PREFIX)
.trim_start_matches('_');

/// Removes the provided prefix from an ident and returns the newly created
/// ident.
pub(crate) fn remove_ident_prefix(ident: &Ident, prefix: &str) -> Ident {
format_ident!("{}", ident.to_string().trim_start_matches(prefix))
format_ident!("{ident}")
}
7 changes: 7 additions & 0 deletions crates/stackable-versioned/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Fixed

- Report variant rename validation error at the correct span and trim underscores
from variants not using PascalCase (#[842]).

[#842]: https://github.com/stackabletech/operator-rs/pull/842

## [0.1.1] - 2024-07-10

### Added
Expand Down

0 comments on commit 79252b2

Please sign in to comment.