use std::{borrow::Cow, collections::HashMap, ops::Deref};
pub const EVENT_SPAN_NAME: &str = "__EVENT__";
pub trait AttributeMapper: Send + Sync + 'static {
fn map(&self, span_name: &str, name: Cow<'static, str>) -> Cow<'static, str>;
}
impl<F> AttributeMapper for F
where
F: for<'a> Fn(&'a str, Cow<'static, str>) -> Cow<'static, str> + Send + Sync + 'static,
{
fn map(&self, span_name: &str, name: Cow<'static, str>) -> Cow<'static, str> {
self(span_name, name)
}
}
impl AttributeMapper for HashMap<String, String> {
fn map(&self, _span_name: &str, name: Cow<'static, str>) -> Cow<'static, str> {
self.get(name.deref())
.cloned()
.map(Cow::from)
.unwrap_or(name)
}
}
impl AttributeMapper for HashMap<&'static str, &'static str> {
fn map(&self, _span_name: &str, name: Cow<'static, str>) -> Cow<'static, str> {
self.get(name.deref())
.copied()
.map(Cow::from)
.unwrap_or(name)
}
}
impl AttributeMapper for HashMap<String, HashMap<String, String>> {
fn map(&self, span_name: &str, name: Cow<'static, str>) -> Cow<'static, str> {
self.get(span_name)
.and_then(|span_map| span_map.get(name.deref()).cloned().map(Cow::from))
.unwrap_or(name)
}
}
impl AttributeMapper for HashMap<&'static str, HashMap<&'static str, &'static str>> {
fn map(&self, span_name: &str, name: Cow<'static, str>) -> Cow<'static, str> {
self.get(span_name)
.and_then(|span_map| span_map.get(name.deref()).copied().map(Cow::from))
.unwrap_or(name)
}
}