mod builder;
pub use builder::Builder;
mod object;
use ltk_hash::BinHash;
pub use object::{BinObject, Builder as ObjectBuilder};
pub(crate) mod read;
pub(crate) mod write;
#[cfg(test)]
mod tests;
use indexmap::IndexMap;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Bin {
pub version: u32,
pub objects: IndexMap<BinHash, BinObject>,
pub dependencies: Vec<String>,
}
impl Default for Bin {
fn default() -> Self {
Self {
version: 3,
objects: IndexMap::new(),
dependencies: Vec::new(),
}
}
}
impl Bin {
pub fn new(
objects: impl IntoIterator<Item = BinObject>,
dependencies: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
Self {
version: 3,
objects: objects
.into_iter()
.map(|o: BinObject| (o.path_hash, o))
.collect(),
dependencies: dependencies.into_iter().map(Into::into).collect(),
}
}
pub fn builder() -> builder::Builder {
builder::Builder::new()
}
#[inline]
pub fn len(&self) -> usize {
self.objects.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.objects.is_empty()
}
#[inline]
pub fn get_object(&self, path_hash: BinHash) -> Option<&BinObject> {
self.objects.get(&path_hash)
}
#[inline]
pub fn get_object_mut(&mut self, path_hash: BinHash) -> Option<&mut BinObject> {
self.objects.get_mut(&path_hash)
}
#[inline]
pub fn contains_object(&self, path_hash: BinHash) -> bool {
self.objects.contains_key(&path_hash)
}
pub fn add_object(&mut self, object: BinObject) -> Option<BinObject> {
self.objects.insert(object.path_hash, object)
}
pub fn remove_object(&mut self, path_hash: impl Into<BinHash>) -> Option<BinObject> {
self.objects.shift_remove(&path_hash.into())
}
pub fn add_dependency(&mut self, dependency: impl Into<String>) {
self.dependencies.push(dependency.into());
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item = (&BinHash, &BinObject)> {
self.objects.iter()
}
#[inline]
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&BinHash, &mut BinObject)> {
self.objects.iter_mut()
}
}
impl<'a> IntoIterator for &'a Bin {
type Item = (&'a BinHash, &'a BinObject);
type IntoIter = indexmap::map::Iter<'a, BinHash, BinObject>;
fn into_iter(self) -> Self::IntoIter {
self.objects.iter()
}
}
impl<'a> IntoIterator for &'a mut Bin {
type Item = (&'a BinHash, &'a mut BinObject);
type IntoIter = indexmap::map::IterMut<'a, BinHash, BinObject>;
fn into_iter(self) -> Self::IntoIter {
self.objects.iter_mut()
}
}
impl IntoIterator for Bin {
type Item = (BinHash, BinObject);
type IntoIter = indexmap::map::IntoIter<BinHash, BinObject>;
fn into_iter(self) -> Self::IntoIter {
self.objects.into_iter()
}
}