mod table;
use ahash::AHashMap as HashMap;
use parking_lot::Mutex;
use std::sync::OnceLock;
type Names = HashMap<(&'static str, &'static str), &'static str>;
fn drafted() -> &'static Names {
static DRAFTED: OnceLock<Names> = OnceLock::new();
DRAFTED.get_or_init(|| {
table::ATTRIBUTES
.iter()
.map(|&(node_type, legacy, draft)| ((node_type, legacy), draft))
.collect()
})
}
fn shipped() -> &'static Names {
static SHIPPED: OnceLock<Names> = OnceLock::new();
SHIPPED.get_or_init(|| {
table::ATTRIBUTES
.iter()
.map(|&(node_type, legacy, draft)| ((node_type, draft), legacy))
.collect()
})
}
fn look_up(
names: &'static Names,
node_type: &str,
name: &str,
) -> Option<&'static str> {
let node_type = legacy_node_type(node_type).unwrap_or(node_type);
names
.get(&(node_type, name))
.or_else(|| names.get(&("", name)))
.copied()
}
pub fn draft_attribute(node_type: &str, name: &str) -> Option<&'static str> {
look_up(drafted(), node_type, name)
}
pub fn legacy_attribute(node_type: &str, name: &str) -> Option<&'static str> {
look_up(shipped(), node_type, name)
}
pub fn draft_node_type(node_type: &str) -> Option<&'static str> {
table::NODE_TYPES
.iter()
.find(|&&(legacy, _)| legacy == node_type)
.map(|&(_, draft)| draft)
}
pub fn legacy_node_type(node_type: &str) -> Option<&'static str> {
table::NODE_TYPES
.iter()
.find(|&&(_, draft)| draft == node_type)
.map(|&(legacy, _)| legacy)
}
pub(crate) fn reserved_node_type(handle: &str) -> Option<&'static str> {
match handle {
crate::GLOBAL => Some("global"),
crate::ROOT => Some("root"),
_ => None,
}
}
pub(crate) fn to_draft(node_type: &str, argument: &mut crate::OwnedArgument) {
if let Some(draft) = draft_attribute(node_type, &argument.name) {
warn_deprecated(&argument.name, draft);
argument.name = draft.to_string();
}
}
pub(crate) fn warn_deprecated(legacy: &str, draft: &str) {
static WARNED: OnceLock<Mutex<ahash::AHashSet<String>>> = OnceLock::new();
if WARNED
.get_or_init(Default::default)
.lock()
.insert(legacy.to_string())
{
log::warn!("ɴsɪ `{legacy}` is deprecated; use `{draft}`");
}
}
#[cfg(test)]
mod tests;