mirror of
https://github.com/kristoferssolo/filecaster.git
synced 2025-10-21 19:00:34 +00:00
feat(examples): add nested examples
This commit is contained in:
parent
f972876880
commit
b1b4a3daeb
@ -49,7 +49,7 @@ fn main() {
|
|||||||
|
|
||||||
assert_eq!(config.host, "localhost");
|
assert_eq!(config.host, "localhost");
|
||||||
assert_eq!(config.port, 3000);
|
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);
|
println!("Final Config: {:#?}", config);
|
||||||
|
|
||||||
|
|||||||
7
filecaster/examples/data/nested.json
Normal file
7
filecaster/examples/data/nested.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"key": "json key",
|
||||||
|
"number": 123,
|
||||||
|
"nested": {
|
||||||
|
"inner_number": 42
|
||||||
|
}
|
||||||
|
}
|
||||||
6
filecaster/examples/data/nested.toml
Normal file
6
filecaster/examples/data/nested.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
key = "toml key"
|
||||||
|
number = 456
|
||||||
|
|
||||||
|
[nested]
|
||||||
|
inner_key = "inner toml key"
|
||||||
|
inner_number = 99
|
||||||
@ -2,4 +2,3 @@
|
|||||||
"key": "json key",
|
"key": "json key",
|
||||||
"number": 123
|
"number": 123
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,2 @@
|
|||||||
key = "toml key"
|
|
||||||
number = 456
|
number = 456
|
||||||
|
exists = true
|
||||||
|
|||||||
59
filecaster/examples/nested.rs
Normal file
59
filecaster/examples/nested.rs
Normal file
@ -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::<MyDataFile>(&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::<MyDataFile>(&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);
|
||||||
|
}
|
||||||
@ -5,35 +5,44 @@ use std::fs;
|
|||||||
pub struct MyData {
|
pub struct MyData {
|
||||||
#[from_file(default = "default key")]
|
#[from_file(default = "default key")]
|
||||||
pub key: String,
|
pub key: String,
|
||||||
#[from_file(default = 0)]
|
pub number: i32,
|
||||||
pub number: i64,
|
pub exists: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// Get the absolute current directory
|
||||||
let current_dir = std::env::current_dir().expect("Failed to get 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");
|
let data_dir = current_dir.join("filecaster/examples/data");
|
||||||
|
|
||||||
|
// Paths to JSON and TOML files
|
||||||
let json_path = data_dir.join("simple.json");
|
let json_path = data_dir.join("simple.json");
|
||||||
let toml_path = data_dir.join("simple.toml");
|
let toml_path = data_dir.join("simple.toml");
|
||||||
|
|
||||||
|
// Read and parse JSON file
|
||||||
let json_content = fs::read_to_string(&json_path)
|
let json_content = fs::read_to_string(&json_path)
|
||||||
.unwrap_or_else(|e| panic!("Failed to read JSON file at {:?}: {}", json_path, e));
|
.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)
|
let json_data: MyData = serde_json::from_str::<MyDataFile>(&json_content)
|
||||||
.unwrap_or_else(|e| panic!("Failed to parse JSON in {:?}: {}", json_path, e))
|
.unwrap_or_else(|e| panic!("Failed to parse JSON in {:?}: {}", json_path, e))
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
assert_eq!(json_data.key, "json key".to_string());
|
// Read and parse TOML file
|
||||||
assert_eq!(json_data.number, 123);
|
|
||||||
|
|
||||||
let toml_content = fs::read_to_string(&toml_path)
|
let toml_content = fs::read_to_string(&toml_path)
|
||||||
.unwrap_or_else(|e| panic!("Failed to read TOML file at {:?}: {}", toml_path, e));
|
.unwrap_or_else(|e| panic!("Failed to read TOML file at {:?}: {}", toml_path, e));
|
||||||
let toml_data: MyData = toml::from_str::<MyDataFile>(&toml_content)
|
let toml_data: MyData = toml::from_str::<MyDataFile>(&toml_content)
|
||||||
.unwrap_or_else(|e| panic!("Failed to parse TOML in {:?}: {}", toml_path, e))
|
.unwrap_or_else(|e| panic!("Failed to parse TOML in {:?}: {}", toml_path, e))
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
assert_eq!(toml_data.key, "toml key".to_string());
|
// Output the parsed data
|
||||||
assert_eq!(toml_data.number, 456);
|
|
||||||
|
|
||||||
dbg!(&json_data);
|
dbg!(&json_data);
|
||||||
dbg!(&toml_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);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user