Added Configuration parsing module
parent
9f509a929f
commit
bb968d770e
|
@ -209,6 +209,7 @@ name = "tester"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"serde",
|
||||||
"serde_yaml",
|
"serde_yaml",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -7,4 +7,5 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.4.16", features = ["derive"] }
|
clap = { version = "4.4.16", features = ["derive"] }
|
||||||
|
serde = { version = "1.0.195", features = ["derive"] }
|
||||||
serde_yaml = "0.9.30"
|
serde_yaml = "0.9.30"
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
mod config;
|
pub mod config;
|
||||||
|
|
|
@ -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)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct Config {
|
pub struct Config {
|
||||||
executable_paths: HashMap<String, PathBuf>,
|
tested_executables: Vec<Team>,
|
||||||
|
|
||||||
input_path: PathBuf,
|
input_path: PathBuf,
|
||||||
output_path: PathBuf,
|
output_path: PathBuf,
|
||||||
|
in_stream_path: PathBuf,
|
||||||
|
|
||||||
|
runtimes: Option<Vec<Team>>,
|
||||||
|
|
||||||
toolchains: Vec<Toolchain>,
|
toolchains: Vec<Toolchain>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct Toolchain {
|
pub struct Team {
|
||||||
name: String,
|
name: String,
|
||||||
steps: Vec<Step>,
|
executable: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct Step {
|
pub struct Toolchain {
|
||||||
|
name: String,
|
||||||
|
steps: Vec<Step>,}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct Step {
|
||||||
name: String,
|
name: String,
|
||||||
|
|
||||||
executable_path: Option<PathBuf>, // if None then we use the current executable path
|
executable_path: Option<PathBuf>, // if None then we use the current executable path
|
||||||
arguments: Vec<String>, // special string $INPUT corresponds to previous step output
|
arguments: Vec<String>, // special string $INPUT corresponds to previous step output
|
||||||
output: String, // the output file name
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue