Skip to content

Commit

Permalink
Document about enum and strum
Browse files Browse the repository at this point in the history
  • Loading branch information
yukinarit committed Sep 10, 2024
1 parent 0b9d9b8 commit 7ee8867
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions econf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7ee8867

Please sign in to comment.