From 5556d42b46e3bfe281343d69da588378c728c089 Mon Sep 17 00:00:00 2001 From: mo8it Date: Wed, 28 Aug 2024 01:10:19 +0200 Subject: [PATCH] Use sol_path --- src/app_state.rs | 10 +++------- src/cargo_toml.rs | 2 +- src/exercise.rs | 31 +++++++++++++++++++++++++++++++ src/info_file.rs | 29 +++++------------------------ src/init.rs | 4 ++-- 5 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/app_state.rs b/src/app_state.rs index d7de1fdb..1000047d 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) - }; + let sol_path = current_exercise.sol_path(); - if Path::new(&solution_path).exists() { - return Ok(Some(solution_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 445b6b57..8d417ffa 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 ea15465c..11eea638 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::(&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 { @@ -153,6 +179,11 @@ impl RunnableExercise for Exercise { self.name } + #[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 d4e46110..fdc8f0f3 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 { @@ -84,6 +60,11 @@ impl RunnableExercise for ExerciseInfo { &self.name } + #[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 aecb2d8c..332bf52e 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)]