Skip to content

Commit

Permalink
Updated documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
azriel91 committed Sep 20, 2018
1 parent d34c1d7 commit 03a95c0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 16 deletions.
51 changes: 43 additions & 8 deletions README.md
Expand Up @@ -14,8 +14,8 @@ Cargo.toml. Strum_macros contains the macros needed to derive all the traits in

```toml
[dependencies]
strum = "0.10.0"
strum_macros = "0.10.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.
Expand Down Expand Up @@ -91,10 +91,10 @@ Strum has implemented the following macros:
*/
```

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`: prints out the given enum. This enables you to perform round trip
style conversions from enum into string and back again for unit style variants. `ToString` and
Expand Down Expand Up @@ -298,8 +298,43 @@ Strum has implemented the following macros:

# 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.
Expand Down
51 changes: 43 additions & 8 deletions strum/src/lib.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 03a95c0

Please sign in to comment.