use std::fmt;
use binrw::binrw;
use xxhash_rust::xxh3::xxh3_64;
#[binrw]
#[brw(little)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PathHash(u64);
impl PathHash {
pub const fn new(raw: u64) -> Self {
Self(raw)
}
pub fn from_hex_name(name: &str) -> Option<Self> {
let base = name.split('.').next().unwrap_or(name);
if base.len() != 16 || !base.bytes().all(|b| b.is_ascii_hexdigit()) {
return None;
}
u64::from_str_radix(base, 16).ok().map(Self)
}
pub const fn value(self) -> u64 {
self.0
}
}
impl fmt::Display for PathHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:016x}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LayerHash(u64);
impl LayerHash {
pub const NONE: Self = Self(u64::MAX);
pub fn from_name(name: &str) -> Self {
Self(xxh3_64(name.to_ascii_lowercase().as_bytes()))
}
pub const fn new(raw: u64) -> Self {
Self(raw)
}
pub const fn value(self) -> u64 {
self.0
}
}
impl fmt::Display for LayerHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:016x}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct WadNameHash(u64);
impl WadNameHash {
pub const NONE: Self = Self(u64::MAX);
pub fn from_name(name: &str) -> Self {
Self(xxh3_64(name.to_ascii_lowercase().as_bytes()))
}
pub const fn new(raw: u64) -> Self {
Self(raw)
}
pub const fn value(self) -> u64 {
self.0
}
}
impl fmt::Display for WadNameHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:016x}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ChunkKey {
pub path: PathHash,
pub layer: LayerHash,
}
impl ChunkKey {
pub const fn new(path: PathHash, layer: LayerHash) -> Self {
Self { path, layer }
}
}