trim_end_with_slash

This commit is contained in:
manmen-mi 2024-12-08 11:19:51 +09:00
parent 1ffe7fc837
commit 5cd711e909

View file

@ -5,7 +5,13 @@ pub struct UrlReplacer {
const EN_BASE_URL: &str = "https://doc.rust-lang.org/book"; const EN_BASE_URL: &str = "https://doc.rust-lang.org/book";
impl UrlReplacer { impl UrlReplacer {
pub fn new(base_url: String) -> Self { pub fn new(mut base_url: String) -> Self {
base_url = if base_url.ends_with('/') {
base_url.trim_end_matches('/').to_owned()
} else {
base_url
};
Self { Self {
base_url base_url
} }
@ -24,9 +30,7 @@ mod test {
#[test] #[test]
fn non_rustbook_url() { fn non_rustbook_url() {
let replacer = UrlReplacer { let replacer = UrlReplacer::new(String::from(TEST_DOMAIN));
base_url: String::from(TEST_DOMAIN)
};
let hint = "\ let hint = "\
hints (...) lines (...) hints (...) lines (...)
@ -38,9 +42,7 @@ link: https://example.com/ch03-02-data-types.html";
#[test] #[test]
fn replace_rustbook_url() { fn replace_rustbook_url() {
let replacer = UrlReplacer { let replacer = UrlReplacer::new(String::from(TEST_DOMAIN));
base_url: String::from(TEST_DOMAIN)
};
let hint = "\ let hint = "\
hints (...) lines (...) hints (...) lines (...)
@ -50,4 +52,11 @@ link: https://doc.rust-lang.org/book/ch03-02-data-types.html";
hints (...) lines (...) hints (...) lines (...)
link: https://doc.rust-kr.org/ch03-02-data-types.html", replacer.replace(hint)); link: https://doc.rust-kr.org/ch03-02-data-types.html", replacer.replace(hint));
} }
#[test]
fn trim_end_with_slash() {
let replacer = UrlReplacer::new(String::from(TEST_DOMAIN));
assert_eq!(TEST_DOMAIN, replacer.base_url);
}
} }