44//!
55//! # Example
66//!
7- //! - File `toggles.txt ` conains:
8- //! ```txt
9- //! 0 FeatureA
10- //! 1 FeatureB
7+ //! - File `toggles.yaml ` conains:
8+ //! ```yaml
9+ //! FeatureA: 0
10+ //! FeatureB: 1
1111//! ```
1212//!
1313//! - Basic usage
2424//! let mut toggles: EnumToggles::<MyToggle> = EnumToggles::new();
2525//! toggles.set(MyToggle::FeatureA as usize, true);
2626//! 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);
27+ //! // toggles.load_from_file("toggles.yaml "); // Load toggles state from file
28+ //! println!("{:? }", toggles);
2929//! ```
3030//!
3131//! - With concucrency context
5757//! toggle
5858//! });
5959//!
60- //! println!("{}", TOGGLES.deref());
60+ //! println!("{:? }", TOGGLES.deref());
6161//! ```
6262//!
6363
6464use bitvec:: prelude:: * ;
65- use log:: error;
66- use std:: io:: BufRead ;
65+ use std:: fs;
6766use std:: { collections:: HashMap , fmt} ;
67+ use yaml_rust:: { Yaml , YamlLoader } ;
6868
6969/// Contains the toggle value for each item of the enum T.
7070pub struct EnumToggles < T > {
@@ -101,24 +101,23 @@ where
101101 toggles
102102 }
103103
104- /// Set all toggles value defiend in the file.
104+ /// Set all toggles value defiend in the yaml file.
105105 pub fn load_from_file ( & mut self , filepath : & str ) {
106- let file = std:: fs:: File :: open ( filepath) . expect ( "Unable to open file" ) ;
107- let reader = std:: io:: BufReader :: new ( file) ;
108- for line in reader. lines ( ) {
109- match line {
110- Ok ( line) => {
111- let parts: Vec < & str > = line. split_whitespace ( ) . collect ( ) ;
112- if parts. len ( ) == 2 {
113- if let Ok ( value) = parts[ 0 ] . parse :: < u8 > ( ) {
114- self . set_by_name ( parts[ 1 ] , value != 0 ) ;
115- }
106+ match fs:: read_to_string ( filepath) {
107+ Ok ( content) => {
108+ let docs = YamlLoader :: load_from_str ( & content) . unwrap ( ) ;
109+ let doc = & docs[ 0 ] ;
110+
111+ if let Yaml :: Hash ( ref h) = doc {
112+ for ( key, value) in h {
113+ self . set_by_name (
114+ key. as_str ( ) . unwrap_or ( "<non-string>" ) ,
115+ value. as_i64 ( ) . unwrap_or ( 0 ) == 1 ,
116+ ) ;
116117 }
117118 }
118- Err ( e) => {
119- error ! ( "Error reading line: {e}" ) ;
120- }
121119 }
120+ Err ( e) => println ! ( "Error reading file: {}" , e) ,
122121 }
123122 }
124123
@@ -170,7 +169,7 @@ where
170169}
171170
172171/// Diplay all toggles and their values.
173- impl < T > fmt:: Display for EnumToggles < T >
172+ impl < T > fmt:: Debug for EnumToggles < T >
174173where
175174 T : strum:: IntoEnumIterator + AsRef < str > + PartialEq + ' static ,
176175{
@@ -225,7 +224,7 @@ mod tests {
225224 #[ test]
226225 fn display ( ) {
227226 let toggles: EnumToggles < TestToggles > = EnumToggles :: new ( ) ;
228- assert_eq ! ( format!( "{}" , toggles) . is_empty( ) , false ) ;
227+ assert_eq ! ( format!( "{:? }" , toggles) . is_empty( ) , false ) ;
229228 }
230229
231230 #[ test]
@@ -235,10 +234,9 @@ mod tests {
235234 tempfile:: NamedTempFile :: new ( ) . expect ( "Unable to create temporary file" ) ;
236235
237236 // Write some data to the file
238- writeln ! ( temp_file, "1 Toggle1" ) . expect ( "Unable to write to temporary file" ) ;
239- writeln ! ( temp_file, "0 Toggle2" ) . expect ( "Unable to write to temporary file" ) ;
240- writeln ! ( temp_file, "0 VAR1" ) . expect ( "Unable to write to temporary file" ) ;
241- writeln ! ( temp_file, "TESTTEST" ) . expect ( "Unable to write to temporary file" ) ;
237+ writeln ! ( temp_file, "Toggle1: 1" ) . expect ( "Unable to write to temporary file" ) ;
238+ writeln ! ( temp_file, "Toggle2: 0" ) . expect ( "Unable to write to temporary file" ) ;
239+ writeln ! ( temp_file, "VAR1: 0" ) . expect ( "Unable to write to temporary file" ) ;
242240 writeln ! ( temp_file, "" ) . expect ( "Unable to write to temporary file" ) ;
243241
244242 // Get the path of the temporary file
0 commit comments