Skip to content

Commit 709e6f6

Browse files
committed
docs: Update field and variant attribute docs
1 parent 5f4a500 commit 709e6f6

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ use crate::{
99
consts::{DEPRECATED_FIELD_PREFIX, DEPRECATED_VARIANT_PREFIX},
1010
};
1111

12+
/// This trait helps to unify attribute validation for both field and variant
13+
/// attributes.
14+
///
15+
/// This trait is implemented using a blanket implementation on types
16+
/// `T: Attributes`. The [`Attributes`] trait allows access to the common
17+
/// attributes shared across field and variant attributes.
1218
pub(crate) trait ValidateVersions<I>
1319
where
1420
I: Spanned,
@@ -79,6 +85,10 @@ where
7985
}
8086
}
8187

88+
// NOTE (@Techassi): It might be possible (but is it required) to move this
89+
// functionality into a shared trait, which knows what type of item 'Self' is.
90+
91+
/// This enum is used to run different validation based on the type of item.
8292
#[derive(Debug, strum::Display)]
8393
#[strum(serialize_all = "lowercase")]
8494
pub(crate) enum ItemType {
@@ -89,6 +99,15 @@ pub(crate) enum ItemType {
8999
/// These attributes are meant to be used in super structs, which add
90100
/// [`Field`](syn::Field) or [`Variant`](syn::Variant) specific attributes via
91101
/// darling's flatten feature. This struct only provides shared attributes.
102+
///
103+
/// ### Shared Item Rules
104+
///
105+
/// - An item can only ever be added once at most. An item not marked as 'added'
106+
/// is part of the container in every version until renamed or deprecated.
107+
/// - An item can be renamed many times. That's why renames are stored in a
108+
/// [`Vec`].
109+
/// - An item can only be deprecated once. A field not marked as 'deprecated'
110+
/// will be included up until the latest version.
92111
#[derive(Debug, FromMeta)]
93112
pub(crate) struct ItemAttributes {
94113
/// This parses the `added` attribute on items (fields or variants). It can

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

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ use crate::attrs::common::{ItemAttributes, ItemType};
99
/// Data stored in this struct is validated using darling's `and_then` attribute.
1010
/// During darlings validation, it is not possible to validate that action
1111
/// versions match up with declared versions on the container. This validation
12-
/// can be done using the associated [`FieldAttributes::validate_versions`]
12+
/// can be done using the associated [`ValidateVersions::validate_versions`][1]
1313
/// function.
1414
///
15-
/// ### Field Rules
15+
/// Rules shared across fields and variants can be found [here][2].
1616
///
17-
/// - A field can only ever be added once at most. A field not marked as 'added'
18-
/// is part of the struct in every version until renamed or deprecated.
19-
/// - A field can be renamed many times. That's why renames are stored in a
20-
/// [`Vec`].
21-
/// - A field can only be deprecated once. A field not marked as 'deprecated'
22-
/// will be included up until the latest version.
17+
/// [1]: crate::attrs::common::ValidateVersions::validate_versions
18+
/// [2]: crate::attrs::common::ItemAttributes
2319
#[derive(Debug, FromField)]
2420
#[darling(
2521
attributes(versioned),
@@ -37,23 +33,18 @@ pub(crate) struct FieldAttributes {
3733
}
3834

3935
impl FieldAttributes {
40-
// NOTE (@Techassi): Ideally, these validations should be moved to the
41-
// ItemAttributes impl, because common validation like action combinations
42-
// and action order can be validated without taking the type of attribute
43-
// into account (field vs variant). However, we would loose access to the
44-
// field / variant ident and as such, cannot display the error directly on
45-
// the affected field / variant. This is a significant decrease in DX.
46-
// See https://github.com/TedDriggs/darling/discussions/294
47-
4836
/// This associated function is called by darling (see and_then attribute)
4937
/// after it successfully parsed the attribute. This allows custom
5038
/// validation of the attribute which extends the validation already in
5139
/// place by darling.
5240
///
5341
/// Internally, it calls out to other specialized validation functions.
5442
fn validate(self) -> Result<Self, Error> {
55-
self.common
56-
.validate(self.ident.as_ref().unwrap(), &ItemType::Field)?;
43+
let ident = self
44+
.ident
45+
.as_ref()
46+
.expect("internal error: field must have an ident");
47+
self.common.validate(ident, &ItemType::Field)?;
5748

5849
Ok(self)
5950
}

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ use syn::Ident;
44

55
use crate::attrs::common::{ItemAttributes, ItemType};
66

7+
/// This struct describes all available variant attributes, as well as the
8+
/// variant name to display better diagnostics.
9+
///
10+
/// Data stored in this struct is validated using darling's `and_then` attribute.
11+
/// During darlings validation, it is not possible to validate that action
12+
/// versions match up with declared versions on the container. This validation
13+
/// can be done using the associated [`FieldAttributes::validate_versions`][1]
14+
/// function.
15+
///
16+
/// Rules shared across fields and variants can be found [here][2].
17+
///
18+
/// [1]: crate::attrs::common::ValidateVersions::validate_versions
19+
/// [2]: crate::attrs::common::ItemAttributes
720
#[derive(Debug, FromVariant)]
821
#[darling(
922
attributes(versioned),
@@ -21,14 +34,6 @@ pub(crate) struct VariantAttributes {
2134
}
2235

2336
impl VariantAttributes {
24-
// NOTE (@Techassi): Ideally, these validations should be moved to the
25-
// ItemAttributes impl, because common validation like action combinations
26-
// and action order can be validated without taking the type of attribute
27-
// into account (field vs variant). However, we would loose access to the
28-
// field / variant ident and as such, cannot display the error directly on
29-
// the affected field / variant. This is a significant decrease in DX.
30-
// See https://github.com/TedDriggs/darling/discussions/294
31-
3237
/// This associated function is called by darling (see and_then attribute)
3338
/// after it successfully parsed the attribute. This allows custom
3439
/// validation of the attribute which extends the validation already in

0 commit comments

Comments
 (0)