use std::sync::Arc;
#[derive(Clone)]
pub enum OriginMatcher {
Exact(String),
Suffix(String),
Custom(Arc<dyn Fn(&str) -> bool + Send + Sync + 'static>),
}
impl OriginMatcher {
pub(crate) fn matches(&self, origin: &str) -> bool {
match self {
Self::Exact(s) => s == origin,
Self::Suffix(s) => {
let host = url::Url::parse(origin)
.ok()
.and_then(|u| u.host_str().map(str::to_owned))
.unwrap_or_default();
if host.is_empty() {
return false;
}
host == *s.as_str() || host.ends_with(&format!(".{s}"))
}
Self::Custom(f) => f(origin),
}
}
}
impl<S: Into<String>> From<S> for OriginMatcher {
fn from(value: S) -> Self {
Self::Exact(value.into())
}
}