1
0
mirror of https://github.com/danog/strum.git synced 2024-11-26 20:14:40 +01:00

Update documentation to include listing all the supported case styles.

This commit is contained in:
Peter Glotfelty 2021-10-08 16:58:11 -07:00
parent 6119f107e2
commit 201d794fd6

View File

@ -1,41 +1,57 @@
//! # Documentation for Additional Attributes //! # Documentation for Additional Attributes
//!
//! ## Attributes on Enums
//! //!
//! Strum supports several custom attributes to modify the generated code. At the enum level, the //! Strum supports several custom attributes to modify the generated code. At the enum level, the following attributes are supported:
//! `#[strum(serialize_all = "snake_case")]` attribute can be used to change the case used when //!
//! serializing to and deserializing from strings: //! - `#[strum(serialize_all = "case_style")]` attribute can be used to change the case used when serializing to and deserializing
//! from strings. This feature is enabled by [withoutboats/heck](https://github.com/withoutboats/heck) and supported case styles are:
//!
//! - `camelCase`
//! - `PascalCase`
//! - `kebab-case`
//! - `snake_case`
//! - `SCREAMING_SNAKE_CASE`
//! - `SCREAMING-KEBAB-CASE`
//! - `lowercase`
//! - `UPPERCASE`
//! - `title_case`
//! - `mixed_case`
//!
//! ```rust
//! use std::string::ToString;
//! use strum;
//! use strum_macros;
//!
//! #[derive(Debug, Eq, PartialEq, strum_macros::ToString)]
//! #[strum(serialize_all = "snake_case")]
//! enum Brightness {
//! DarkBlack,
//! Dim {
//! glow: usize,
//! },
//! #[strum(serialize = "bright")]
//! BrightWhite,
//! }
//!
//! assert_eq!(
//! String::from("dark_black"),
//! Brightness::DarkBlack.to_string().as_ref()
//! );
//! assert_eq!(
//! String::from("dim"),
//! Brightness::Dim { glow: 0 }.to_string().as_ref()
//! );
//! assert_eq!(
//! String::from("bright"),
//! Brightness::BrightWhite.to_string().as_ref()
//! );
//! ```
//! //!
//! ```rust //! - You can also apply the `#[strum(ascii_case_insensitive)]` attribute to the enum,
//! use std::string::ToString; //! and this has the same effect of applying it to every variant.
//! use strum; //!
//! use strum_macros; //! ## Attributes on Variants
//!
//! #[derive(Debug, Eq, PartialEq, strum_macros::ToString)]
//! #[strum(serialize_all = "snake_case")]
//! enum Brightness {
//! DarkBlack,
//! Dim {
//! glow: usize,
//! },
//! #[strum(serialize = "bright")]
//! BrightWhite,
//! }
//!
//! assert_eq!(
//! String::from("dark_black"),
//! Brightness::DarkBlack.to_string().as_ref()
//! );
//! assert_eq!(
//! String::from("dim"),
//! Brightness::Dim { glow: 0 }.to_string().as_ref()
//! );
//! assert_eq!(
//! String::from("bright"),
//! Brightness::BrightWhite.to_string().as_ref()
//! );
//! ```
//!
//! You can also apply the `#[strum(ascii_case_insensitive)]` attribute to the enum,
//! and this has the same effect of applying it to every variant.
//! //!
//! Custom attributes are applied to a variant by adding `#[strum(parameter="value")]` to the variant. //! Custom attributes are applied to a variant by adding `#[strum(parameter="value")]` to the variant.
//! //!