to contain String

This commit is contained in:
manmen-mi 2024-12-07 22:01:18 +09:00
parent 96816a7b78
commit 2c09341074
2 changed files with 12 additions and 13 deletions

View file

@ -101,8 +101,6 @@ 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 {
@ -187,9 +185,14 @@ fn main() -> Result<ExitCode> {
if let Some(name) = name {
app_state.set_current_exercise_by_name(&name)?;
}
let hint = app_state.current_exercise().hint;
println!("{}", replacer.replace(hint));
if let Some(base_url) = args.base_url {
let replacer = UrlReplacer::new(base_url);
println!("{}", replacer.replace(hint));
} else {
println!("{}", hint);
};
}
// Handled in an earlier match.
Some(Subcommands::Init | Subcommands::Dev(_)) => (),

View file

@ -1,21 +1,17 @@
pub struct UrlReplacer <'a> {
base_url: &'a Option<String>
pub struct UrlReplacer {
base_url: 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 {
impl UrlReplacer {
pub fn new(base_url: 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()
}
hint.replace(EN_BASE_URL, &self.base_url)
}
}