use std::collections::HashMap;
use ltk_hash::BinHash;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ObjectEntry {
pub path_hash: BinHash,
pub class_hash: BinHash,
pub offset: u64,
pub size: u32,
}
impl ObjectEntry {
#[must_use]
pub fn byte_range(&self) -> std::ops::Range<u64> {
self.offset..self.offset + 4 + u64::from(self.size)
}
}
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(from = "Vec<ObjectEntry>", into = "Vec<ObjectEntry>")
)]
#[derive(Debug, Clone, Default)]
pub struct BinToc {
entries: Vec<ObjectEntry>,
index: HashMap<BinHash, usize>,
}
impl BinToc {
#[must_use]
pub fn entries(&self) -> &[ObjectEntry] {
&self.entries
}
#[must_use]
pub fn entry(&self, path_hash: impl Into<BinHash>) -> Option<&ObjectEntry> {
self.entries.get(*self.index.get(&path_hash.into())?)
}
pub(crate) fn push(&mut self, entry: ObjectEntry) {
self.index.insert(entry.path_hash, self.entries.len());
self.entries.push(entry);
}
}
impl From<Vec<ObjectEntry>> for BinToc {
fn from(entries: Vec<ObjectEntry>) -> Self {
let mut toc = Self::default();
for entry in entries {
toc.push(entry);
}
toc
}
}
impl From<BinToc> for Vec<ObjectEntry> {
fn from(toc: BinToc) -> Self {
toc.entries
}
}