Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wiki to doc tests #121

Merged
merged 3 commits into from Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 68 additions & 0 deletions strum/src/additional_attributes.rs
@@ -0,0 +1,68 @@
//! # Documentation for Additional Attributes
//!
//! 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
//! 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()
//! );
//! ```
//!
//! 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.
//!
//! - `to_string="..."`: Similar to `serialize`. This value will be included when using `FromStr()`. More importantly,
//! this specifies what text to use when calling `variant.to_string()` with the `ToString` derivation, or when calling `variant.as_ref()` with `AsRefStr`.
//!
//! - `default`: Applied to a single variant of an enum. The variant must be a Tuple-like
//! variant with a single piece of data that can be create from a `&str` i.e. `T: From<&str>`.
//! The generated code will now return the variant with the input string captured as shown below
//! instead of failing.
//!
//! ```rust,ignore
//! // Replaces this:
//! _ => Err(strum::ParseError::VariantNotFound)
//! // With this in generated code:
//! default => Ok(Variant(default.into()))
//! ```
//! The plugin will fail if the data doesn't implement From<&str>. You can only have one `default`
//! on your enum.
//!
//! - `disabled`: removes variant from generated code.
//!
//! - `message=".."`: Adds a message to enum variant. This is used in conjunction with the `EnumMessage`
//! trait to associate a message with a variant. If `detailed_message` is not provided,
//! then `message` will also be returned when get_detailed_message() is called.
//!
//! - `detailed_message=".."`: Adds a more detailed message to a variant. If this value is omitted, then
//! `message` will be used in it's place.
//!
//! - `props(key="value")`: Enables associating additional information with a given variant.
31 changes: 3 additions & 28 deletions strum/src/lib.rs
Expand Up @@ -20,33 +20,9 @@
//! strum_macros = "0.18.0"
//! ```
//!
//! # Strum Macros
//!
//! Strum has implemented the following macros:
//!
//! | Macro | Description |
//! | --- | ----------- |
//! | [EnumString] | Converts strings to enum variants based on their name |
//! | [Display] | Converts enum variants to strings |
//! | [AsRefStr] | Converts enum variants to `&'static str` |
//! | [IntoStaticStr] | Implements `From<MyEnum> for &'static str` on an enum |
//! | [EnumVariantNames] | Implements Strum::VariantNames which adds an associated constant `VARIANTS` which is an array of discriminant names |
//! | [EnumIter] | Creates a new type that iterates of the variants of an enum. |
//! | [EnumProperty] | Add custom properties to enum variants. |
//! | [EnumMessage] | Add a verbose message to an enum variant. |
//! | [EnumDiscriminants] | Generate a new type with only the discriminant names. |
//! | [EnumCount] | Add a constant `usize` equal to the number of variants. |
//!
//! [EnumString]: https://github.com/Peternator7/strum/wiki/Derive-EnumString
//! [Display]: https://github.com/Peternator7/strum/wiki/Derive-Display
//! [AsRefStr]: https://github.com/Peternator7/strum/wiki/Derive-AsRefStr
//! [IntoStaticStr]: https://github.com/Peternator7/strum/wiki/Derive-IntoStaticStr
//! [EnumVariantNames]: https://github.com/Peternator7/strum/wiki/Derive-EnumVariantNames
//! [EnumIter]: https://github.com/Peternator7/strum/wiki/Derive-EnumIter
//! [EnumProperty]: https://github.com/Peternator7/strum/wiki/Derive-EnumProperty
//! [EnumMessage]: https://github.com/Peternator7/strum/wiki/Derive-EnumMessage
//! [EnumDiscriminants]: https://github.com/Peternator7/strum/wiki/Derive-EnumDiscriminants
//! [EnumCount]: https://github.com/Peternator7/strum/wiki/Derive-EnumCount

// only for documentation purposes
pub mod additional_attributes;

/// The ParseError enum is a collection of all the possible reasons
/// an enum can fail to parse from a string.
Expand Down Expand Up @@ -204,5 +180,4 @@ pub trait VariantNames {
}

#[cfg(feature = "derive")]
#[doc(hidden)]
pub use strum_macros::*;
3 changes: 3 additions & 0 deletions strum_macros/Cargo.toml
Expand Up @@ -24,6 +24,9 @@ proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0", features = ["parsing", "extra-traits"] }

[dev-dependencies]
strum = "0.19"

[features]
verbose-enumstring-name = []
verbose-asrefstr-name = []
Expand Down