From 16b71407d26e76cbda3051f5327575f82210b4cc Mon Sep 17 00:00:00 2001 From: Thomas Faour Date: Thu, 19 Jun 2025 10:32:55 -0400 Subject: [PATCH] Working config file reading! --- Cargo.toml | 2 ++ src/config.rs | 15 +++++++++++++++ src/main.rs | 21 +++++++++++++++++++++ src/types.rs | 5 +++++ 4 files changed, 43 insertions(+) create mode 100644 src/config.rs create mode 100644 src/types.rs diff --git a/Cargo.toml b/Cargo.toml index ef19827..55f0a64 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..a244c5c --- /dev/null +++ b/src/config.rs @@ -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, +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 5c4cbc9..6de29c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>(path: P) -> Result> { + 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); } diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..e7e4868 --- /dev/null +++ b/src/types.rs @@ -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;