use std::{borrow::Cow, collections::HashMap, hash::BuildHasher};
use ltk_hash::BinHash;
pub trait FieldNames {
fn field(&self, field: BinHash, class: Option<BinHash>) -> Option<Cow<'_, str>>;
#[expect(
unused_variables,
reason = "the default names its parameter for the reader and uses none"
)]
fn hash(&self, hash: BinHash) -> Option<Cow<'_, str>> {
None
}
}
impl FieldNames for () {
fn field(&self, _field: BinHash, _class: Option<BinHash>) -> Option<Cow<'_, str>> {
None
}
}
impl<S: BuildHasher> FieldNames for HashMap<BinHash, String, S> {
fn field(&self, field: BinHash, _class: Option<BinHash>) -> Option<Cow<'_, str>> {
self.get(&field).map(|name| Cow::Borrowed(name.as_str()))
}
}
impl<S: BuildHasher> FieldNames for HashMap<(BinHash, BinHash), String, S> {
fn field(&self, field: BinHash, class: Option<BinHash>) -> Option<Cow<'_, str>> {
self.get(&(class?, field))
.map(|name| Cow::Borrowed(name.as_str()))
}
}
impl<T: FieldNames + ?Sized> FieldNames for &T {
fn field(&self, field: BinHash, class: Option<BinHash>) -> Option<Cow<'_, str>> {
(**self).field(field, class)
}
fn hash(&self, hash: BinHash) -> Option<Cow<'_, str>> {
(**self).hash(hash)
}
}