Skip to content

Commit b19e69d

Browse files
author
alexmadeathing
committed
🔨 Update docs to round off formatting feature.
1 parent a9671d3 commit b19e69d

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/lib.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,42 @@
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+
//! ```
3771
pub use enum_display_macro::*;
3872

3973
#[cfg(test)]

0 commit comments

Comments
 (0)