Use sol_path

This commit is contained in:
mo8it 2024-08-28 01:10:19 +02:00
parent 7d2bc1c7a4
commit 5556d42b46
5 changed files with 42 additions and 34 deletions

View file

@ -321,14 +321,10 @@ impl AppState {
.write_solution_to_disk(self.current_exercise_ind, current_exercise.name) .write_solution_to_disk(self.current_exercise_ind, current_exercise.name)
.map(Some) .map(Some)
} else { } else {
let solution_path = if let Some(dir) = current_exercise.dir { let sol_path = current_exercise.sol_path();
format!("solutions/{dir}/{}.rs", current_exercise.name)
} else {
format!("solutions/{}.rs", current_exercise.name)
};
if Path::new(&solution_path).exists() { if Path::new(&sol_path).exists() {
return Ok(Some(solution_path)); return Ok(Some(sol_path));
} }
Ok(None) Ok(None)

View file

@ -1,7 +1,7 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use std::path::Path; use std::path::Path;
use crate::info_file::ExerciseInfo; use crate::{exercise::RunnableExercise, info_file::ExerciseInfo};
/// Initial capacity of the bins buffer. /// Initial capacity of the bins buffer.
pub const BINS_BUFFER_CAPACITY: usize = 1 << 14; pub const BINS_BUFFER_CAPACITY: usize = 1 << 14;

View file

@ -68,6 +68,7 @@ pub struct Exercise {
pub trait RunnableExercise { pub trait RunnableExercise {
fn name(&self) -> &str; fn name(&self) -> &str;
fn dir(&self) -> Option<&str>;
fn strict_clippy(&self) -> bool; fn strict_clippy(&self) -> bool;
fn test(&self) -> bool; fn test(&self) -> bool;
@ -145,6 +146,31 @@ pub trait RunnableExercise {
self.run::<true>(&bin_name, output, cmd_runner) self.run::<true>(&bin_name, output, cmd_runner)
} }
fn sol_path(&self) -> String {
let name = self.name();
let mut path = if let Some(dir) = self.dir() {
// 14 = 10 + 1 + 3
// solutions/ + / + .rs
let mut path = String::with_capacity(14 + dir.len() + name.len());
path.push_str("solutions/");
path.push_str(dir);
path.push('/');
path
} else {
// 13 = 10 + 3
// solutions/ + .rs
let mut path = String::with_capacity(13 + name.len());
path.push_str("solutions/");
path
};
path.push_str(name);
path.push_str(".rs");
path
}
} }
impl RunnableExercise for Exercise { impl RunnableExercise for Exercise {
@ -153,6 +179,11 @@ impl RunnableExercise for Exercise {
self.name self.name
} }
#[inline]
fn dir(&self) -> Option<&str> {
self.dir
}
#[inline] #[inline]
fn strict_clippy(&self) -> bool { fn strict_clippy(&self) -> bool {
self.strict_clippy self.strict_clippy

View file

@ -52,30 +52,6 @@ impl ExerciseInfo {
path path
} }
/// Path to the solution file starting with the `solutions/` directory.
pub fn sol_path(&self) -> String {
let mut path = if let Some(dir) = &self.dir {
// 14 = 10 + 1 + 3
// solutions/ + / + .rs
let mut path = String::with_capacity(14 + dir.len() + self.name.len());
path.push_str("solutions/");
path.push_str(dir);
path.push('/');
path
} else {
// 13 = 10 + 3
// solutions/ + .rs
let mut path = String::with_capacity(13 + self.name.len());
path.push_str("solutions/");
path
};
path.push_str(&self.name);
path.push_str(".rs");
path
}
} }
impl RunnableExercise for ExerciseInfo { impl RunnableExercise for ExerciseInfo {
@ -84,6 +60,11 @@ impl RunnableExercise for ExerciseInfo {
&self.name &self.name
} }
#[inline]
fn dir(&self) -> Option<&str> {
self.dir.as_deref()
}
#[inline] #[inline]
fn strict_clippy(&self) -> bool { fn strict_clippy(&self) -> bool {
self.strict_clippy self.strict_clippy

View file

@ -13,8 +13,8 @@ use std::{
}; };
use crate::{ use crate::{
cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, info_file::InfoFile, cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, exercise::RunnableExercise,
term::press_enter_prompt, info_file::InfoFile, term::press_enter_prompt,
}; };
#[derive(Deserialize)] #[derive(Deserialize)]