diff options
| author | mo8it <mo8it@proton.me> | 2024-08-28 01:10:19 +0200 |
|---|---|---|
| committer | mo8it <mo8it@proton.me> | 2024-08-28 01:10:19 +0200 |
| commit | 5556d42b46e3bfe281343d69da588378c728c089 (patch) | |
| tree | 66583d0250efb3e4532141e457bff2a3b5d9d325 /src | |
| parent | 7d2bc1c7a4333de5460cb86a8dca5e5ecad2a643 (diff) | |
Use sol_path
Diffstat (limited to 'src')
| -rw-r--r-- | src/app_state.rs | 12 | ||||
| -rw-r--r-- | src/cargo_toml.rs | 2 | ||||
| -rw-r--r-- | src/exercise.rs | 31 | ||||
| -rw-r--r-- | src/info_file.rs | 29 | ||||
| -rw-r--r-- | src/init.rs | 4 |
5 files changed, 43 insertions, 35 deletions
diff --git a/src/app_state.rs b/src/app_state.rs index d7de1fd..1000047 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -321,14 +321,10 @@ impl AppState { .write_solution_to_disk(self.current_exercise_ind, current_exercise.name) .map(Some) } else { - let solution_path = if let Some(dir) = current_exercise.dir { - format!("solutions/{dir}/{}.rs", current_exercise.name) - } else { - format!("solutions/{}.rs", current_exercise.name) - }; - - if Path::new(&solution_path).exists() { - return Ok(Some(solution_path)); + let sol_path = current_exercise.sol_path(); + + if Path::new(&sol_path).exists() { + return Ok(Some(sol_path)); } Ok(None) diff --git a/src/cargo_toml.rs b/src/cargo_toml.rs index 445b6b5..8d417ff 100644 --- a/src/cargo_toml.rs +++ b/src/cargo_toml.rs @@ -1,7 +1,7 @@ use anyhow::{Context, Result}; use std::path::Path; -use crate::info_file::ExerciseInfo; +use crate::{exercise::RunnableExercise, info_file::ExerciseInfo}; /// Initial capacity of the bins buffer. pub const BINS_BUFFER_CAPACITY: usize = 1 << 14; diff --git a/src/exercise.rs b/src/exercise.rs index ea15465..11eea63 100644 --- a/src/exercise.rs +++ b/src/exercise.rs @@ -68,6 +68,7 @@ pub struct Exercise { pub trait RunnableExercise { fn name(&self) -> &str; + fn dir(&self) -> Option<&str>; fn strict_clippy(&self) -> bool; fn test(&self) -> bool; @@ -145,6 +146,31 @@ pub trait RunnableExercise { 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 { @@ -154,6 +180,11 @@ impl RunnableExercise for Exercise { } #[inline] + fn dir(&self) -> Option<&str> { + self.dir + } + + #[inline] fn strict_clippy(&self) -> bool { self.strict_clippy } diff --git a/src/info_file.rs b/src/info_file.rs index d4e4611..fdc8f0f 100644 --- a/src/info_file.rs +++ b/src/info_file.rs @@ -52,30 +52,6 @@ impl ExerciseInfo { 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 { @@ -85,6 +61,11 @@ impl RunnableExercise for ExerciseInfo { } #[inline] + fn dir(&self) -> Option<&str> { + self.dir.as_deref() + } + + #[inline] fn strict_clippy(&self) -> bool { self.strict_clippy } diff --git a/src/init.rs b/src/init.rs index aecb2d8..332bf52 100644 --- a/src/init.rs +++ b/src/init.rs @@ -13,8 +13,8 @@ use std::{ }; use crate::{ - cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, info_file::InfoFile, - term::press_enter_prompt, + cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, exercise::RunnableExercise, + info_file::InfoFile, term::press_enter_prompt, }; #[derive(Deserialize)] |
