Added Configuration parsing module

pull/3/head
Ayrton Chilibeck 2024-01-12 21:34:45 -07:00
parent 9f509a929f
commit bb968d770e
Signed by: ayrton
GPG Key ID: BAE24A03DCBF160D
4 changed files with 66 additions and 12 deletions

1
Cargo.lock generated
View File

@ -209,6 +209,7 @@ name = "tester"
version = "0.1.0"
dependencies = [
"clap",
"serde",
"serde_yaml",
]

View File

@ -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"

View File

@ -1 +1 @@
mod config;
pub mod config;

View File

@ -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<String, PathBuf>,
#[derive(Debug, Deserialize)]
pub struct Config {
tested_executables: Vec<Team>,
input_path: PathBuf,
output_path: PathBuf,
in_stream_path: PathBuf,
runtimes: Option<Vec<Team>>,
toolchains: Vec<Toolchain>,
}
#[derive(Default)]
struct Toolchain {
#[derive(Debug, Deserialize)]
pub struct Team {
name: String,
steps: Vec<Step>,
executable: PathBuf,
}
#[derive(Default)]
struct Step {
#[derive(Debug, Deserialize)]
pub struct Toolchain {
name: String,
steps: Vec<Step>,}
#[derive(Debug, Deserialize)]
pub struct Step {
name: String,
executable_path: Option<PathBuf>, // if None then we use the current executable path
arguments: Vec<String>, // special string $INPUT corresponds to previous step output
output: String, // the output file name
uses_runtime: Option<bool>,
uses_in_stream: Option<bool>,
allow_error: Option<bool>
}
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);
}
}