-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stackable-versioned): Add support for versioned enums (#813)
* Update changelog * Update PR link in the changelog * Start to move code, add enum impls * Introduce generalized structs for containers and items * Finish traits and generic types * Use From<&ContainerAttributes> for Vec<ContainerVersion> implementation * Finish basic enum code generation * Use darling(flatten) for field attrs * Replace unwraps with expects * Generate code for all item states * Start adding From ipls for enum conversion * Finish basic From impls for enums * Apply suggestions Co-authored-by: Nick <[email protected]> * Apply more suggestions Co-authored-by: Nick <[email protected]> * Rename starts_with variable to starts_with_deprecated * Remove old todo comment * Add auto-generated notes for deprecated versions * Move attribute parsing into new() functions --------- Co-authored-by: Nick <[email protected]>
- Loading branch information
1 parent
3c825bf
commit cca721a
Showing
22 changed files
with
1,162 additions
and
272 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
crates/stackable-versioned-macros/src/attrs/common/item.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use darling::{util::SpannedValue, Error, FromMeta}; | ||
use k8s_version::Version; | ||
use proc_macro2::Span; | ||
use syn::Path; | ||
|
||
/// These attributes are meant to be used in super structs, which add | ||
/// [`Field`](syn::Field) or [`Variant`](syn::Variant) specific attributes via | ||
/// darling's flatten feature. This struct only provides shared attributes. | ||
#[derive(Debug, FromMeta)] | ||
#[darling(and_then = ItemAttributes::validate)] | ||
pub(crate) struct ItemAttributes { | ||
/// This parses the `added` attribute on items (fields or variants). It can | ||
/// only be present at most once. | ||
pub(crate) added: Option<AddedAttributes>, | ||
|
||
/// This parses the `renamed` attribute on items (fields or variants). It | ||
/// can be present 0..n times. | ||
#[darling(multiple, rename = "renamed")] | ||
pub(crate) renames: Vec<RenamedAttributes>, | ||
|
||
/// This parses the `deprecated` attribute on items (fields or variants). It | ||
/// can only be present at most once. | ||
pub(crate) deprecated: Option<DeprecatedAttributes>, | ||
} | ||
|
||
impl ItemAttributes { | ||
fn validate(self) -> Result<Self, Error> { | ||
// Validate deprecated options | ||
|
||
// TODO (@Techassi): Make the field 'note' optional, because in the | ||
// future, the macro will generate parts of the deprecation note | ||
// automatically. The user-provided note will then be appended to the | ||
// auto-generated one. | ||
|
||
if let Some(deprecated) = &self.deprecated { | ||
if deprecated.note.is_empty() { | ||
return Err(Error::custom("deprecation note must not be empty") | ||
.with_span(&deprecated.note.span())); | ||
} | ||
} | ||
|
||
Ok(self) | ||
} | ||
} | ||
|
||
/// For the added() action | ||
/// | ||
/// Example usage: | ||
/// - `added(since = "...")` | ||
/// - `added(since = "...", default_fn = "custom_fn")` | ||
#[derive(Clone, Debug, FromMeta)] | ||
pub(crate) struct AddedAttributes { | ||
pub(crate) since: SpannedValue<Version>, | ||
|
||
#[darling(rename = "default", default = "default_default_fn")] | ||
pub(crate) default_fn: SpannedValue<Path>, | ||
} | ||
|
||
fn default_default_fn() -> SpannedValue<Path> { | ||
SpannedValue::new( | ||
syn::parse_str("std::default::Default::default").expect("internal error: path must parse"), | ||
Span::call_site(), | ||
) | ||
} | ||
|
||
/// For the renamed() action | ||
/// | ||
/// Example usage: | ||
/// - `renamed(since = "...", from = "...")` | ||
#[derive(Clone, Debug, FromMeta)] | ||
pub(crate) struct RenamedAttributes { | ||
pub(crate) since: SpannedValue<Version>, | ||
pub(crate) from: SpannedValue<String>, | ||
} | ||
|
||
/// For the deprecated() action | ||
/// | ||
/// Example usage: | ||
/// - `deprecated(since = "...", note = "...")` | ||
#[derive(Clone, Debug, FromMeta)] | ||
pub(crate) struct DeprecatedAttributes { | ||
pub(crate) since: SpannedValue<Version>, | ||
pub(crate) note: SpannedValue<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod container; | ||
mod item; | ||
|
||
pub(crate) use container::*; | ||
pub(crate) use item::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub(crate) mod container; | ||
pub(crate) mod common; | ||
pub(crate) mod field; | ||
pub(crate) mod variant; |
Oops, something went wrong.