enum_display/
lib.rs

1//!
2//! enum-display is a crate for implementing [`std::fmt::Display`] on enum variants with macros.
3//!
4//! # Simple Example
5//!
6//! ```rust
7//! use enum_display::EnumDisplay;
8//!
9//! #[derive(EnumDisplay)]
10//! enum Color {
11//!    Red,
12//!    Green,
13//!    Blue,
14//! }
15//!
16//! assert_eq!(Color::Red.to_string(), "Red");
17//! assert_eq!(Color::Green.to_string(), "Green");
18//! assert_eq!(Color::Blue.to_string(), "Blue");
19//! ```
20//!
21//! # Example With Custom Case Transform
22//!
23//! Any case from [convert_case](https://docs.rs/convert_case/latest/convert_case/) is supported.
24//!
25//! ```rust
26//! use enum_display::EnumDisplay;
27//!
28//! #[derive(EnumDisplay)]
29//! #[enum_display(case = "Kebab")]
30//! enum Message {
31//!     HelloGreeting { name: String },
32//! }
33//!
34//! assert_eq!(Message::HelloGreeting { name: "Alice".to_string() }.to_string(), "hello-greeting");
35//!
36
37pub use enum_display_macro::*;
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[allow(dead_code)]
44    #[derive(EnumDisplay)]
45    enum TestEnum {
46        Name,
47        Address {
48            street: String,
49            city: String,
50            state: String,
51            zip: String,
52        },
53        DateOfBirth(u32, u32, u32),
54    }
55
56    #[allow(dead_code)]
57    #[derive(EnumDisplay)]
58    #[enum_display(case = "Kebab")]
59    enum TestEnumWithAttribute {
60        Name,
61        Address {
62            street: String,
63            city: String,
64            state: String,
65            zip: String,
66        },
67        DateOfBirth(u32, u32, u32),
68    }
69
70    #[test]
71    fn test_unit_field_variant() {
72        assert_eq!(TestEnum::Name.to_string(), "Name");
73    }
74
75    #[test]
76    fn test_named_fields_variant() {
77        assert_eq!(
78            TestEnum::Address {
79                street: "123 Main St".to_string(),
80                city: "Any Town".to_string(),
81                state: "CA".to_string(),
82                zip: "12345".to_string()
83            }
84            .to_string(),
85            "Address"
86        );
87    }
88
89    #[test]
90    fn test_unnamed_fields_variant() {
91        assert_eq!(TestEnum::DateOfBirth(1, 1, 2000).to_string(), "DateOfBirth");
92    }
93
94    #[test]
95    fn test_unit_field_variant_case_transform() {
96        assert_eq!(TestEnumWithAttribute::Name.to_string(), "name");
97    }
98
99    #[test]
100    fn test_named_fields_variant_case_transform() {
101        assert_eq!(
102            TestEnumWithAttribute::Address {
103                street: "123 Main St".to_string(),
104                city: "Any Town".to_string(),
105                state: "CA".to_string(),
106                zip: "12345".to_string()
107            }
108            .to_string(),
109            "address"
110        );
111    }
112
113    #[test]
114    fn test_unnamed_fields_variant_case_transform() {
115        assert_eq!(
116            TestEnumWithAttribute::DateOfBirth(1, 1, 2000).to_string(),
117            "date-of-birth"
118        );
119    }
120}