diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-01-02 23:17:26 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-01-02 23:17:26 +0800 |
| commit | 118f24df4024099b6fbdd6d74fca46ae089ac07a (patch) | |
| tree | 62211f21987137d2f3e09ad489f3fc105a2d97db /crates/utils/cfg_file/cfg_file_test/src | |
| parent | ba11b73da83d5f6c66129b52cff7a45d8994a6a0 (diff) | |
Add support for Bincode config file format
Diffstat (limited to 'crates/utils/cfg_file/cfg_file_test/src')
| -rw-r--r-- | crates/utils/cfg_file/cfg_file_test/src/lib.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/crates/utils/cfg_file/cfg_file_test/src/lib.rs b/crates/utils/cfg_file/cfg_file_test/src/lib.rs index 4db4c22..f70d00d 100644 --- a/crates/utils/cfg_file/cfg_file_test/src/lib.rs +++ b/crates/utils/cfg_file/cfg_file_test/src/lib.rs @@ -14,6 +14,15 @@ mod test_cfg_file { secret: HashMap<String, String>, } + #[derive(ConfigFile, Deserialize, Serialize, Default)] + #[cfg_file(path = "./.temp/example_bincode.bcfg")] + struct ExampleBincodeConfig { + name: String, + age: i32, + hobby: Vec<String>, + secret: HashMap<String, String>, + } + #[tokio::test] async fn test_config_file_serialization() { let mut example = ExampleConfig { @@ -48,4 +57,39 @@ mod test_cfg_file { assert_eq!(read_cfg.secret["No comments"], secret_no_comments); assert_eq!(read_cfg.secret["Peek"], secret_peek); } + + #[tokio::test] + async fn test_bincode_config_file_serialization() { + let mut example = ExampleBincodeConfig { + name: "Weicao".to_string(), + age: 22, + hobby: ["Programming", "Painting"] + .iter() + .map(|m| m.to_string()) + .collect(), + secret: HashMap::new(), + }; + let secret_no_comments = + "Actually, I'm really too lazy to write comments, documentation, and unit tests."; + example + .secret + .entry("No comments".to_string()) + .insert_entry(secret_no_comments.to_string()); + + let secret_peek = "Of course, it's peeking at you who's reading the source code."; + example + .secret + .entry("Peek".to_string()) + .insert_entry(secret_peek.to_string()); + + ExampleBincodeConfig::write(&example).await.unwrap(); // Write to default path. + + // Read from default path. + let read_cfg = ExampleBincodeConfig::read().await.unwrap(); + assert_eq!(read_cfg.name, "Weicao"); + assert_eq!(read_cfg.age, 22); + assert_eq!(read_cfg.hobby, vec!["Programming", "Painting"]); + assert_eq!(read_cfg.secret["No comments"], secret_no_comments); + assert_eq!(read_cfg.secret["Peek"], secret_peek); + } } |
