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
11 changes: 9 additions & 2 deletions crates/cli/src/cli/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use clap::{Arg, ArgAction, ArgMatches, Command};

use crate::comp::{add, create, finish, list, remove, rename, solve, test};
use crate::config::get_settings;
use crate::problem::run::{RunnableCategory, RunnableFile};
use crate::util::get_project_root;

pub fn cli() -> Command {
Expand Down Expand Up @@ -189,15 +190,21 @@ pub fn exec(args: &ArgMatches) -> Result<()> {
.context("Competition name is required")?;
let solution_lang = cmd.try_get_one::<String>("lang")?;

solve::solve(&settings, &problems_dir, comp_name, solution_lang)?;
let solution_file =
RunnableFile::new(&settings, RunnableCategory::Solution, None, solution_lang)?;

solve::solve(&settings, &problems_dir, comp_name, solution_file)?;
}
Some(("test", cmd)) => {
let comp_name = cmd
.try_get_one::<String>("comp")?
.context("Competition name is required")?;
let solution_lang = cmd.try_get_one::<String>("lang")?;

test::test(&settings, &problems_dir, comp_name, solution_lang)?;
let solution_file =
RunnableFile::new(&settings, RunnableCategory::Solution, None, solution_lang)?;

test::test(&settings, &problems_dir, comp_name, solution_file)?;
}
_ => {}
}
Expand Down
24 changes: 14 additions & 10 deletions crates/cli/src/cli/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn exec(args: &ArgMatches) -> Result<()> {
let mut solution_files: Vec<RunnableFile> = Vec::new();
for f in files {
let solution_file =
RunnableFile::new(&settings, RunnableCategory::Solution, Some(f));
RunnableFile::new(&settings, RunnableCategory::Solution, Some(f), None);
solution_files.push(solution_file?);
}

Expand Down Expand Up @@ -231,14 +231,15 @@ pub fn exec(args: &ArgMatches) -> Result<()> {
let mut solution_files: Vec<RunnableFile> = Vec::new();
for f in files {
let solution_file =
RunnableFile::new(&settings, RunnableCategory::Solution, Some(f));
RunnableFile::new(&settings, RunnableCategory::Solution, Some(f), None);
solution_files.push(solution_file?);
}

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

let fuzz_args = fuzz::FuzzArgs {
Expand All @@ -260,6 +261,7 @@ pub fn exec(args: &ArgMatches) -> Result<()> {
&settings,
RunnableCategory::Generator,
cmd.try_get_one::<String>("file")?,
None,
)?;

let test_name = cmd
Expand All @@ -281,33 +283,35 @@ pub fn exec(args: &ArgMatches) -> Result<()> {
None => &get_problem_from_cwd(&problems_dir)?,
};

let solution_file = cmd.try_get_one::<String>("file")?.map(|f| f.as_str());
let solution_file = cmd.try_get_one::<String>("file")?;
let solution_lang = cmd.try_get_one::<String>("lang")?;

solve::solve(
let solution_file = RunnableFile::new(
&settings,
&problems_dir,
problem_name,
RunnableCategory::Solution,
solution_file,
solution_lang,
)?;

solve::solve(&settings, &problems_dir, problem_name, &solution_file)?;
}
Some(("test", cmd)) => {
let problem_name = match cmd.try_get_one::<String>("problem")? {
Some(name) => name,
None => &get_problem_from_cwd(&problems_dir)?,
};

let solution_file = cmd.try_get_one::<String>("file")?.map(|f| f.as_str());
let solution_file = cmd.try_get_one::<String>("file")?;
let solution_lang = cmd.try_get_one::<String>("lang")?;

test::test(
let solution_file = RunnableFile::new(
&settings,
&problems_dir,
problem_name,
RunnableCategory::Solution,
solution_file,
solution_lang,
)?;

test::test(&settings, &problems_dir, problem_name, &solution_file)?;
}
_ => {}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/src/comp/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::{bail, Context, Result};
use serde_json::from_reader;

use crate::config::Settings;
use crate::problem::run::RunnableFile;
use crate::problem::solve::solve as problem_solve;

use super::{Competitions, COMPETITIONS_FILE};
Expand All @@ -13,7 +14,7 @@ pub fn solve(
settings: &Settings,
problems_dir: &Path,
comp_name: &str,
solution_lang: Option<&String>,
solution_file: RunnableFile,
) -> Result<()> {
let comp_file_path = problems_dir.join(COMPETITIONS_FILE);
if !fs::exists(&comp_file_path)? {
Expand All @@ -34,8 +35,7 @@ pub fn solve(
settings,
problems_dir,
problem_name.as_str(),
None,
solution_lang,
&solution_file,
)?;
}

Expand Down
6 changes: 3 additions & 3 deletions crates/cli/src/comp/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::{bail, Context, Result};
use serde_json::from_reader;

use crate::config::Settings;
use crate::problem::run::RunnableFile;
use crate::problem::test::test as problem_test;

use super::{Competitions, COMPETITIONS_FILE};
Expand All @@ -13,7 +14,7 @@ pub fn test(
settings: &Settings,
problems_dir: &Path,
comp_name: &str,
solution_lang: Option<&String>,
solution_file: RunnableFile,
) -> Result<()> {
let comp_file_path = problems_dir.join(COMPETITIONS_FILE);
if !fs::exists(&comp_file_path)? {
Expand All @@ -34,8 +35,7 @@ pub fn test(
settings,
problems_dir,
problem_name.as_str(),
None,
solution_lang,
&solution_file,
)?;
}

Expand Down
50 changes: 35 additions & 15 deletions crates/cli/src/problem/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,46 @@ pub struct RunnableFile {

impl RunnableFile {
/// 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.
/// extension. If the language is provided but not the file name, the
/// default language file is used for that category.
///
/// If neither is provided, it defaults to the category name
/// and the default language from the settings.
pub fn new(
settings: &Settings,
category: RunnableCategory,
name: Option<&String>,
language: Option<&String>,
) -> 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();
}
let (filename, lang) = match (name, language) {
(Some(name), Some(lang)) => {
let file_lang = get_lang_from_extension(name)
.context("Failed to get language from file extension")?;
if file_lang != *lang {
bail!(
"Language from file extension ({file_lang}) does not match provided language ({lang})"
);
}
(name.to_string(), lang.to_string())
}
(Some(name), None) => {
let lang = get_lang_from_extension(name)
.context("Failed to get language from file extension")?;
(name.to_string(), lang)
}
(None, Some(lang)) => {
let filename = format!("{category}.{lang}");
(filename, lang.to_string())
}
(None, None) => {
let lang = match category {
RunnableCategory::Solution => settings.problem.default_lang.clone(),
RunnableCategory::Generator => settings.problem.default_generator_lang.clone(),
};
let filename = format!("{category}.{lang}");
(filename, lang)
}
};

Ok(Self {
name: filename,
Expand Down
22 changes: 4 additions & 18 deletions crates/cli/src/problem/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::Path;
use anyhow::{Context, Result};

use super::sync_mappings::get_problem;
use crate::problem::run::{RunCommand, RunnableCategory, RunnableFile};
use crate::problem::run::{RunCommand, RunnableFile};
use crate::util::get_project_root;
use crate::{config::Settings, util::get_input_files_in_directory};

Expand All @@ -14,31 +14,17 @@ pub fn solve(
settings: &Settings,
problems_dir: &Path,
problem_name: &str,
solution_file_name: Option<&str>,
solution_lang: Option<&String>,
solution_file: &RunnableFile,
) -> Result<()> {
let project_root = get_project_root()?;
let problem_path = project_root.join(get_problem(problems_dir, problem_name)?);

let solution_lang = solution_lang.unwrap_or(&settings.problem.default_lang);
let mut solution_file = format!("solution.{solution_lang}");

// Use custom solution file or script file if it exists
if solution_file_name.is_some() {
solution_file = solution_file_name
.context("Failed to get solution file name")?
.to_string();
}

let runnable_file =
RunnableFile::new(settings, RunnableCategory::Solution, Some(&solution_file))?;

let run_command = RunCommand::new(
settings,
&problem_path,
&runnable_file,
solution_file,
problem_path.join("solutions/solution.out"),
problem_path.join(&solution_file),
problem_path.join(format!("{solution_file}")),
)?;

let test_files = get_input_files_in_directory(problem_path.join("tests"))?;
Expand Down
22 changes: 4 additions & 18 deletions crates/cli/src/problem/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::time::Duration;
use anyhow::{Context, Result};

use crate::config::Settings;
use crate::problem::run::{RunCommand, RunnableCategory, RunnableFile};
use crate::problem::run::{RunCommand, RunnableFile};
use crate::util::{get_input_files_in_directory, get_project_root};

use super::sync_mappings::get_problem;
Expand All @@ -16,31 +16,17 @@ pub fn test(
settings: &Settings,
problems_dir: &Path,
problem_name: &str,
solution_file_name: Option<&str>,
solution_lang: Option<&String>,
solution_file: &RunnableFile,
) -> Result<()> {
let project_root = get_project_root()?;
let problem_path = project_root.join(get_problem(problems_dir, problem_name)?);

let solution_lang = solution_lang.unwrap_or(&settings.problem.default_lang);
let mut solution_file = format!("solution.{solution_lang}");

// Use custom solution file or script file if it exists
if solution_file_name.is_some() {
solution_file = solution_file_name
.context("Failed to get solution file name")?
.to_string();
}

let runnable_file =
RunnableFile::new(settings, RunnableCategory::Solution, Some(&solution_file))?;

let run_command = RunCommand::new(
settings,
&problem_path,
&runnable_file,
solution_file,
problem_path.join("solutions/solution.out"),
problem_path.join(&solution_file),
problem_path.join(format!("{solution_file}")),
)?;

let test_files = get_input_files_in_directory(problem_path.join("tests"))?;
Expand Down