Skip to content

Commit 589e3d4

Browse files
committed
refactor: load yaml file and print to debug
1 parent 310042d commit 589e3d4

File tree

4 files changed

+51
-36
lines changed

4 files changed

+51
-36
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bitvec = "1.0"
1919
log = "0.4"
2020
strum = "0.27.1"
2121
strum_macros = "0.27.1"
22+
yaml-rust = "0.4.5"
2223

2324
[dev-dependencies]
2425
criterion = { version = "0.6", features = ["html_reports"] }

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ There's a direct relationship where each string name corresponds to a unique nam
2222
cargo add enum-toggles strum strum_macros
2323
```
2424

25-
- File `toggles.txt` conains:
25+
- File `toggles.yaml` conains:
2626

27-
```txt
28-
0 FeatureA
29-
1 FeatureB
27+
```yaml
28+
FeatureA: 0
29+
FeatureB: 1
3030
```
3131
3232
```rust
@@ -42,8 +42,8 @@ enum MyToggle {
4242
let mut toggles: EnumToggles::<MyToggle> = EnumToggles::new();
4343
toggles.set(MyToggle::FeatureA as usize, true);
4444
toggles.set_by_name("FeatureB", true); // Mapped to MyToggle::FeatureB
45-
// toggles.load_from_file("toggles.txt"); // Load toggles state from file
46-
println!("{}", toggles);
45+
// toggles.load_from_file("toggles.yaml"); // Load toggles state from file
46+
println!("{:?}", toggles);
4747
```
4848

4949
### Example 2: With concucrency context
@@ -76,5 +76,5 @@ pub static TOGGLES: Lazy<EnumToggles<MyToggle>> = Lazy::new(|| {
7676
toggle
7777
});
7878

79-
println!("{}", TOGGLES.deref());
79+
println!("{:?}", TOGGLES.deref());
8080
```

src/lib.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
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
@@ -24,8 +24,8 @@
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
@@ -57,14 +57,14 @@
5757
//! toggle
5858
//! });
5959
//!
60-
//! println!("{}", TOGGLES.deref());
60+
//! println!("{:?}", TOGGLES.deref());
6161
//! ```
6262
//!
6363
6464
use bitvec::prelude::*;
65-
use log::error;
66-
use std::io::BufRead;
65+
use std::fs;
6766
use std::{collections::HashMap, fmt};
67+
use yaml_rust::{Yaml, YamlLoader};
6868

6969
/// Contains the toggle value for each item of the enum T.
7070
pub 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>
174173
where
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

Comments
 (0)