try subcommand

This commit is contained in:
manmen-mi 2024-12-07 21:40:18 +09:00
parent d49f192835
commit 96816a7b78
2 changed files with 28 additions and 2 deletions

View file

@ -8,7 +8,7 @@ use std::{
};
use term::{clear_terminal, press_enter_prompt};
use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile};
use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile, url_replacer::UrlReplacer};
mod app_state;
mod cargo_toml;
@ -22,6 +22,7 @@ mod list;
mod run;
mod term;
mod watch;
mod url_replacer;
const CURRENT_FORMAT_VERSION: u8 = 1;
@ -100,6 +101,8 @@ fn main() -> Result<ExitCode> {
info_file.final_message.unwrap_or_default(),
)?;
let replacer = UrlReplacer::new(&args.base_url);
// Show the welcome message if the state file doesn't exist yet.
if let Some(welcome_message) = info_file.welcome_message {
match state_file_status {
@ -184,7 +187,9 @@ fn main() -> Result<ExitCode> {
if let Some(name) = name {
app_state.set_current_exercise_by_name(&name)?;
}
println!("{}", app_state.current_exercise().hint);
let hint = app_state.current_exercise().hint;
println!("{}", replacer.replace(hint));
}
// Handled in an earlier match.
Some(Subcommands::Init | Subcommands::Dev(_)) => (),

21
src/url_replacer.rs Normal file
View file

@ -0,0 +1,21 @@
pub struct UrlReplacer <'a> {
base_url: &'a Option<String>
}
const EN_BASE_URL: &str = "https://doc.rust-lang.org/book";
impl <'a> UrlReplacer <'a> {
pub fn new(base_url: &'a Option<String>) -> Self {
Self {
base_url
}
}
pub fn replace(&self, hint: &str) -> String {
if let Some(base_url) = self.base_url {
hint.replace(EN_BASE_URL, base_url)
} else {
hint.to_owned()
}
}
}