feat(examples): add simple example

This commit is contained in:
Kristofers Solo 2025-07-15 19:38:06 +03:00
parent 89732ff8e2
commit f972876880
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
6 changed files with 51 additions and 4 deletions

4
Cargo.lock generated
View File

@ -50,7 +50,7 @@ checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
[[package]] [[package]]
name = "filecaster" name = "filecaster"
version = "0.2.2" version = "0.2.3"
dependencies = [ dependencies = [
"filecaster-derive", "filecaster-derive",
"merge", "merge",
@ -62,7 +62,7 @@ dependencies = [
[[package]] [[package]]
name = "filecaster-derive" name = "filecaster-derive"
version = "0.2.2" version = "0.2.3"
dependencies = [ dependencies = [
"claims", "claims",
"filecaster", "filecaster",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "filecaster-derive" name = "filecaster-derive"
version = "0.2.2" version = "0.2.3"
edition = "2024" edition = "2024"
authors = ["Kristofers Solo <dev@kristofers.xyz>"] authors = ["Kristofers Solo <dev@kristofers.xyz>"]
description = "Procedural derive macro for `filecaster`: automatically implement `FromFile` for your structs." description = "Procedural derive macro for `filecaster`: automatically implement `FromFile` for your structs."

View File

@ -1,6 +1,6 @@
[package] [package]
name = "filecaster" name = "filecaster"
version = "0.2.2" version = "0.2.3"
edition = "2024" edition = "2024"
authors = ["Kristofers Solo <dev@kristofers.xyz>"] authors = ["Kristofers Solo <dev@kristofers.xyz>"]
description = "Procedural macro to derive configuration from files, with optional merging capabilities." description = "Procedural macro to derive configuration from files, with optional merging capabilities."

View File

@ -0,0 +1,5 @@
{
"key": "json key",
"number": 123
}

View File

@ -0,0 +1,3 @@
key = "toml key"
number = 456

View File

@ -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::<MyDataFile>(&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::<MyDataFile>(&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);
}