mod apply;
pub use apply::{ApplyReport, SkippedPatch};
mod read;
mod write;
use indexmap::IndexMap;
use ltk_hash::BinHash;
use crate::{path::PropertyPath, property::Kind, BinObject, PropertyValueEnum};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct BinOverride {
pub deleted: Vec<BinHash>,
pub objects: IndexMap<BinHash, BinObject>,
pub patches: Vec<PropertyPatch>,
}
impl Default for BinOverride {
fn default() -> Self {
Self::new()
}
}
impl BinOverride {
#[must_use]
pub fn new() -> Self {
Self {
deleted: Vec::new(),
objects: IndexMap::new(),
patches: Vec::new(),
}
}
#[must_use]
pub fn builder() -> Builder {
Builder::new()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.deleted.is_empty() && self.objects.is_empty() && self.patches.is_empty()
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct PropertyPatch {
pub object_hash: BinHash,
pub path: PropertyPath,
pub value: PropertyValueEnum,
}
impl PropertyPatch {
pub fn new(
object_hash: impl Into<BinHash>,
path: PropertyPath,
value: impl Into<PropertyValueEnum>,
) -> Self {
Self {
object_hash: object_hash.into(),
path,
value: value.into(),
}
}
#[must_use]
#[inline]
pub fn kind(&self) -> Kind {
self.value.kind()
}
}
#[derive(Debug, Clone)]
pub struct Builder {
deleted: Vec<BinHash>,
objects: Vec<BinObject>,
patches: Vec<PropertyPatch>,
}
impl Default for Builder {
fn default() -> Self {
Self {
deleted: Vec::new(),
objects: Vec::new(),
patches: Vec::new(),
}
}
}
impl Builder {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn delete(mut self, object_hash: impl Into<BinHash>) -> Self {
self.deleted.push(object_hash.into());
self
}
#[must_use]
pub fn deletions(mut self, hashes: impl IntoIterator<Item = impl Into<BinHash>>) -> Self {
self.deleted.extend(hashes.into_iter().map(Into::into));
self
}
#[must_use]
pub fn object(mut self, object: BinObject) -> Self {
self.objects.push(object);
self
}
#[must_use]
pub fn objects(mut self, objects: impl IntoIterator<Item = BinObject>) -> Self {
self.objects.extend(objects);
self
}
#[must_use]
pub fn set(
self,
object_hash: impl Into<BinHash>,
path: PropertyPath,
value: impl Into<PropertyValueEnum>,
) -> Self {
self.patch(PropertyPatch::new(object_hash, path, value))
}
#[must_use]
pub fn patch(mut self, patch: PropertyPatch) -> Self {
self.patches.push(patch);
self
}
#[must_use]
pub fn patches(mut self, patches: impl IntoIterator<Item = PropertyPatch>) -> Self {
self.patches.extend(patches);
self
}
#[must_use]
pub fn build(self) -> BinOverride {
BinOverride {
deleted: self.deleted,
objects: self
.objects
.into_iter()
.map(|object| (object.path_hash, object))
.collect(),
patches: self.patches,
}
}
}