rustlings/src/url_replacer.rs

55 lines
1.2 KiB
Rust
Raw Normal View History

2024-12-07 13:01:18 +00:00
pub struct UrlReplacer {
base_url: String
2024-12-07 12:40:18 +00:00
}
const EN_BASE_URL: &str = "https://doc.rust-lang.org/book";
2024-12-07 13:01:18 +00:00
impl UrlReplacer {
pub fn new(base_url: String) -> Self {
2024-12-07 12:40:18 +00:00
Self {
base_url
}
}
pub fn replace(&self, hint: &str) -> String {
2024-12-07 13:01:18 +00:00
hint.replace(EN_BASE_URL, &self.base_url)
2024-12-07 12:40:18 +00:00
}
}
2024-12-08 02:12:43 +00:00
#[cfg(test)]
mod test {
use super::*;
const TEST_DOMAIN: &str = "https://doc.rust-kr.org";
#[test]
fn non_url() {
let replacer = UrlReplacer {
base_url: String::from(TEST_DOMAIN)
};
let hint = "\
hints (...) lines (...)
link: https://doc.rust-lang.org/book/ch03-02-data-types.html";
assert_eq!("\
hints (...) lines (...)
link: https://doc.rust-kr.org/ch03-02-data-types.html", replacer.replace(hint));
}
#[test]
fn replace_url() {
let replacer = UrlReplacer {
base_url: String::from(TEST_DOMAIN)
};
let hint = "\
hints (...) lines (...)
link: https://doc.rust-lang.org/book/ch03-02-data-types.html";
assert_eq!("\
hints (...) lines (...)
link: https://doc.rust-kr.org/ch03-02-data-types.html", replacer.replace(hint));
}
}