From bb968d770ef9a31c44cbc6c171913b8c72e2a70f Mon Sep 17 00:00:00 2001 From: Ayrton Chilibeck Date: Fri, 12 Jan 2024 21:34:45 -0700 Subject: [PATCH] Added Configuration parsing module --- Cargo.lock | 1 + Cargo.toml | 1 + src/util.rs | 2 +- src/util/config.rs | 74 +++++++++++++++++++++++++++++++++++++++------- 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5303825..a646b96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -209,6 +209,7 @@ name = "tester" version = "0.1.0" dependencies = [ "clap", + "serde", "serde_yaml", ] diff --git a/Cargo.toml b/Cargo.toml index 88a5171..b78d78d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] clap = { version = "4.4.16", features = ["derive"] } +serde = { version = "1.0.195", features = ["derive"] } serde_yaml = "0.9.30" diff --git a/src/util.rs b/src/util.rs index 1bf79df..ef68c36 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1 +1 @@ -mod config; +pub mod config; diff --git a/src/util/config.rs b/src/util/config.rs index 97dd99c..1aa5457 100644 --- a/src/util/config.rs +++ b/src/util/config.rs @@ -1,32 +1,84 @@ -use std::{path::PathBuf, collections::HashMap}; +use std::path::PathBuf; -use serde_yaml; +use serde::Deserialize; +use serde_yaml::{self}; -#[derive(Default)] -struct Config { - executable_paths: HashMap, +#[derive(Debug, Deserialize)] +pub struct Config { + tested_executables: Vec, input_path: PathBuf, output_path: PathBuf, + in_stream_path: PathBuf, + + runtimes: Option>, toolchains: Vec, } -#[derive(Default)] -struct Toolchain { +#[derive(Debug, Deserialize)] +pub struct Team { name: String, - steps: Vec, + executable: PathBuf, } -#[derive(Default)] -struct Step { +#[derive(Debug, Deserialize)] +pub struct Toolchain { + name: String, + steps: Vec,} + +#[derive(Debug, Deserialize)] +pub struct Step { name: String, executable_path: Option, // if None then we use the current executable path arguments: Vec, // special string $INPUT corresponds to previous step output output: String, // the output file name + + uses_runtime: Option, + uses_in_stream: Option, + allow_error: Option } -pub fn parse_config(path: PathBuf) { +pub fn parse_config(path: PathBuf) -> Config { + // load the yaml from the system path, if it fails, tell the user and exit + let yaml_load = std::fs::File::open(path); + let yaml_file = match yaml_load { + Ok(file) => file, + Err(error) => panic!("Failed to load configuration file 🤮, error thrown: {:?}", error), + }; + let config: Config = match serde_yaml::from_reader(yaml_file) { + Ok(conf) => conf, + Err(error) => panic!("Failed to parse YAML file 😕, error: {}", error), + }; + + return config; +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[should_panic] + fn test_nonexistant_config() { + let path: PathBuf = PathBuf::from("src/util/test/parse_config/non_existant.yaml"); + parse_config(path); + } + + #[test] + fn test_load_config() { + let path: PathBuf = PathBuf::from("src/util/test/parse_config/config.yaml"); + let config = parse_config(path); + println!("{:?}", config); + } + + #[test] + #[should_panic] + fn test_load_bad_config() { + let path: PathBuf = PathBuf::from("src/util/test/parse_config/bad_config.yaml"); + let config = parse_config(path); + println!("{:?}", config); + } }