Skip to content

Commit 5556d42

Browse files
committed
Use sol_path
1 parent 7d2bc1c commit 5556d42

File tree

5 files changed

+43
-35
lines changed

5 files changed

+43
-35
lines changed

src/app_state.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,10 @@ impl AppState {
321321
.write_solution_to_disk(self.current_exercise_ind, current_exercise.name)
322322
.map(Some)
323323
} else {
324-
let solution_path = if let Some(dir) = current_exercise.dir {
325-
format!("solutions/{dir}/{}.rs", current_exercise.name)
326-
} else {
327-
format!("solutions/{}.rs", current_exercise.name)
328-
};
329-
330-
if Path::new(&solution_path).exists() {
331-
return Ok(Some(solution_path));
324+
let sol_path = current_exercise.sol_path();
325+
326+
if Path::new(&sol_path).exists() {
327+
return Ok(Some(sol_path));
332328
}
333329

334330
Ok(None)

src/cargo_toml.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::{Context, Result};
22
use std::path::Path;
33

4-
use crate::info_file::ExerciseInfo;
4+
use crate::{exercise::RunnableExercise, info_file::ExerciseInfo};
55

66
/// Initial capacity of the bins buffer.
77
pub const BINS_BUFFER_CAPACITY: usize = 1 << 14;

src/exercise.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub struct Exercise {
6868

6969
pub trait RunnableExercise {
7070
fn name(&self) -> &str;
71+
fn dir(&self) -> Option<&str>;
7172
fn strict_clippy(&self) -> bool;
7273
fn test(&self) -> bool;
7374

@@ -145,6 +146,31 @@ pub trait RunnableExercise {
145146

146147
self.run::<true>(&bin_name, output, cmd_runner)
147148
}
149+
150+
fn sol_path(&self) -> String {
151+
let name = self.name();
152+
153+
let mut path = if let Some(dir) = self.dir() {
154+
// 14 = 10 + 1 + 3
155+
// solutions/ + / + .rs
156+
let mut path = String::with_capacity(14 + dir.len() + name.len());
157+
path.push_str("solutions/");
158+
path.push_str(dir);
159+
path.push('/');
160+
path
161+
} else {
162+
// 13 = 10 + 3
163+
// solutions/ + .rs
164+
let mut path = String::with_capacity(13 + name.len());
165+
path.push_str("solutions/");
166+
path
167+
};
168+
169+
path.push_str(name);
170+
path.push_str(".rs");
171+
172+
path
173+
}
148174
}
149175

150176
impl RunnableExercise for Exercise {
@@ -153,6 +179,11 @@ impl RunnableExercise for Exercise {
153179
self.name
154180
}
155181

182+
#[inline]
183+
fn dir(&self) -> Option<&str> {
184+
self.dir
185+
}
186+
156187
#[inline]
157188
fn strict_clippy(&self) -> bool {
158189
self.strict_clippy

src/info_file.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,6 @@ impl ExerciseInfo {
5252

5353
path
5454
}
55-
56-
/// Path to the solution file starting with the `solutions/` directory.
57-
pub fn sol_path(&self) -> String {
58-
let mut path = if let Some(dir) = &self.dir {
59-
// 14 = 10 + 1 + 3
60-
// solutions/ + / + .rs
61-
let mut path = String::with_capacity(14 + dir.len() + self.name.len());
62-
path.push_str("solutions/");
63-
path.push_str(dir);
64-
path.push('/');
65-
path
66-
} else {
67-
// 13 = 10 + 3
68-
// solutions/ + .rs
69-
let mut path = String::with_capacity(13 + self.name.len());
70-
path.push_str("solutions/");
71-
path
72-
};
73-
74-
path.push_str(&self.name);
75-
path.push_str(".rs");
76-
77-
path
78-
}
7955
}
8056

8157
impl RunnableExercise for ExerciseInfo {
@@ -84,6 +60,11 @@ impl RunnableExercise for ExerciseInfo {
8460
&self.name
8561
}
8662

63+
#[inline]
64+
fn dir(&self) -> Option<&str> {
65+
self.dir.as_deref()
66+
}
67+
8768
#[inline]
8869
fn strict_clippy(&self) -> bool {
8970
self.strict_clippy

src/init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::{
1313
};
1414

1515
use crate::{
16-
cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, info_file::InfoFile,
17-
term::press_enter_prompt,
16+
cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, exercise::RunnableExercise,
17+
info_file::InfoFile, term::press_enter_prompt,
1818
};
1919

2020
#[derive(Deserialize)]

0 commit comments

Comments
 (0)