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