-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
245 additions
and
58 deletions.
There are no files selected for viewing
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,31 @@ | ||
use darling::{util::SpannedValue, FromMeta}; | ||
use k8s_version::Version; | ||
use proc_macro2::Span; | ||
use syn::Path; | ||
|
||
#[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(), | ||
) | ||
} | ||
|
||
#[derive(Clone, Debug, FromMeta)] | ||
pub(crate) struct RenamedAttributes { | ||
pub(crate) since: SpannedValue<Version>, | ||
pub(crate) from: SpannedValue<String>, | ||
} | ||
|
||
#[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
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,4 @@ | ||
pub(crate) mod common; | ||
pub(crate) mod container; | ||
pub(crate) mod field; | ||
pub(crate) mod variant; |
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,40 @@ | ||
// TODO (@Techassi): Think about what can be moved into a common impl for both | ||
// fields and variants. | ||
|
||
use darling::{Error, FromVariant}; | ||
use syn::{Ident, Variant}; | ||
|
||
use crate::attrs::{ | ||
common::{AddedAttributes, DeprecatedAttributes, RenamedAttributes}, | ||
container::ContainerAttributes, | ||
}; | ||
|
||
#[derive(Debug, FromVariant)] | ||
#[darling( | ||
attributes(versioned), | ||
forward_attrs(allow, doc, cfg, serde), | ||
and_then = VariantAttributes::validate | ||
)] | ||
pub(crate) struct VariantAttributes { | ||
pub(crate) ident: Ident, | ||
pub(crate) added: Option<AddedAttributes>, | ||
|
||
#[darling(multiple, rename = "renamed")] | ||
pub(crate) renames: Vec<RenamedAttributes>, | ||
|
||
pub(crate) deprecated: Option<DeprecatedAttributes>, | ||
} | ||
|
||
impl VariantAttributes { | ||
pub(crate) fn validate(self) -> Result<Self, Error> { | ||
Ok(self) | ||
} | ||
|
||
pub(crate) fn validate_versions( | ||
&self, | ||
attributes: &ContainerAttributes, | ||
variant: &Variant, | ||
) -> Result<(), Error> { | ||
todo!() | ||
} | ||
} |
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,51 @@ | ||
use k8s_version::Version; | ||
use proc_macro2::{Span, TokenStream}; | ||
use syn::Ident; | ||
|
||
use crate::attrs::container::ContainerAttributes; | ||
|
||
#[derive(Debug, Clone)] | ||
pub(crate) struct ContainerVersion { | ||
pub(crate) deprecated: bool, | ||
pub(crate) skip_from: bool, | ||
pub(crate) inner: Version, | ||
pub(crate) ident: Ident, | ||
} | ||
|
||
impl From<&ContainerAttributes> for Vec<ContainerVersion> { | ||
fn from(attributes: &ContainerAttributes) -> Self { | ||
attributes | ||
.versions | ||
.iter() | ||
.map(|v| ContainerVersion { | ||
skip_from: v.skip.as_ref().map_or(false, |s| s.from.is_present()), | ||
ident: Ident::new(&v.name.to_string(), Span::call_site()), | ||
deprecated: v.deprecated.is_present(), | ||
inner: v.name, | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
pub(crate) trait VersionedContainer { | ||
type Container; | ||
type Data; | ||
|
||
fn new(ident: Ident, data: Self::Data, attributes: ContainerAttributes) -> Self::Container; | ||
fn generate_tokens(&self) -> TokenStream; | ||
} | ||
|
||
pub(crate) struct Container<T: Sized> { | ||
/// The ident, or name, of the versioned enum. | ||
pub(crate) ident: Ident, | ||
|
||
/// List of declared versions for this enum. Each version, except the | ||
/// latest, generates a definition with appropriate fields. | ||
pub(crate) versions: Vec<ContainerVersion>, | ||
|
||
pub(crate) items: Vec<T>, | ||
|
||
/// The name of the enum used in `From` implementations. | ||
pub(crate) from_ident: Ident, | ||
pub(crate) skip_from: bool, | ||
} |
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::borrow::Borrow; | ||
|
||
use darling::FromVariant; | ||
use itertools::Itertools; | ||
use proc_macro2::TokenStream; | ||
use syn::{DataEnum, Error, Ident, Result}; | ||
|
||
use crate::{ | ||
attrs::{container::ContainerAttributes, variant::VariantAttributes}, | ||
gen::{common::ContainerVersion, venum::variant::VersionedVariant}, | ||
}; | ||
|
||
mod variant; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct VersionedEnum { | ||
/// The ident, or name, of the versioned enum. | ||
pub(crate) ident: Ident, | ||
|
||
/// List of declared versions for this enum. Each version, except the | ||
/// latest, generates a definition with appropriate fields. | ||
pub(crate) versions: Vec<ContainerVersion>, | ||
|
||
/// List of variants defined in the base enum. How, and if, a variant should | ||
/// generate code, is decided by the currently generated version. | ||
pub(crate) variants: Vec<VersionedVariant>, | ||
|
||
/// The name of the enum used in `From` implementations. | ||
pub(crate) from_ident: Ident, | ||
pub(crate) skip_from: bool, | ||
} | ||
|
||
impl VersionedEnum { | ||
pub(crate) fn new( | ||
ident: Ident, | ||
data: DataEnum, | ||
attributes: ContainerAttributes, | ||
) -> Result<Self> { | ||
let versions: Vec<ContainerVersion> = attributes.borrow().into(); | ||
let mut variants = Vec::new(); | ||
|
||
for variant in data.variants { | ||
let attrs = VariantAttributes::from_variant(&variant)?; | ||
attrs.validate_versions(&attributes, &variant)?; | ||
|
||
let mut versioned_variant = VersionedVariant::new(); | ||
versioned_variant.insert_container_versions(&versions); | ||
variants.push(versioned_variant); | ||
} | ||
|
||
for version in &versions { | ||
if !variants.iter().map(|v| v.get_ident(version)).all_unique() { | ||
return Err(Error::new( | ||
ident.span(), | ||
format!("enum contains renamed variant which collide with other variant in version {version}", version = version.inner), | ||
)); | ||
} | ||
} | ||
|
||
todo!() | ||
} | ||
|
||
pub(crate) fn generate_tokens(&self) -> TokenStream { | ||
todo!() | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
crates/stackable-versioned-macros/src/gen/venum/variant.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,27 @@ | ||
use syn::Ident; | ||
|
||
use crate::gen::common::ContainerVersion; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct VersionedVariant {} | ||
|
||
impl VersionedVariant { | ||
pub(crate) fn new() -> Self { | ||
todo!() | ||
} | ||
|
||
pub(crate) fn insert_container_versions(&mut self, versions: &[ContainerVersion]) { | ||
todo!() | ||
} | ||
|
||
pub(crate) fn get_ident(&self, version: &ContainerVersion) -> Option<&Ident> { | ||
// match &self.chain { | ||
// Some(chain) => chain | ||
// .get(&version.inner) | ||
// .expect("internal error: chain must contain container version") | ||
// .get_ident(), | ||
// None => self.inner.ident.as_ref(), | ||
// } | ||
todo!() | ||
} | ||
} |
This file was deleted.
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