use crate::types::UpdateOp;
#[derive(Debug, Clone, Default)]
pub struct UpdateBuilder {
ops: Vec<UpdateOp>,
}
impl UpdateBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn set(mut self, path: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
self.ops.push(UpdateOp::set(path.into(), value.into()));
self
}
pub fn increment(mut self, path: impl Into<String>, by: i64) -> Self {
self.ops.push(UpdateOp::increment(path.into(), by));
self
}
pub fn decrement(mut self, path: impl Into<String>, by: i64) -> Self {
self.ops.push(UpdateOp::decrement(path.into(), by));
self
}
pub fn append(mut self, path: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
self.ops.push(UpdateOp::append(path.into(), value.into()));
self
}
pub fn remove(mut self, path: impl Into<String>) -> Self {
self.ops.push(UpdateOp::remove(path.into()));
self
}
pub fn merge(mut self, value: impl Into<serde_json::Value>) -> Self {
self.ops.push(UpdateOp::merge(value.into()));
self
}
pub fn build(self) -> Vec<UpdateOp> {
self.ops
}
}
#[cfg(test)]
mod tests {
use crate::types::MergePath;
use super::*;
#[test]
fn builder_adds_append_operation() {
let ops = UpdateBuilder::new()
.append("chunks", serde_json::json!("hello"))
.build();
assert_eq!(ops.len(), 1);
match &ops[0] {
UpdateOp::Append {
path: Some(MergePath::Single(s)),
value,
} => {
assert_eq!(s, "chunks");
assert_eq!(value, &serde_json::json!("hello"));
}
other => panic!("expected single-string append, got {other:?}"),
}
}
}