Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 13 additions & 34 deletions crates/cli/src/cli/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};

use crate::config::Settings;
use crate::problem::fuzz;
use crate::problem::{
archive, check, compare, create, generate,
run::{RunnableCategory, RunnableFile},
solve, test,
};
use crate::util::{get_lang_from_extension, get_problem_from_cwd, get_project_root};
use crate::problem::run::{RunnableCategory, RunnableFile};
use crate::problem::{archive, check, compare, create, generate, solve, test};
use crate::util::{get_problem_from_cwd, get_project_root};

pub fn cli() -> Command {
Command::new("problem")
Expand Down Expand Up @@ -44,7 +41,7 @@ pub fn cli() -> Command {
Arg::new("file")
.long("file")
.help("Name of the solution file")
.action(ArgAction::Set),
.action(ArgAction::Append),
Arg::new("problem")
.short('p')
.long("problem")
Expand Down Expand Up @@ -77,15 +74,11 @@ pub fn cli() -> Command {
Arg::new("file")
.long("file")
.help("Name of the solution file")
.action(ArgAction::Set),
.action(ArgAction::Append),
Arg::new("generator-file")
.long("generator-file")
.help("Name of the generator file")
.action(ArgAction::Set),
Arg::new("generator-lang")
.long("generator-lang")
.help("Language of the generator file (e.g. cpp, py)")
.action(ArgAction::Set),
Arg::new("problem")
.short('p')
.long("problem")
Expand All @@ -101,10 +94,6 @@ pub fn cli() -> Command {
.long("file")
.help("Name of the generator file")
.action(ArgAction::Set),
Arg::new("lang")
.long("lang")
.help("Language of the generator file (e.g. cpp, py)")
.action(ArgAction::Set),
Arg::new("problem")
.short('p')
.long("problem")
Expand Down Expand Up @@ -199,13 +188,9 @@ pub fn exec(args: &ArgMatches, settings: &Settings) -> Result<()> {

let mut solution_files: Vec<RunnableFile> = Vec::new();
for f in files {
let solution_file = RunnableFile::new(
settings,
RunnableCategory::Solution,
Some(f),
Some(&get_lang_from_extension(f)?),
);
solution_files.push(solution_file);
let solution_file =
RunnableFile::new(settings, RunnableCategory::Solution, Some(f));
solution_files.push(solution_file?);
}

let compare_args = compare::CompareArgs {
Expand Down Expand Up @@ -244,21 +229,16 @@ pub fn exec(args: &ArgMatches, settings: &Settings) -> Result<()> {

let mut solution_files: Vec<RunnableFile> = Vec::new();
for f in files {
let solution_file = RunnableFile::new(
settings,
RunnableCategory::Solution,
Some(f),
Some(&get_lang_from_extension(f)?),
);
solution_files.push(solution_file);
let solution_file =
RunnableFile::new(settings, RunnableCategory::Solution, Some(f));
solution_files.push(solution_file?);
}

let generator = RunnableFile::new(
settings,
RunnableCategory::Generator,
cmd.try_get_one::<String>("generator-file")?,
cmd.try_get_one::<String>("generator-lang")?,
);
)?;

let fuzz_args = fuzz::FuzzArgs {
problems_dir: &problems_dir,
Expand All @@ -279,8 +259,7 @@ pub fn exec(args: &ArgMatches, settings: &Settings) -> Result<()> {
settings,
RunnableCategory::Generator,
cmd.try_get_one::<String>("file")?,
cmd.try_get_one::<String>("lang")?,
);
)?;

let test_name = cmd
.try_get_one::<String>("test-name")?
Expand Down
52 changes: 32 additions & 20 deletions crates/cli/src/problem/run.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use anyhow::{bail, Context, Result};
use normpath::PathExt;
use std::ffi::OsStr;
use std::fmt;
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use anyhow::{bail, Context, Result};
use normpath::PathExt;
use subprocess::{Exec, Redirection};

use crate::config::Settings;
use crate::util::get_lang_from_extension;

/// Represents the category of a runnable file, either a solution or a generator.
#[derive(Eq, PartialEq)]
Expand All @@ -26,38 +28,48 @@ impl fmt::Display for RunnableCategory {
}

/// Represents a runnable file, which can be either a solution or a generator.
/// The file must not be a binary file, and is expected to be a script or a
/// source code file
pub struct RunnableFile {
pub category: RunnableCategory,
pub name: String,
pub lang: String,
category: RunnableCategory,
name: String,
lang: String,
}

impl RunnableFile {
/// Sets the name and language if given, otherwise defaults to the category name
/// and the default language from the settings.
/// Sets the file name if given, and infers the language from the file
/// extension. Otherwise defaults to the category name and the default
/// language from the settings.
pub fn new(
settings: &Settings,
category: RunnableCategory,
name: Option<&String>,
lang: Option<&String>,
) -> Self {
Self {
name: name.cloned().unwrap_or(format!("{category}")),
lang: lang
.cloned()
.unwrap_or(if category == RunnableCategory::Solution {
settings.problem.default_lang.clone()
} else {
settings.problem.default_generator_lang.clone()
}),
category,
) -> Result<Self> {
let lang: String;
let filename: String;
if name.is_none() {
lang = match category {
RunnableCategory::Solution => settings.problem.default_lang.clone(),
RunnableCategory::Generator => settings.problem.default_generator_lang.clone(),
};
filename = format!("{category}.{lang}");
} else {
lang =
get_lang_from_extension(name.context("Failed to get filename of runnable file")?)?;
filename = name.unwrap().to_string();
}

Ok(Self {
name: filename,
lang,
category,
})
}
}

impl fmt::Display for RunnableFile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}s/{}.{}", self.category, self.name, self.lang)
write!(f, "{}s/{}", self.category, self.name)
}
}

Expand Down