From 20996e7155d17a42ddc3bbead0e478589763c852 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Wed, 19 Sep 2018 09:10:48 +1200 Subject: [PATCH] Updated documentation. Issue #21 --- strum/src/lib.rs | 51 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/strum/src/lib.rs b/strum/src/lib.rs index 6cdfcf7a..00b7f25c 100644 --- a/strum/src/lib.rs +++ b/strum/src/lib.rs @@ -14,8 +14,8 @@ //! //! ```toml //! [dependencies] -//! strum = "0.9.0" -//! strum_macros = "0.9.0" +//! strum = "0.11.0" +//! strum_macros = "0.11.0" //! ``` //! //! And add these lines to the root of your project, either lib.rs or main.rs. @@ -79,10 +79,10 @@ //! # fn main() {} //! ``` //! -//! Note that the implementation of `FromStr` only matches on the name of the variant. -//! Strum, where possible, avoids operations that have an unknown runtime cost, and parsing strings -//! is potentially an expensive operation. If you do need that behavior, consider the more powerful -//! Serde library for your serialization. +//! Note that the implementation of `FromStr` by default only matches on the name of the +//! variant. There is an option to match on different case conversions through the +//! `#[strum(serialize_all = "snake_case")]` type attribute. See the **Additional Attributes** +//! Section for more information on using this feature. //! //! 2. `Display`, `ToString`: both derives print out the given enum variant. This enables you to perform round trip //! style conversions from enum into string and back again for unit style variants. `ToString` and `Display` @@ -293,8 +293,43 @@ //! //! # Additional Attributes //! -//! Strum supports several custom attributes to modify the generated code. Custom attributes are -//! applied to a variant by adding #[strum(parameter="value")] to the variant. +//! Strum supports several custom attributes to modify the generated code. At the enum level, the +//! `#[strum(serialize_all = "snake_case")]` attribute can be used to change the case used when +//! serializing to and deserializing from strings: +//! +//! ```rust +//! extern crate strum; +//! #[macro_use] +//! extern crate strum_macros; +//! +//! #[derive(Debug, Eq, PartialEq, ToString)] +//! #[strum(serialize_all = "snake_case")] +//! enum Brightness { +//! DarkBlack, +//! Dim { +//! glow: usize, +//! }, +//! #[strum(serialize = "bright")] +//! BrightWhite, +//! } +//! +//! fn main() { +//! 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() +//! ); +//! } +//! ``` +//! +//! Custom attributes are applied to a variant by adding #[strum(parameter="value")] to the variant. //! //! - `serialize="..."`: Changes the text that `FromStr()` looks for when parsing a string. This attribute can //! be applied multiple times to an element and the enum variant will be parsed if any of them match.