From f972876880e3d11aafa7bcdae6ce067240f49ab7 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Tue, 15 Jul 2025 19:38:06 +0300 Subject: [PATCH] feat(examples): add simple example --- Cargo.lock | 4 +-- filecaster-derive/Cargo.toml | 2 +- filecaster/Cargo.toml | 2 +- filecaster/examples/data/simple.json | 5 ++++ filecaster/examples/data/simple.toml | 3 +++ filecaster/examples/simple.rs | 39 ++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 filecaster/examples/data/simple.json create mode 100644 filecaster/examples/data/simple.toml create mode 100644 filecaster/examples/simple.rs diff --git a/Cargo.lock b/Cargo.lock index e62de89..2e07aa3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,7 +50,7 @@ checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" [[package]] name = "filecaster" -version = "0.2.2" +version = "0.2.3" dependencies = [ "filecaster-derive", "merge", @@ -62,7 +62,7 @@ dependencies = [ [[package]] name = "filecaster-derive" -version = "0.2.2" +version = "0.2.3" dependencies = [ "claims", "filecaster", diff --git a/filecaster-derive/Cargo.toml b/filecaster-derive/Cargo.toml index 0157309..c278132 100644 --- a/filecaster-derive/Cargo.toml +++ b/filecaster-derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "filecaster-derive" -version = "0.2.2" +version = "0.2.3" edition = "2024" authors = ["Kristofers Solo "] description = "Procedural derive macro for `filecaster`: automatically implement `FromFile` for your structs." diff --git a/filecaster/Cargo.toml b/filecaster/Cargo.toml index c17be3b..31673ab 100644 --- a/filecaster/Cargo.toml +++ b/filecaster/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "filecaster" -version = "0.2.2" +version = "0.2.3" edition = "2024" authors = ["Kristofers Solo "] description = "Procedural macro to derive configuration from files, with optional merging capabilities." diff --git a/filecaster/examples/data/simple.json b/filecaster/examples/data/simple.json new file mode 100644 index 0000000..5fd3dec --- /dev/null +++ b/filecaster/examples/data/simple.json @@ -0,0 +1,5 @@ +{ + "key": "json key", + "number": 123 +} + diff --git a/filecaster/examples/data/simple.toml b/filecaster/examples/data/simple.toml new file mode 100644 index 0000000..06644bb --- /dev/null +++ b/filecaster/examples/data/simple.toml @@ -0,0 +1,3 @@ +key = "toml key" +number = 456 + diff --git a/filecaster/examples/simple.rs b/filecaster/examples/simple.rs new file mode 100644 index 0000000..e1cca09 --- /dev/null +++ b/filecaster/examples/simple.rs @@ -0,0 +1,39 @@ +use filecaster::FromFile; +use std::fs; + +#[derive(Debug, FromFile)] +pub struct MyData { + #[from_file(default = "default key")] + pub key: String, + #[from_file(default = 0)] + pub number: i64, +} + +fn main() { + let current_dir = std::env::current_dir().expect("Failed to get current directory"); + let data_dir = current_dir.join("filecaster/examples/data"); + + let json_path = data_dir.join("simple.json"); + let toml_path = data_dir.join("simple.toml"); + + let json_content = fs::read_to_string(&json_path) + .unwrap_or_else(|e| panic!("Failed to read JSON file at {:?}: {}", json_path, e)); + let json_data: MyData = serde_json::from_str::(&json_content) + .unwrap_or_else(|e| panic!("Failed to parse JSON in {:?}: {}", json_path, e)) + .into(); + + assert_eq!(json_data.key, "json key".to_string()); + assert_eq!(json_data.number, 123); + + let toml_content = fs::read_to_string(&toml_path) + .unwrap_or_else(|e| panic!("Failed to read TOML file at {:?}: {}", toml_path, e)); + let toml_data: MyData = toml::from_str::(&toml_content) + .unwrap_or_else(|e| panic!("Failed to parse TOML in {:?}: {}", toml_path, e)) + .into(); + + assert_eq!(toml_data.key, "toml key".to_string()); + assert_eq!(toml_data.number, 456); + + dbg!(&json_data); + dbg!(&toml_data); +}