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+
159use bitvec:: prelude:: * ;
260use log:: error;
361use std:: io:: BufRead ;
462use std:: { collections:: HashMap , fmt} ;
563
64+ /// Contains the toggle value for each item of the enum T.
665pub 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.
2283impl < T > EnumToggles < T >
2384where
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( ) ] ,
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) ;
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 ( ) {
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.
83161impl < T > fmt:: Display for EnumToggles < T >
84162where
85163 T : strum:: IntoEnumIterator + AsRef < str > + PartialEq + ' static ,
0 commit comments