Skip to content

Commit 5bcd7f0

Browse files
committed
refactor(enum): use value for discriminants to be less verbose
Refs: #178
1 parent 596ba24 commit 5bcd7f0

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

crates/macros/src/enum_.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct PhpEnumAttribute {
3030
struct PhpEnumVariantAttribute {
3131
#[darling(flatten)]
3232
rename: PhpRename,
33+
#[darling(rename = "value")]
3334
discriminant: Option<Lit>,
3435
attrs: Vec<syn::Attribute>,
3536
}
@@ -47,7 +48,7 @@ pub fn parser(mut input: ItemEnum) -> Result<TokenStream> {
4748
bail!("Enum cases must be unit variants, found: {:?}", variant);
4849
}
4950
if !php_attr.allow_native_discriminants.is_present() && variant.discriminant.is_some() {
50-
bail!(variant => "Native discriminants are currently not exported to PHP. To set a discriminant, use the `#[php(allow_native_discriminants)]` attribute on the enum. To export discriminants, set the #[php(discriminant = ...)] attribute on the enum case.");
51+
bail!(variant => "Native discriminants are currently not exported to PHP. To set a discriminant, use the `#[php(allow_native_discriminants)]` attribute on the enum. To export discriminants, set the #[php(value = ...)] attribute on the enum case.");
5152
}
5253

5354
let variant_attr = PhpEnumVariantAttribute::from_attributes(&variant.attrs)?;

crates/macros/src/lib.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,9 @@ fn php_class_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
237237
/// - `#[php(name = "CaseName")]` or `#[php(change_case = snake_case)]`: Sets
238238
/// the name of the enum case in PHP. The default is the `PascalCase` name of
239239
/// the case.
240-
/// - `#[php(discriminant = "value")]` or `#[php(discriminant = 123)]`: Sets the
241-
/// discriminant value for the enum case. This can be a string or an integer.
242-
/// If not set, the case will be exported as a simple enum case without a
243-
/// discriminant.
240+
/// - `#[php(value = "value")]` or `#[php(value = 123)]`: Sets the discriminant
241+
/// value for the enum case. This can be a string or an integer. If not set,
242+
/// the case will be exported as a simple enum case without a discriminant.
244243
///
245244
/// ### Example
246245
///
@@ -268,11 +267,11 @@ fn php_class_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
268267
///
269268
/// ## Backed Enums
270269
/// Enums can also be backed by either `i64` or `&'static str`. Those values can
271-
/// be set using the `#[php(discriminant = "value")]` or `#[php(discriminant =
272-
/// 123)]` attributes on the enum variants.
270+
/// be set using the `#[php(value = "value")]` or `#[php(value = 123)]`
271+
/// attributes on the enum variants.
273272
///
274-
/// All variants must have a discriminant of the same type, either all `i64` or
275-
/// all `&'static str`.
273+
/// All variants must have a value of the same type, either all `i64` or all
274+
/// `&'static str`.
276275
///
277276
/// ```rust,no_run,ignore
278277
/// # #![cfg_attr(windows, feature(abi_vectorcall))]
@@ -281,13 +280,13 @@ fn php_class_internal(args: TokenStream2, input: TokenStream2) -> TokenStream2 {
281280
///
282281
/// #[php_enum]
283282
/// pub enum Suit {
284-
/// #[php(discriminant = "hearts")]
283+
/// #[php(value = "hearts")]
285284
/// Hearts,
286-
/// #[php(discriminant = "diamonds")]
285+
/// #[php(value = "diamonds")]
287286
/// Diamonds,
288-
/// #[php(discriminant = "clubs")]
287+
/// #[php(value = "clubs")]
289288
/// Clubs,
290-
/// #[php(discriminant = "spades")]
289+
/// #[php(value = "spades")]
291290
/// Spades,
292291
/// }
293292
/// #[php_module]

guide/src/macros/enum.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The `#[php_enum]` attribute can be configured with the following options:
1515
The cases of the enum can be configured with the following options:
1616
- `#[php(name = "CaseName")]` or `#[php(change_case = snake_case)]`: Sets the name of the enum case in PHP.
1717
The default is the `PascalCase` name of the case.
18-
- `#[php(discriminant = "value")]` or `#[php(discriminant = 123)]`: Sets the discriminant value for the enum case.
18+
- `#[php(value = "value")]` or `#[php(value = 123)]`: Sets the discriminant value for the enum case.
1919
This can be a string or an integer. If not set, the case will be exported as a simple enum case without a discriminant.
2020

2121
### Example
@@ -44,9 +44,9 @@ pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
4444

4545
## Backed Enums
4646
Enums can also be backed by either `i64` or `&'static str`. Those values can be set using the
47-
`#[php(discriminant = "value")]` or `#[php(discriminant = 123)]` attributes on the enum variants.
47+
`#[php(value = "value")]` or `#[php(value = 123)]` attributes on the enum variants.
4848

49-
All variants must have a discriminant of the same type, either all `i64` or all `&'static str`.
49+
All variants must have a value of the same type, either all `i64` or all `&'static str`.
5050

5151
```rust,no_run
5252
# #![cfg_attr(windows, feature(abi_vectorcall))]
@@ -55,13 +55,13 @@ use ext_php_rs::prelude::*;
5555
5656
#[php_enum]
5757
pub enum Suit {
58-
#[php(discriminant = "hearts")]
58+
#[php(value = "hearts")]
5959
Hearts,
60-
#[php(discriminant = "diamonds")]
60+
#[php(value = "diamonds")]
6161
Diamonds,
62-
#[php(discriminant = "clubs")]
62+
#[php(value = "clubs")]
6363
Clubs,
64-
#[php(discriminant = "spades")]
64+
#[php(value = "spades")]
6565
Spades,
6666
}
6767
#[php_module]

tests/src/integration/enum_/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ pub enum TestEnum {
1515

1616
#[php_enum]
1717
pub enum IntBackedEnum {
18-
#[php(discriminant = 1)]
18+
#[php(value = 1)]
1919
Variant1,
20-
#[php(discriminant = 2)]
20+
#[php(value = 2)]
2121
Variant2,
2222
}
2323

2424
#[php_enum]
2525
pub enum StringBackedEnum {
26-
#[php(discriminant = "foo")]
26+
#[php(value = "foo")]
2727
Variant1,
28-
#[php(discriminant = "bar")]
28+
#[php(value = "bar")]
2929
Variant2,
3030
}
3131

0 commit comments

Comments
 (0)