Skip to content

Commit 2512701

Browse files
committed
Keep exercise path owned
1 parent 0cbcb89 commit 2512701

4 files changed

Lines changed: 12 additions & 18 deletions

File tree

src/app_state.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ impl AppState {
8383
let mut exercises = exercise_infos
8484
.into_iter()
8585
.map(|exercise_info| {
86-
// Leaking to be able to borrow in the watch mode `Table`.
87-
// Leaking is not a problem because the `AppState` instance lives until
88-
// the end of the program.
89-
let path = exercise_info.path().leak();
90-
let hint = exercise_info.hint.trim_ascii();
91-
9286
let canonical_path = dir_canonical_path.as_deref().map(|dir_canonical_path| {
9387
let mut canonical_path;
9488
if let Some(dir) = exercise_info.dir {
@@ -114,11 +108,11 @@ impl AppState {
114108
Exercise {
115109
dir: exercise_info.dir,
116110
name: exercise_info.name,
117-
path,
111+
path: exercise_info.path(),
118112
canonical_path,
119113
test: exercise_info.test,
120114
strict_clippy: exercise_info.strict_clippy,
121-
hint,
115+
hint: exercise_info.hint.trim_ascii(),
122116
// Updated below.
123117
done: false,
124118
}
@@ -342,12 +336,12 @@ impl AppState {
342336
Ok(())
343337
}
344338

345-
pub fn reset_current_exercise(&mut self) -> Result<&'static str> {
339+
pub fn reset_current_exercise(&mut self) -> Result<&str> {
346340
self.set_pending(self.current_exercise_ind)?;
347341
let exercise = self.current_exercise();
348-
self.reset(self.current_exercise_ind, exercise.path)?;
342+
self.reset(self.current_exercise_ind, &exercise.path)?;
349343

350-
Ok(exercise.path)
344+
Ok(&exercise.path)
351345
}
352346

353347
// Reset the exercise by index and return its name.
@@ -358,7 +352,7 @@ impl AppState {
358352

359353
self.set_pending(exercise_ind)?;
360354
let exercise = &self.exercises[exercise_ind];
361-
self.reset(exercise_ind, exercise.path)?;
355+
self.reset(exercise_ind, &exercise.path)?;
362356

363357
Ok(exercise.name)
364358
}
@@ -600,7 +594,7 @@ mod tests {
600594
Exercise {
601595
dir: None,
602596
name: "0",
603-
path: "exercises/0.rs",
597+
path: String::from("exercises/0.rs"),
604598
canonical_path: None,
605599
test: false,
606600
strict_clippy: false,

src/exercise.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub struct Exercise {
6969
pub dir: Option<&'static str>,
7070
pub name: &'static str,
7171
/// Path of the exercise file starting with the `exercises/` directory.
72-
pub path: &'static str,
72+
pub path: String,
7373
pub canonical_path: Option<String>,
7474
pub test: bool,
7575
pub strict_clippy: bool,
@@ -85,9 +85,9 @@ impl Exercise {
8585
) -> io::Result<()> {
8686
file_path(writer, Color::Blue, |writer| {
8787
if emit_file_links && let Some(canonical_path) = self.canonical_path.as_deref() {
88-
terminal_file_link(writer, self.path, canonical_path)
88+
terminal_file_link(writer, &self.path, canonical_path)
8989
} else {
90-
writer.write_str(self.path)
90+
writer.write_str(&self.path)
9191
}
9292
})
9393
}

src/info_file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl InfoFile {
9595
pub fn parse() -> Result<Self> {
9696
// Read a local `info.toml` if it exists.
9797
let slf = match fs::read_to_string("info.toml") {
98-
// Leaking is fine since `InfoFile` is used until the end of the program.
98+
// Leaking is fine since the info file is used until the end of the program.
9999
Ok(file_content) => toml::de::from_str::<Self>(file_content.leak())
100100
.context("Failed to parse the `info.toml` file")?,
101101
Err(e) => {

src/list/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,11 @@ impl<'a> ListState<'a> {
366366

367367
let exercise_ind = self.selected_to_exercise_ind(selected)?;
368368
let exercise_name = self.app_state.reset_exercise_by_ind(exercise_ind)?;
369-
self.update_rows();
370369
write!(
371370
self.message,
372371
"The exercise `{exercise_name}` has been reset",
373372
)?;
373+
self.update_rows();
374374

375375
Ok(())
376376
}

0 commit comments

Comments
 (0)