use super::grammar;
static DEFAULT_FIELDS: &[&str] = &[
"message",
"custom.error.message",
"custom.error.stack",
"custom.title",
"_default_",
];
static RESERVED_ATTRIBUTES: &[&str] = &[
"host",
"source",
"status",
"service",
"trace_id",
"message",
"timestamp",
"tags",
];
#[derive(Clone, Hash, PartialEq, Eq, Debug)]
pub enum Field {
Default(String),
Reserved(String),
Attribute(String),
Tag(String),
}
impl Field {
pub fn as_str(&self) -> &str {
match self {
Self::Default(s) => s,
Self::Reserved(s) => s,
Self::Attribute(s) => s,
Self::Tag(s) => s,
}
}
}
pub fn normalize_fields<T: AsRef<str>>(value: T) -> Vec<Field> {
let value = value.as_ref();
if value.eq(grammar::DEFAULT_FIELD) {
return DEFAULT_FIELDS
.iter()
.map(|s| Field::Default((*s).to_owned()))
.collect();
}
let field = match value.replace('@', ".") {
v if value.starts_with('@') => Field::Attribute(v),
v if DEFAULT_FIELDS.contains(&v.as_ref()) => Field::Default(v),
v if RESERVED_ATTRIBUTES.contains(&v.as_ref()) => Field::Reserved(v),
v => Field::Tag(v),
};
vec![field]
}