use std::hash::{Hash, Hasher};
use nadir_core::hash::FxLikeHasher;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct Id(u64);
impl Id {
pub(crate) const ROOT: Self = Self(0x6e61_6469_725f_6462);
pub(crate) fn with(self, key: impl Hash) -> Self {
let mut hasher = FxLikeHasher::default();
self.0.hash(&mut hasher);
key.hash(&mut hasher);
Self(hasher.finish())
}
}
pub(crate) fn split(text: &str) -> (&str, &str) {
match text.find("##") {
Some(at) => (&text[..at], &text[at + 2..]),
None => (text, text),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_explicit_id_is_kept_while_the_text_shown_changes() {
assert_eq!(split("Pause (3)##pause"), ("Pause (3)", "pause"));
assert_eq!(split("Reset"), ("Reset", "Reset"));
assert_eq!(split("##hidden"), ("", "hidden"));
let window = Id::ROOT.with("Stats");
assert_eq!(
window.with(split("Pause (3)##pause").1),
window.with("pause")
);
assert_ne!(window.with("pause"), Id::ROOT.with("pause"), "by parent");
assert_ne!(window.with("a"), window.with("b"));
}
}