summaryrefslogtreecommitdiff
path: root/crates/utils/cfg_file/cfg_file_example
diff options
context:
space:
mode:
Diffstat (limited to 'crates/utils/cfg_file/cfg_file_example')
-rw-r--r--crates/utils/cfg_file/cfg_file_example/Cargo.toml10
-rw-r--r--crates/utils/cfg_file/cfg_file_example/src/main.rs45
2 files changed, 55 insertions, 0 deletions
diff --git a/crates/utils/cfg_file/cfg_file_example/Cargo.toml b/crates/utils/cfg_file/cfg_file_example/Cargo.toml
new file mode 100644
index 0000000..932bc31
--- /dev/null
+++ b/crates/utils/cfg_file/cfg_file_example/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "cfg_file_example"
+edition = "2024"
+version.workspace = true
+
+[dependencies]
+cfg_file = { path = "../../cfg_file" }
+cfg_file_derive = { path = "../cfg_file_derive" }
+tokio = { version = "1.46.1", features = ["full"] }
+serde = { version = "1.0.219", features = ["derive"] } \ No newline at end of file
diff --git a/crates/utils/cfg_file/cfg_file_example/src/main.rs b/crates/utils/cfg_file/cfg_file_example/src/main.rs
new file mode 100644
index 0000000..aacef4e
--- /dev/null
+++ b/crates/utils/cfg_file/cfg_file_example/src/main.rs
@@ -0,0 +1,45 @@
+use std::collections::HashMap;
+use cfg_file_derive::ConfigFile;
+use serde::{Deserialize, Serialize};
+use cfg_file::config::ConfigFile;
+
+#[derive(ConfigFile, Deserialize, Serialize, Default)]
+#[cfg_file(path = "./.temp/example/cfg_file/example_cfg.toml")]
+struct ExampleConfig {
+ name: String,
+ age: i32,
+ hobby: Vec<String>,
+ secret: HashMap<String, String>,
+}
+
+#[tokio::main]
+async fn main() {
+ let mut example = ExampleConfig {
+ name: "Weicao".to_string(),
+ age: 22,
+ hobby: vec![ "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());
+
+ ExampleConfig::write(&example).await; // Write to default path.
+
+ // Read from default path.
+ let read_cfg = ExampleConfig::read().await;
+ 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);
+}