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