mirror of
https://github.com/kristoferssolo/filecaster.git
synced 2025-10-21 19:00:34 +00:00
feat(examples): add simple example
This commit is contained in:
parent
89732ff8e2
commit
f972876880
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -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",
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "filecaster-derive"
|
||||
version = "0.2.2"
|
||||
version = "0.2.3"
|
||||
edition = "2024"
|
||||
authors = ["Kristofers Solo <dev@kristofers.xyz>"]
|
||||
description = "Procedural derive macro for `filecaster`: automatically implement `FromFile` for your structs."
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "filecaster"
|
||||
version = "0.2.2"
|
||||
version = "0.2.3"
|
||||
edition = "2024"
|
||||
authors = ["Kristofers Solo <dev@kristofers.xyz>"]
|
||||
description = "Procedural macro to derive configuration from files, with optional merging capabilities."
|
||||
|
||||
5
filecaster/examples/data/simple.json
Normal file
5
filecaster/examples/data/simple.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"key": "json key",
|
||||
"number": 123
|
||||
}
|
||||
|
||||
3
filecaster/examples/data/simple.toml
Normal file
3
filecaster/examples/data/simple.toml
Normal file
@ -0,0 +1,3 @@
|
||||
key = "toml key"
|
||||
number = 456
|
||||
|
||||
39
filecaster/examples/simple.rs
Normal file
39
filecaster/examples/simple.rs
Normal 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);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user