1
0
Fork 0
mirror of https://github.com/notohh/rustlings.git synced 2024-12-18 15:08:08 -05:00

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)
.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)

View file

@ -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;

View file

@ -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 {
@ -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

View file

@ -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

View file

@ -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)]