File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3232//! }
3333//!
3434//! assert_eq!(Message::HelloGreeting { name: "Alice".to_string() }.to_string(), "hello-greeting");
35+ //! ```
3536//!
36-
37+ //! # Example With Custom Formatting
38+ //!
39+ //! Display output can be customised with a format string passed to the `display` enum
40+ //! variant attribute.
41+ //!
42+ //! The case converted variant name is available via the `variant` named parameter.
43+ //!
44+ //! Additional parameters available to the format string depend on the type of enum variant:
45+ //!
46+ //! | Variant Type | Format String Field Access | Example |
47+ //! |------------------|-----------------------------------------------------------------------------------|-------------------------------------|
48+ //! | Named | [Named Parameters](https://doc.rust-lang.org/std/fmt/#named-parameters) | `"{variant} name field is: {name}"` |
49+ //! | Unnamed (tuple) | [Positional Parameters](https://doc.rust-lang.org/std/fmt/#positional-parameters) | `"{variant} age field is: {0}"` |
50+ //! | Unit | No additional fields available | `"{variant} has no fields"` |
51+ //!
52+ //! ```rust
53+ //! use enum_display::EnumDisplay;
54+ //!
55+ //! #[derive(EnumDisplay)]
56+ //! enum Message {
57+ //! #[display("{variant} {name}!")]
58+ //! Hello { name: String },
59+ //!
60+ //! #[display("{variant}? {0}")]
61+ //! HowOld(usize),
62+ //!
63+ //! #[display("{variant}!")]
64+ //! Wow,
65+ //! }
66+ //!
67+ //! assert_eq!(Message::Hello { name: "Alice".to_string() }.to_string(), "Hello Alice!");
68+ //! assert_eq!(Message::HowOld(123).to_string(), "HowOld? 123");
69+ //! assert_eq!(Message::Wow.to_string(), "Wow!");
70+ //! ```
3771pub use enum_display_macro:: * ;
3872
3973#[ cfg( test) ]
You can’t perform that action at this time.
0 commit comments