mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-03-30 19:19:19 +00:00
Keep exercise path owned
This commit is contained in:
parent
0cbcb8964c
commit
2512701e2f
@ -83,12 +83,6 @@ impl AppState {
|
||||
let mut exercises = exercise_infos
|
||||
.into_iter()
|
||||
.map(|exercise_info| {
|
||||
// Leaking to be able to borrow in the watch mode `Table`.
|
||||
// Leaking is not a problem because the `AppState` instance lives until
|
||||
// the end of the program.
|
||||
let path = exercise_info.path().leak();
|
||||
let hint = exercise_info.hint.trim_ascii();
|
||||
|
||||
let canonical_path = dir_canonical_path.as_deref().map(|dir_canonical_path| {
|
||||
let mut canonical_path;
|
||||
if let Some(dir) = exercise_info.dir {
|
||||
@ -114,11 +108,11 @@ impl AppState {
|
||||
Exercise {
|
||||
dir: exercise_info.dir,
|
||||
name: exercise_info.name,
|
||||
path,
|
||||
path: exercise_info.path(),
|
||||
canonical_path,
|
||||
test: exercise_info.test,
|
||||
strict_clippy: exercise_info.strict_clippy,
|
||||
hint,
|
||||
hint: exercise_info.hint.trim_ascii(),
|
||||
// Updated below.
|
||||
done: false,
|
||||
}
|
||||
@ -342,12 +336,12 @@ impl AppState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn reset_current_exercise(&mut self) -> Result<&'static str> {
|
||||
pub fn reset_current_exercise(&mut self) -> Result<&str> {
|
||||
self.set_pending(self.current_exercise_ind)?;
|
||||
let exercise = self.current_exercise();
|
||||
self.reset(self.current_exercise_ind, exercise.path)?;
|
||||
self.reset(self.current_exercise_ind, &exercise.path)?;
|
||||
|
||||
Ok(exercise.path)
|
||||
Ok(&exercise.path)
|
||||
}
|
||||
|
||||
// Reset the exercise by index and return its name.
|
||||
@ -358,7 +352,7 @@ impl AppState {
|
||||
|
||||
self.set_pending(exercise_ind)?;
|
||||
let exercise = &self.exercises[exercise_ind];
|
||||
self.reset(exercise_ind, exercise.path)?;
|
||||
self.reset(exercise_ind, &exercise.path)?;
|
||||
|
||||
Ok(exercise.name)
|
||||
}
|
||||
@ -600,7 +594,7 @@ mod tests {
|
||||
Exercise {
|
||||
dir: None,
|
||||
name: "0",
|
||||
path: "exercises/0.rs",
|
||||
path: String::from("exercises/0.rs"),
|
||||
canonical_path: None,
|
||||
test: false,
|
||||
strict_clippy: false,
|
||||
|
||||
@ -69,7 +69,7 @@ pub struct Exercise {
|
||||
pub dir: Option<&'static str>,
|
||||
pub name: &'static str,
|
||||
/// Path of the exercise file starting with the `exercises/` directory.
|
||||
pub path: &'static str,
|
||||
pub path: String,
|
||||
pub canonical_path: Option<String>,
|
||||
pub test: bool,
|
||||
pub strict_clippy: bool,
|
||||
@ -85,9 +85,9 @@ impl Exercise {
|
||||
) -> io::Result<()> {
|
||||
file_path(writer, Color::Blue, |writer| {
|
||||
if emit_file_links && let Some(canonical_path) = self.canonical_path.as_deref() {
|
||||
terminal_file_link(writer, self.path, canonical_path)
|
||||
terminal_file_link(writer, &self.path, canonical_path)
|
||||
} else {
|
||||
writer.write_str(self.path)
|
||||
writer.write_str(&self.path)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -95,7 +95,7 @@ impl InfoFile {
|
||||
pub fn parse() -> Result<Self> {
|
||||
// Read a local `info.toml` if it exists.
|
||||
let slf = match fs::read_to_string("info.toml") {
|
||||
// Leaking is fine since `InfoFile` is used until the end of the program.
|
||||
// Leaking is fine since the info file is used until the end of the program.
|
||||
Ok(file_content) => toml::de::from_str::<Self>(file_content.leak())
|
||||
.context("Failed to parse the `info.toml` file")?,
|
||||
Err(e) => {
|
||||
|
||||
@ -366,11 +366,11 @@ impl<'a> ListState<'a> {
|
||||
|
||||
let exercise_ind = self.selected_to_exercise_ind(selected)?;
|
||||
let exercise_name = self.app_state.reset_exercise_by_ind(exercise_ind)?;
|
||||
self.update_rows();
|
||||
write!(
|
||||
self.message,
|
||||
"The exercise `{exercise_name}` has been reset",
|
||||
)?;
|
||||
self.update_rows();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user