From b1b4a3daebfbd3d31f0441d4c314f848ef12d799 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Tue, 15 Jul 2025 19:44:09 +0300 Subject: [PATCH] feat(examples): add nested examples --- README.md | 2 +- filecaster/examples/data/nested.json | 7 ++++ filecaster/examples/data/nested.toml | 6 +++ filecaster/examples/data/simple.json | 1 - filecaster/examples/data/simple.toml | 3 +- filecaster/examples/nested.rs | 59 ++++++++++++++++++++++++++++ filecaster/examples/simple.rs | 25 ++++++++---- 7 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 filecaster/examples/data/nested.json create mode 100644 filecaster/examples/data/nested.toml create mode 100644 filecaster/examples/nested.rs diff --git a/README.md b/README.md index e6a8b60..9bfda66 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ fn main() { assert_eq!(config.host, "localhost"); assert_eq!(config.port, 3000); - assert_eq!(config.auto_reload, false); // `Default::default()` for bool is `false` + assert_eq!(config.auto_reload, false); // `bool::default()` is `false` println!("Final Config: {:#?}", config); diff --git a/filecaster/examples/data/nested.json b/filecaster/examples/data/nested.json new file mode 100644 index 0000000..388c2f3 --- /dev/null +++ b/filecaster/examples/data/nested.json @@ -0,0 +1,7 @@ +{ + "key": "json key", + "number": 123, + "nested": { + "inner_number": 42 + } +} diff --git a/filecaster/examples/data/nested.toml b/filecaster/examples/data/nested.toml new file mode 100644 index 0000000..609a779 --- /dev/null +++ b/filecaster/examples/data/nested.toml @@ -0,0 +1,6 @@ +key = "toml key" +number = 456 + +[nested] +inner_key = "inner toml key" +inner_number = 99 diff --git a/filecaster/examples/data/simple.json b/filecaster/examples/data/simple.json index 5fd3dec..eedbda9 100644 --- a/filecaster/examples/data/simple.json +++ b/filecaster/examples/data/simple.json @@ -2,4 +2,3 @@ "key": "json key", "number": 123 } - diff --git a/filecaster/examples/data/simple.toml b/filecaster/examples/data/simple.toml index 06644bb..251407e 100644 --- a/filecaster/examples/data/simple.toml +++ b/filecaster/examples/data/simple.toml @@ -1,3 +1,2 @@ -key = "toml key" number = 456 - +exists = true diff --git a/filecaster/examples/nested.rs b/filecaster/examples/nested.rs new file mode 100644 index 0000000..4db0c39 --- /dev/null +++ b/filecaster/examples/nested.rs @@ -0,0 +1,59 @@ +use filecaster::FromFile; +use std::fs; + +#[derive(Debug, FromFile)] +pub struct InnerData { + #[from_file(default = "inner default")] + pub inner_key: String, + #[from_file(default = 42)] + pub inner_number: i32, +} + +#[derive(Debug, FromFile)] +pub struct MyData { + #[from_file(default = "default key")] + pub key: String, + #[from_file(default = 0)] + pub number: i32, + pub nested: InnerData, +} + +fn main() { + // Get the absolute current directory + let current_dir = std::env::current_dir().expect("Failed to get current directory"); + // Path to the data directory + let data_dir = current_dir.join("filecaster/examples/data"); + + // Paths to JSON and TOML files + let json_path = data_dir.join("nested.json"); + let toml_path = data_dir.join("nested.toml"); + + // Read and parse JSON file + 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(); + + // Read and parse TOML file + 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(); + + // Output the parsed data + dbg!(&json_data); + dbg!(&toml_data); + + // Example assertions (adjust based on your actual file contents) + assert_eq!(json_data.key, "json key"); + assert_eq!(json_data.number, 123); + assert_eq!(json_data.nested.inner_key, "inner default"); + assert_eq!(json_data.nested.inner_number, 42); + + assert_eq!(toml_data.key, "toml key"); + assert_eq!(toml_data.number, 456); + assert_eq!(toml_data.nested.inner_key, "inner toml key"); + assert_eq!(toml_data.nested.inner_number, 99); +} diff --git a/filecaster/examples/simple.rs b/filecaster/examples/simple.rs index e1cca09..17f3df1 100644 --- a/filecaster/examples/simple.rs +++ b/filecaster/examples/simple.rs @@ -5,35 +5,44 @@ use std::fs; pub struct MyData { #[from_file(default = "default key")] pub key: String, - #[from_file(default = 0)] - pub number: i64, + pub number: i32, + pub exists: bool, } fn main() { + // Get the absolute current directory let current_dir = std::env::current_dir().expect("Failed to get current directory"); + // Path to the data directory let data_dir = current_dir.join("filecaster/examples/data"); + // Paths to JSON and TOML files let json_path = data_dir.join("simple.json"); let toml_path = data_dir.join("simple.toml"); + // Read and parse JSON file 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); - + // Read and parse TOML file 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); - + // Output the parsed data dbg!(&json_data); dbg!(&toml_data); + + // Example assertions (adjust based on your actual file contents) + assert_eq!(json_data.key, "json key".to_string()); + assert_eq!(json_data.number, 123); + assert_eq!(json_data.exists, false); // `bool::default()` is `false` + + assert_eq!(toml_data.key, "default key".to_string()); + assert_eq!(toml_data.number, 456); + assert_eq!(toml_data.exists, true); }