use crate::error::Result;
use crate::types::{ArchiveLocation, EKey};
use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexVersion {
V5, V7, }
pub struct IndexFile {
version: IndexVersion,
entries: std::collections::BTreeMap<EKey, ArchiveLocation>,
}
impl IndexFile {
pub fn new(version: IndexVersion) -> Self {
Self {
version,
entries: std::collections::BTreeMap::new(),
}
}
pub fn load(path: &Path) -> Result<Self> {
if path.extension().and_then(|s| s.to_str()) == Some("idx") {
let parser = super::IdxParser::parse_file(path)?;
let mut entries = std::collections::BTreeMap::new();
for (ekey, location) in parser.entries() {
entries.insert(*ekey, *location);
}
Ok(Self {
version: IndexVersion::V7,
entries,
})
} else if path.extension().and_then(|s| s.to_str()) == Some("index") {
let parser = super::GroupIndex::parse_file(path)?;
let mut entries = std::collections::BTreeMap::new();
for (ekey, location) in parser.entries() {
entries.insert(*ekey, *location);
}
Ok(Self {
version: IndexVersion::V7,
entries,
})
} else {
Err(crate::error::CascError::InvalidIndexFormat(format!(
"Unknown index file extension: {:?}",
path.extension()
)))
}
}
pub fn lookup(&self, ekey: &EKey) -> Option<&ArchiveLocation> {
self.entries.get(ekey)
}
pub fn add_entry(&mut self, ekey: EKey, location: ArchiveLocation) {
self.entries.insert(ekey, location);
}
pub fn remove_entry(&mut self, ekey: &EKey) -> Option<ArchiveLocation> {
self.entries.remove(ekey)
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn version(&self) -> IndexVersion {
self.version
}
pub fn entries(&self) -> impl Iterator<Item = (&EKey, &ArchiveLocation)> {
self.entries.iter()
}
}