File tree Expand file tree Collapse file tree 1 file changed +35
-1
lines changed Expand file tree Collapse file tree 1 file changed +35
-1
lines changed Original file line number Diff line number Diff line change 32
32
//! }
33
33
//!
34
34
//! assert_eq!(Message::HelloGreeting { name: "Alice".to_string() }.to_string(), "hello-greeting");
35
+ //! ```
35
36
//!
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
+ //! ```
37
71
pub use enum_display_macro:: * ;
38
72
39
73
#[ cfg( test) ]
You can’t perform that action at this time.
0 commit comments