Working config file reading!
This commit is contained in:
parent
97cad94f6e
commit
16b71407d2
@ -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
15
src/config.rs
Normal 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>,
|
||||
}
|
21
src/main.rs
21
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<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
5
src/types.rs
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user