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

Updated Version. Changed Readme

This commit is contained in:
Peternator7 2017-04-15 18:54:49 -07:00
parent bb2a37e9b6
commit fbea713633
4 changed files with 78 additions and 11 deletions

View File

@ -45,7 +45,8 @@ only dump the code generated on a type named `YourType`.
Strum has implemented the following macros:
1. `EnumString`: auto-derives `std::str::FromStr` on the enum. Each variant of the enum will match on it's
own name. This can be overridden using `serialize="DifferentName"` on the attribute as shown below.
own name. This can be overridden using `serialize="DifferentName"` or `to_string="DifferentName"`
on the attribute as shown below.
Multiple deserializations can be added to the same variant. If the variant contains additional data,
they will be set to their default values upon deserialization.
@ -95,7 +96,40 @@ Strum has implemented the following macros:
is potentially an expensive operation. If you do need that behavior, consider the more powerful
Serde library for your serialization.
2. `EnumIter`: iterate over the variants of an Enum. Any additional data on your variants will be
2. `ToString`: prints out the given enum variant as a string. This enables you to perform round trip
style conversions from enum into string and back again for unit style variants. `ToString` chooses
which serialization to used based on the following criteria:
1. If there is a `to_string` property, this value will be used. There can only be one per variant.
2. Of the various `serialize` properties, the value with the longest length is chosen. If that
behavior isn't desired, you should use `to_string`.
3. The name of the variant will be used if there are no `serialize` or `to_string` attributes.
```rust
// You need to bring the type into scope to use it!!!
use std::string::ToString;
#[derive(ToString,Debug)]
enum Color {
#[strum(serialize="redred")]
Red,
Green { range:usize },
Blue(usize),
Yellow,
}
// It's simple to iterate over the variants of an enum.
fn debug_colors() {
let red = Color::Red;
assert_eq!(String::from("redred"), red.to_string());
}
fn main() {
debug_colors();
}
```
3. `EnumIter`: iterate over the variants of an Enum. Any additional data on your variants will be
set to `Default::default()`. The macro implements `strum::IntoEnumIter` on your enum and
creates a new type called `YourEnumIter` that is the iterator object. You cannot derive
`EnumIter` on any type with a lifetime bound (`<'a>`) because the iterator would surely
@ -125,7 +159,7 @@ Strum has implemented the following macros:
}
```
3. `EnumMessage`: encode strings into the enum itself. This macro implements
4. `EnumMessage`: encode strings into the enum itself. This macro implements
the `strum::EnumMessage` trait. `EnumMessage` looks for
`#[strum(message="...")]` attributes on your variants.
You can also provided a `detailed_message="..."` attribute to create a
@ -187,7 +221,7 @@ Strum has implemented the following macros:
```
4. `EnumProperty`: Enables the encoding of arbitary constants into enum variants. This method
5. `EnumProperty`: Enables the encoding of arbitary constants into enum variants. This method
currently only supports adding additional string values. Other types of literals are still
experimental in the rustc compiler. The generated code works by nesting match statements.
The first match statement matches on the type of the enum, and the inner match statement

View File

@ -1,6 +1,6 @@
[package]
name = "strum"
version = "0.5.1"
version = "0.6.0"
authors = ["Peter Glotfelty <peglotfe@microsoft.com>"]
license = "MIT"
@ -13,7 +13,7 @@ homepage = "https://github.com/Peternator7/strum"
readme = "../README.md"
[dev-dependencies]
strum_macros = "0.5.0"
strum_macros = "0.6.0"
# strum_macros = { path = "../strum_macros" }

View File

@ -33,7 +33,7 @@
//! Strum has implemented the following macros:
//!
//! 1. `EnumString`: auto-derives `std::str::FromStr` on the enum. Each variant of the enum will match on it's
//! own name. This can be overridden using `serialize="DifferentName"` on the attribute as shown below.
//! own name. This can be overridden using `serialize="DifferentName"` or `to_string="DifferentName"`on the attribute as shown below.
//! Multiple deserializations can be added to the same variant. If the variant contains additional data,
//! they will be set to their default values upon deserialization.
//!
@ -84,7 +84,40 @@
//! is potentially an expensive operation. If you do need that behavior, consider the more powerful
//! Serde library for your serialization.
//!
//! 2. `EnumIter`: iterate over the variants of an Enum. Any additional data on your variants will be
//! 2. `ToString`: prints out the given enum variant as a string. This enables you to perform round trip
//! style conversions from enum into string and back again for unit style variants. `ToString` chooses
//! which serialization to used based on the following criteria:
//!
//! 1. If there is a `to_string` property, this value will be used. There can only be one per variant.
//! 2. Of the various `serialize` properties, the value with the longest length is chosen. If that
//! behavior isn't desired, you should use `to_string`.
//! 3. The name of the variant will be used if there are no `serialize` or `to_string` attributes.
//!
//! ```rust
//! # extern crate strum;
//! # #[macro_use] extern crate strum_macros;
//! // You need to bring the type into scope to use it!!!
//! use std::string::ToString;
//!
//! #[derive(ToString, Debug)]
//! enum Color {
//! #[strum(serialize="redred")]
//! Red,
//! Green { range:usize },
//! Blue(usize),
//! Yellow,
//! }
//!
//! // It's simple to iterate over the variants of an enum.
//! fn debug_colors() {
//! let red = Color::Red;
//! assert_eq!(String::from("redred"), red.to_string());
//! }
//!
//! fn main () { debug_colors(); }
//! ```
//!
//! 3. `EnumIter`: iterate over the variants of an Enum. Any additional data on your variants will be
//! set to `Default::default()`. The macro implements `strum::IntoEnumIter` on your enum and
//! creates a new type called `YourEnumIter` that is the iterator object. You cannot derive
//! `EnumIter` on any type with a lifetime bound (`<'a>`) because the iterator would surely
@ -117,7 +150,7 @@
//! }
//! ```
//!
//! 3. `EnumMessage`: encode strings into the enum itself. This macro implements
//! 4. `EnumMessage`: encode strings into the enum itself. This macro implements
//! the `strum::EnumMessage` trait. `EnumMessage` looks for
//! `#[strum(message="...")]` attributes on your variants.
//! You can also provided a `detailed_message="..."` attribute to create a
@ -181,7 +214,7 @@
//! # fn main() {}
//! ```
//!
//! 4. `EnumProperty`: Enables the encoding of arbitary constants into enum variants. This method
//! 5. `EnumProperty`: Enables the encoding of arbitary constants into enum variants. This method
//! currently only supports adding additional string values. Other types of literals are still
//! experimental in the rustc compiler. The generated code works by nesting match statements.
//! The first match statement matches on the type of the enum, and the inner match statement

View File

@ -1,6 +1,6 @@
[package]
name = "strum_macros"
version = "0.5.0"
version = "0.6.0"
authors = ["Peter Glotfelty <peglotfe@microsoft.com>"]
license = "MIT"