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;
use crate::property::NoMeta;
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(bound = "for <'dee> M: serde::Serialize + serde::Deserialize<'dee>")
)]
#[derive(Debug, Clone, PartialEq)]
pub struct Bin<M = NoMeta> {
pub version: u32,
pub objects: IndexMap<BinHash, BinObject<M>>,
pub dependencies: Vec<String>,
}
impl Default for Bin {
fn default() -> Self {
Self {
version: 3,
objects: IndexMap::new(),
dependencies: Vec::new(),
}
}
}
impl<M> Bin<M> {
pub fn new(
objects: impl IntoIterator<Item = BinObject<M>>,
dependencies: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
Self {
version: 3,
objects: objects
.into_iter()
.map(|o: BinObject<M>| (o.path_hash, o))
.collect(),
dependencies: dependencies.into_iter().map(Into::into).collect(),
}
}
pub fn builder() -> builder::Builder<M>
where
M: Default,
{
builder::Builder::<M>::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<M>> {
self.objects.get(&path_hash)
}
#[inline]
pub fn get_object_mut(&mut self, path_hash: BinHash) -> Option<&mut BinObject<M>> {
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<M>) -> Option<BinObject<M>> {
self.objects.insert(object.path_hash, object)
}
pub fn remove_object(&mut self, path_hash: impl Into<BinHash>) -> Option<BinObject<M>> {
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<M>)> {
self.objects.iter()
}
#[inline]
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&BinHash, &mut BinObject<M>)> {
self.objects.iter_mut()
}
}
impl<'a, M> IntoIterator for &'a Bin<M> {
type Item = (&'a BinHash, &'a BinObject<M>);
type IntoIter = indexmap::map::Iter<'a, BinHash, BinObject<M>>;
fn into_iter(self) -> Self::IntoIter {
self.objects.iter()
}
}
impl<'a, M> IntoIterator for &'a mut Bin<M> {
type Item = (&'a BinHash, &'a mut BinObject<M>);
type IntoIter = indexmap::map::IterMut<'a, BinHash, BinObject<M>>;
fn into_iter(self) -> Self::IntoIter {
self.objects.iter_mut()
}
}
impl<M> IntoIterator for Bin<M> {
type Item = (BinHash, BinObject<M>);
type IntoIter = indexmap::map::IntoIter<BinHash, BinObject<M>>;
fn into_iter(self) -> Self::IntoIter {
self.objects.into_iter()
}
}