ltk_meta 0.7.0

Metadata formats and utilities for League Toolkit
Documentation
use super::BinObject;
use crate::Bin;

/// A builder for constructing [`Bin`] instances.
///
/// # Examples
///
/// ```
/// use ltk_meta::{Bin, BinObject, property::NoMeta};
///
/// let tree = Bin::<NoMeta>::builder()
///     .dependency("base.bin")
///     .dependencies(["extra1.bin", "extra2.bin"])
///     .object(BinObject::new(0x1234, 0x5678))
///     .build();
/// ```
#[derive(Debug, Default, Clone)]
pub struct Builder<M> {
    objects: Vec<BinObject<M>>,
    dependencies: Vec<String>,
}

impl<M> Builder<M> {
    /// See: [`Bin::builder`]
    pub fn new() -> Self
    where
        M: Default,
    {
        Self::default()
    }

    /// Adds a single dependency.
    pub fn dependency(mut self, dep: impl Into<String>) -> Self {
        self.dependencies.push(dep.into());
        self
    }

    /// Adds multiple dependencies.
    pub fn dependencies(mut self, deps: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.dependencies.extend(deps.into_iter().map(Into::into));
        self
    }

    /// Adds a single object.
    pub fn object(mut self, obj: BinObject<M>) -> Self {
        self.objects.push(obj);
        self
    }

    /// Adds multiple objects.
    pub fn objects(mut self, objs: impl IntoIterator<Item = BinObject<M>>) -> Self {
        self.objects.extend(objs);
        self
    }

    /// Build the final [`Bin`].
    ///
    /// The resulting tree will have version 3, which is always used when writing.
    pub fn build(self) -> Bin<M> {
        Bin {
            version: 3,
            objects: self.objects.into_iter().map(|o| (o.path_hash, o)).collect(),
            dependencies: self.dependencies,
        }
    }
}