1use std::{
2 fs::read_to_string,
3 path::{Path, PathBuf},
4};
5
6use anyhow::Context;
7use itertools::Itertools;
8use serde::Deserialize;
9
10#[derive(Debug, Clone, Deserialize)]
11pub struct ThreadConfig {
12 pub thread_num: Option<usize>,
15}
16
17#[derive(Debug, Clone, Deserialize)]
18pub struct PathConfig {
19 pub seed_file: std::path::PathBuf,
21
22 pub input_dir: std::path::PathBuf,
24
25 pub output_dir: std::path::PathBuf,
27
28 pub evaluation_record: std::path::PathBuf,
30}
31
32#[derive(Debug, Clone, Deserialize)]
33pub struct Build {
34 pub submission: Vec<String>,
36
37 pub tester: Vec<String>,
39}
40
41#[derive(Debug, Clone, Deserialize)]
42pub struct Execute {
43 pub submission: Vec<String>,
45
46 pub tester: Vec<String>,
53
54 pub integrated: bool,
56}
57
58#[derive(Debug, Clone, Deserialize)]
59pub struct CommandConfig {
60 pub build: Build,
62
63 pub execute: Execute,
65}
66
67#[derive(Debug, Clone, Deserialize)]
68pub struct Config {
69 pub thread: ThreadConfig,
71
72 pub path: PathConfig,
74
75 pub command: CommandConfig,
77}
78
79impl Config {
80 pub fn read_from_file<P>(config_file_path: P) -> anyhow::Result<Self>
82 where
83 P: AsRef<Path>,
84 {
85 let config_str = read_to_string(config_file_path)
86 .with_context(|| "Failed to read configuration file.")?;
87 toml::from_str(&config_str).with_context(|| "Failed to deserialize configuration file.")
88 }
89
90 pub fn input_file_path(&self, seed: usize) -> PathBuf {
92 self.path.input_dir.join(format!("{:04}.txt", seed))
93 }
94
95 pub fn output_file_path(&self, seed: usize) -> PathBuf {
97 self.path.output_dir.join(format!("{:04}.txt", seed))
98 }
99
100 pub fn cmd_args_for_execute_tester(&self, seed: usize) -> Vec<String> {
102 self.command
103 .execute
104 .tester
105 .iter()
106 .map(|arg| match arg.as_str() {
107 "{input}" => self.input_file_path(seed).to_str().unwrap().to_owned(),
108 "{output}" => self.output_file_path(seed).to_str().unwrap().to_owned(),
109 "{cmd}" => self.command.execute.submission.iter().join(" "),
110 _ => arg.clone(),
111 })
112 .collect()
113 }
114}