mod builder;
pub use builder::Builder;
mod object;
pub use object::{BinObject, Builder as ObjectBuilder};
mod read;
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 is_override: bool,
pub version: u32,
pub objects: IndexMap<u32, BinObject<M>>,
pub dependencies: Vec<String>,
data_overrides: Vec<()>,
}
impl Default for Bin {
fn default() -> Self {
Self {
version: 3,
is_override: false,
objects: IndexMap::new(),
dependencies: Vec::new(),
data_overrides: 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,
is_override: false,
objects: objects
.into_iter()
.map(|o: BinObject<M>| (o.path_hash, o))
.collect(),
dependencies: dependencies.into_iter().map(Into::into).collect(),
data_overrides: Vec::new(),
}
}
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: u32) -> Option<&BinObject<M>> {
self.objects.get(&path_hash)
}
#[inline]
pub fn get_object_mut(&mut self, path_hash: u32) -> Option<&mut BinObject<M>> {
self.objects.get_mut(&path_hash)
}
#[inline]
pub fn contains_object(&self, path_hash: u32) -> 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: u32) -> Option<BinObject<M>> {
self.objects.shift_remove(&path_hash)
}
pub fn add_dependency(&mut self, dependency: impl Into<String>) {
self.dependencies.push(dependency.into());
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item = (&u32, &BinObject<M>)> {
self.objects.iter()
}
#[inline]
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&u32, &mut BinObject<M>)> {
self.objects.iter_mut()
}
}
impl<'a, M> IntoIterator for &'a Bin<M> {
type Item = (&'a u32, &'a BinObject<M>);
type IntoIter = indexmap::map::Iter<'a, u32, BinObject<M>>;
fn into_iter(self) -> Self::IntoIter {
self.objects.iter()
}
}
impl<'a, M> IntoIterator for &'a mut Bin<M> {
type Item = (&'a u32, &'a mut BinObject<M>);
type IntoIter = indexmap::map::IterMut<'a, u32, BinObject<M>>;
fn into_iter(self) -> Self::IntoIter {
self.objects.iter_mut()
}
}
impl<M> IntoIterator for Bin<M> {
type Item = (u32, BinObject<M>);
type IntoIter = indexmap::map::IntoIter<u32, BinObject<M>>;
fn into_iter(self) -> Self::IntoIter {
self.objects.into_iter()
}
}