Skip to content

Commit 5044c61

Browse files
committed
Update the documentation
1 parent 0f32068 commit 5044c61

File tree

4 files changed

+122
-7
lines changed

4 files changed

+122
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "enum-toggles"
3-
version = "0.1.0"
3+
version = "0.1.2"
44
edition = "2021"
55
authors = ["Jxtopher"]
66
description = "A generic Rust library for managing toggles/flags using enums and bitvec."
@@ -16,9 +16,9 @@ crate-type = ["lib"]
1616

1717
[dependencies]
1818
bitvec = "1.0"
19+
log = "0.4"
1920
strum = "0.26"
2021
strum_macros = "0.26"
21-
log = "0.4"
2222

2323
[dev-dependencies]
2424
tempfile = "3.10"

README.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
A generic Rust library for managing toggles/flags using enums and bitvec.
44

5+
This crate provides a toggle manager that can load from a file.
6+
Toggle states are read-only and accessed in O(1) time.
7+
There's a direct relationship where each string name corresponds to a unique name in the enum.
8+
59
## Features
610

711
- Type-safe toggles based on enums
@@ -14,22 +18,55 @@ A generic Rust library for managing toggles/flags using enums and bitvec.
1418
Add to your `Cargo.toml`:
1519

1620
```toml
17-
enum-toggles = "0.1"
21+
enum-toggles = "version = "0.1.2"
1822
```
1923

20-
Example:
24+
### Example 1: Basic usage
25+
26+
- File `toggles.txt` conains:
27+
28+
```txt
29+
0 FeatureA
30+
1 FeatureB
31+
```
2132

2233
```rust
2334
use enum_toggles::EnumToggles;
2435
use strum_macros::{AsRefStr, EnumIter};
25-
2636
#[derive(AsRefStr, EnumIter, PartialEq, Copy, Clone, Debug)]
2737
pub enum MyToggle {
2838
FeatureA,
2939
FeatureB,
3040
}
31-
3241
let mut toggles = EnumToggles::<MyToggle>::new();
3342
toggles.set_enum(MyToggle::FeatureA, true);
43+
toggles.set_by_name("FeatureB", true); // Mapped to MyToggle::FeatureB
44+
toggles.load_from_file("toggles.txt"); // Load toggles state from file
3445
println!("{}", toggles);
3546
```
47+
48+
### Example 2: With concucrency context
49+
50+
```rust
51+
use once_cell::sync::Lazy;
52+
use std::env;
53+
use log::{warn};
54+
#[derive(AsRefStr, EnumIter, PartialEq, Copy, Clone, Debug)]
55+
pub enum MyToggle {
56+
FeatureA,
57+
FeatureB,
58+
}
59+
pub static TOGGLES: Lazy<Toggles<EnumToggle>> = Lazy::new(|| {
60+
let mut toggle = Toggles::new();
61+
let filepath = env::var("TOGGLES_FILE");
62+
match filepath {
63+
Ok(path) => {
64+
if !path.is_empty() {
65+
toggle.load_from_file(&path)
66+
}
67+
}
68+
Err(_) => warn!("Environment variable TOGGLES_FILE not set"),
69+
}
70+
toggle
71+
});
72+
```

src/lib.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,67 @@
1+
//! This crate provides a toggle manager that can load from a file.
2+
//! Toggle states are read-only and accessed in O(1) time.
3+
//! There's a direct relationship where each string name corresponds to a unique name in the enum.
4+
//!
5+
//! # Example
6+
//!
7+
//! - File `toggles.txt` conains:
8+
//! ```txt
9+
//! 0 FeatureA
10+
//! 1 FeatureB
11+
//! ```
12+
//!
13+
//! - Basic usage
14+
//! ```rust
15+
//! use enum_toggles::EnumToggles;
16+
//! use strum_macros::{AsRefStr, EnumIter};
17+
//!
18+
//! #[derive(AsRefStr, EnumIter, PartialEq, Copy, Clone, Debug)]
19+
//! pub enum MyToggle {
20+
//! FeatureA,
21+
//! FeatureB,
22+
//! }
23+
//!
24+
//! let mut toggles = EnumToggles::<MyToggle>::new();
25+
//! toggles.set_enum(MyToggle::FeatureA, true);
26+
//! toggles.set_by_name("FeatureB", true); // Mapped to MyToggle::FeatureB
27+
//! toggles.load_from_file("toggles.txt"); // Load toggles state from file
28+
//! println!("{}", toggles);
29+
//! ```
30+
//!
31+
//! - With concucrency context
32+
//! ```rust
33+
//! use once_cell::sync::Lazy;
34+
//! use std::env;
35+
//! use log::{warn};
36+
//!
37+
//! #[derive(AsRefStr, EnumIter, PartialEq, Copy, Clone, Debug)]
38+
//! pub enum MyToggle {
39+
//! FeatureA,
40+
//! FeatureB,
41+
//! }
42+
//!
43+
//! pub static TOGGLES: Lazy<Toggles<EnumToggle>> = Lazy::new(|| {
44+
//! let mut toggle = Toggles::new();
45+
//! let filepath = env::var("TOGGLES_FILE");
46+
//! match filepath {
47+
//! Ok(path) => {
48+
//! if !path.is_empty() {
49+
//! toggle.load_from_file(&path)
50+
//! }
51+
//! }
52+
//! Err(_) => warn!("Environment variable TOGGLES_FILE not set"),
53+
//! }
54+
//! toggle
55+
//! });
56+
//! ```
57+
//!
58+
159
use bitvec::prelude::*;
260
use log::error;
361
use std::io::BufRead;
462
use std::{collections::HashMap, fmt};
563

64+
/// Contains the toggle value for each item of the enum T.
665
pub struct EnumToggles<T> {
766
toggles_value: BitVec,
867
_marker: std::marker::PhantomData<T>,
@@ -19,10 +78,15 @@ where
1978
}
2079
}
2180
}
81+
82+
/// Handle the toggle value of an enum T.
2283
impl<T> EnumToggles<T>
2384
where
2485
T: strum::IntoEnumIterator + AsRef<str> + PartialEq + 'static,
2586
{
87+
/// Create a new instance of `EnumToggles` with all toggles set to false.
88+
///
89+
/// This operation is *O*(*n*).
2690
pub fn new() -> Self {
2791
let mut toggles: EnumToggles<T> = EnumToggles {
2892
toggles_value: bitvec![0; T::iter().count()],
@@ -32,6 +96,7 @@ where
3296
toggles
3397
}
3498

99+
/// Set all toggles value defiend in the file.
35100
pub fn load_from_file(&mut self, filepath: &str) {
36101
let file = std::fs::File::open(filepath).expect("Unable to open file");
37102
let reader = std::io::BufReader::new(file);
@@ -52,6 +117,9 @@ where
52117
}
53118
}
54119

120+
/// Set the bool value of all toggles based on a HashMap.
121+
///
122+
/// This operation is *O*(*n²*).
55123
pub fn set_all(&mut self, init: HashMap<String, bool>) {
56124
self.toggles_value.fill(false);
57125
for toggle in T::iter() {
@@ -63,6 +131,9 @@ where
63131
}
64132
}
65133

134+
/// Set the bool value of a toggle by its name.
135+
///
136+
/// This operation is *O*(*n*).
66137
fn set_by_name(&mut self, toggle_name: &str, value: bool) {
67138
if let Some(toggle) = T::iter().find(|t| toggle_name == t.as_ref()) {
68139
if let Some(toggle_id) = T::iter().position(|x| x == toggle) {
@@ -71,15 +142,22 @@ where
71142
}
72143
}
73144

145+
/// Set the bool value of a toggle by toggle id.
146+
///
147+
/// This operation is *O*(*1*).
74148
pub fn set(&mut self, toggle_id: usize, value: bool) {
75149
self.toggles_value.set(toggle_id, value);
76150
}
77151

152+
/// Get the bool value of a toggle by toggle id.
153+
///
154+
/// This operation is *O*(*1*).
78155
pub fn get(&self, toggle_id: usize) -> bool {
79156
self.toggles_value[toggle_id]
80157
}
81158
}
82159

160+
/// Diplay all toggles and their values.
83161
impl<T> fmt::Display for EnumToggles<T>
84162
where
85163
T: strum::IntoEnumIterator + AsRef<str> + PartialEq + 'static,

0 commit comments

Comments
 (0)