@@ -220,18 +220,28 @@ pub fn php_class(args: TokenStream, input: TokenStream) -> TokenStream {
220220///
221221/// Enums can be exported to PHP as enums with the `#[php_enum]` attribute
222222/// macro. This attribute derives the `RegisteredClass` and `PhpEnum` traits on
223- /// your enum. To register the enum use the `r#enum ::<EnumName>()` method on the
224- /// `ModuleBuilder` in the `#[php_module]` macro.
223+ /// your enum. To register the enum use the `enumeration ::<EnumName>()` method
224+ /// on the `ModuleBuilder` in the `#[php_module]` macro.
225225///
226226/// ## Options
227227///
228- /// tbd
228+ /// The `#[php_enum]` attribute can be configured with the following options:
229+ /// - `#[php(name = "EnumName")]` or `#[php(change_case = snake_case)]`: Sets
230+ /// the name of the enum in PHP. The default is the `PascalCase` name of the
231+ /// enum.
232+ /// - `#[php(allow_native_discriminants)]`: Allows the use of native Rust
233+ /// discriminants (e.g., `Hearts = 1`).
229234///
230- /// ## Restrictions
231- ///
232- /// tbd
235+ /// The cases of the enum can be configured with the following options:
236+ /// - `#[php(name = "CaseName")]` or `#[php(change_case = snake_case)]`: Sets
237+ /// the name of the enum case in PHP. The default is the `PascalCase` name of
238+ /// the case.
239+ /// - `#[php(discriminant = "value")]` or `#[php(discriminant = 123)]`: Sets the
240+ /// discriminant value for the enum case. This can be a string or an integer.
241+ /// If not set, the case will be exported as a simple enum case without a
242+ /// discriminant.
233243///
234- /// ## Example
244+ /// ### Example
235245///
236246/// This example creates a PHP enum `Suit`.
237247///
@@ -250,11 +260,72 @@ pub fn php_class(args: TokenStream, input: TokenStream) -> TokenStream {
250260///
251261/// #[php_module]
252262/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
253- /// module.r#enum ::<Suit>()
263+ /// module.enumeration ::<Suit>()
254264/// }
255265/// # fn main() {}
256266/// ```
257267///
268+ /// ## Backed Enums
269+ /// Enums can also be backed by either `i64` or `&'static str`. Those values can
270+ /// be set using the `#[php(discriminant = "value")]` or `#[php(discriminant =
271+ /// 123)]` attributes on the enum variants.
272+ ///
273+ /// All variants must have a discriminant of the same type, either all `i64` or
274+ /// all `&'static str`.
275+ ///
276+ /// ```rust,no_run,ignore
277+ /// # #![cfg_attr(windows, feature(abi_vectorcall))]
278+ /// # extern crate ext_php_rs;
279+ /// use ext_php_rs::prelude::*;
280+ ///
281+ /// #[php_enum]
282+ /// pub enum Suit {
283+ /// #[php(discriminant = "hearts")]
284+ /// Hearts,
285+ /// #[php(discriminant = "diamonds")]
286+ /// Diamonds,
287+ /// #[php(discriminant = "clubs")]
288+ /// Clubs,
289+ /// #[php(discriminant = "spades")]
290+ /// Spades,
291+ /// }
292+ /// #[php_module]
293+ /// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
294+ /// module.enumeration::<Suit>()
295+ /// }
296+ /// # fn main() {}
297+ /// ```
298+ ///
299+ /// ### 'Native' Discriminators
300+ /// Native rust discriminants are currently not supported and will not be
301+ /// exported to PHP.
302+ ///
303+ /// To avoid confusion a compiler error will be raised if you try to use a
304+ /// native discriminant. You can ignore this error by adding the
305+ /// `#[php(allow_native_discriminants)]` attribute to your enum.
306+ ///
307+ /// ```rust,no_run,ignore
308+ /// # #![cfg_attr(windows, feature(abi_vectorcall))]
309+ /// # extern crate ext_php_rs;
310+ /// use ext_php_rs::prelude::*;
311+ ///
312+ /// #[php_enum]
313+ /// #[php(allow_native_discriminants)]
314+ /// pub enum Suit {
315+ /// Hearts = 1,
316+ /// Diamonds = 2,
317+ /// Clubs = 3,
318+ /// Spades = 4,
319+ /// }
320+ ///
321+ /// #[php_module]
322+ /// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
323+ /// module.enumeration::<Suit>()
324+ /// }
325+ /// # fn main() {}
326+ /// ```
327+ ///
328+ ///
258329/// TODO: Add backed enums example
259330// END DOCS FROM enum.md
260331#[ proc_macro_attribute]
0 commit comments