#![doc = include_str!("../README.md")]
pub mod types;
use types::{Config, Domain};
pub fn parse_string(config: &Config, text: String) -> String {
if !text.is_empty() {
text.split_ascii_whitespace()
.map(|word| {
for domain in config.domains.iter() {
if let Some(mut url) = domain.contain(word, true) {
if let Some(new_host) = domain.new.host_str() {
url.set_host(Some(new_host)).unwrap();
return url.as_str().to_string();
}
}
}
word.to_string()
})
.collect::<Vec<String>>()
.join(" ")
} else {
text
}
}
pub fn extract_old_domains(config: &Config, text: String) -> Vec<&Domain> {
text.split_ascii_whitespace()
.filter_map(|word| config.contain(word, true))
.collect()
}
#[cfg(test)]
mod tests {
use crate::{parse_string, types::Config};
#[test]
fn parse_string_test() {
let config: Config = Config::default();
assert_eq!(parse_string(&config, String::new()), String::new());
assert_eq!(
parse_string(&config, "Hello, world".to_owned()),
"Hello, world".to_owned()
);
assert_eq!(
parse_string(&config, "Hello https://randomdooomain.com".to_owned()),
"Hello https://randomdooomain.com".to_owned()
);
assert_eq!(
parse_string(&config, "hi, youtube.com/something".to_owned()),
"hi, https://piped.kavin.rocks/something".to_owned()
);
assert_eq!(
parse_string(&config, "hi, youtube.com".to_owned()),
"hi, https://piped.kavin.rocks/".to_owned()
)
}
}