Working config file reading!

This commit is contained in:
Thomas Faour 2025-06-19 10:32:55 -04:00
parent 97cad94f6e
commit 16b71407d2
4 changed files with 43 additions and 0 deletions

View File

@ -8,3 +8,5 @@ authors = ["thomas"]
[dependencies]
clap = { version = "4.5.39", features = ["derive"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"

15
src/config.rs Normal file
View File

@ -0,0 +1,15 @@
use crate::types;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Body {
name: String,
mass: types::Mass,
position: types::Position,
velocity: types::Velocity,
}
#[derive(Debug, Deserialize)]
pub struct ConfigFile {
bodies: Vec<Body>,
}

View File

@ -1,5 +1,13 @@
use clap::Parser;
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
mod types;
mod config;
#[derive(Parser, Debug)]
#[command(
@ -18,8 +26,21 @@ struct Args {
step_size: u8,
}
fn read_config<P: AsRef<Path>>(path: P) -> Result<config::ConfigFile, Box<dyn Error>> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let u = serde_json::from_reader(reader)?;
Ok(u)
}
//fn parse_time(arg: &str)
fn main() {
let args = Args::parse();
println!("Loading initial parameters from {}", args.config);
let conf = read_config(args.config).unwrap();
println!("{:#?}", conf);
}

5
src/types.rs Normal file
View File

@ -0,0 +1,5 @@
pub type Hval = f64;
pub type Position = [Hval; 3];
pub type Velocity = [Hval; 3];
pub type Acceleration = [Hval; 3];
pub type Mass = Hval;