#[cfg(feature = "json")]
use super::ToFromJson;
use crate::types::Domain;
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Config {
pub domains: Vec<Domain>,
}
impl Config {
pub fn new(domains: Vec<Domain>) -> Self {
Self { domains }
}
pub fn old_hosts(&self) -> Vec<&str> {
self.domains
.iter()
.filter_map(|domain| domain.old.host_str())
.collect()
}
pub fn new_hosts(&self) -> Vec<&str> {
self.domains
.iter()
.filter_map(|domain| domain.new.host_str())
.collect()
}
pub fn get_by_old(&self, old_host: &str) -> Option<&Domain> {
self.domains.iter().find(|domain| {
if let Some(host) = domain.old.host_str() {
host == old_host
} else {
false
}
})
}
pub fn contain(&self, word: &str, just_old: bool) -> Option<&Domain> {
self.domains
.iter()
.find(|domain| domain.contain(word, just_old).is_some())
}
}
#[cfg(feature = "json")]
impl ToFromJson<'_> for Config {}
impl Default for Config {
fn default() -> Self {
Self::new(vec![
Domain::try_from(("https://youtube.com/", "https://piped.kavin.rocks/")).unwrap(),
Domain::try_from(("https://www.youtube.com/", "https://piped.kavin.rocks/")).unwrap(),
Domain::try_from(("https://youtu.be/", "https://piped.kavin.rocks/")).unwrap(),
Domain::try_from(("https://t.co/", "https://nitter.net/")).unwrap(),
Domain::try_from(("https://twitter.com/", "https://nitter.net/")).unwrap(),
Domain::try_from(("https://reddit.com/", "https://libredd.it/")).unwrap(),
])
}
}