diff --git a/README.md b/README.md index 0f1d6c0..3d9c63d 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,26 @@ There are some existing crates that provide similar features but `econf` is uniq * Containers: `Vec`, `HashSet`, `HashMap`, `Option`, `BTreeMap`, `BTreeSet`, `BinaryHeap`, `LinkedList`, `VecDeque`, `tuple` * Containers are parsed as YAML format. See [the tests](https://github.com/YushiOMOTE/econf/blob/master/econf/tests/basics.rs). +## Enums + +Since v0.3.0, econf requires enums to implement [FromStr](https://doc.rust-lang.org/std/str/trait.FromStr.html) trait. Without this implementation, your program will fail to compile. While you can write the `FromStr` implementation manually, you can alternatively use [strum](https://github.com/Peternator7/strum) crate to automatically generate it. `strum` provides several useful features, making it a generally recommended choice. See [econf/examples/strum.rs](https://github.com/YushiOMOTE/econf/tree/master/econf/examples/strum.rs) for example code. + +```rust +use econf::LoadEnv; + +#[derive(Debug, strum::EnumString, LoadEnv)] +#[strum(serialize_all = "kebab-case")] +enum AuthMode { + ApiKey, + BasicAuth, + #[strum(ascii_case_insensitive)] + BearerToken, + #[strum(serialize = "oauth", serialize = "OAuth")] + OAuth, + JWT, +} +``` + ## Nesting Nested structs are supported. diff --git a/econf/src/lib.rs b/econf/src/lib.rs index dc325d0..ca7cabc 100644 --- a/econf/src/lib.rs +++ b/econf/src/lib.rs @@ -66,6 +66,26 @@ //! * Containers: `Vec`, `HashSet`, `HashMap`, `Option`, `BTreeMap`, `BTreeSet`, `BinaryHeap`, `LinkedList`, `VecDeque`, `tuple` //! * Containers are parsed as YAML format. See [the tests](https://github.com/YushiOMOTE/econf/blob/master/econf/tests/basics.rs). //! +//! # Enums +//! +//! Since v0.3.0, econf requires enums to implement [FromStr](https://doc.rust-lang.org/std/str/trait.FromStr.html) trait. Without this implementation, your program will fail to compile. While you can write the `FromStr` implementation manually, you can alternatively use [strum](https://github.com/Peternator7/strum) crate to automatically generate it. `strum` provides several useful features, making it a generally recommended choice. See [econf/examples/strum.rs](https://github.com/YushiOMOTE/econf/tree/master/econf/examples/strum.rs) for example code. +//! +//! ``` +//! use econf::LoadEnv; +//! +//! #[derive(Debug, strum::EnumString, LoadEnv)] +//! #[strum(serialize_all = "kebab-case")] +//! enum AuthMode { +//! ApiKey, +//! BasicAuth, +//! #[strum(ascii_case_insensitive)] +//! BearerToken, +//! #[strum(serialize = "oauth", serialize = "OAuth")] +//! OAuth, +//! JWT, +//! } +//! ``` +//! //! # Nesting //! //! Nested structs are supported.