Skip to main content

draxl_patch/apply/
mod.rs

1mod attach;
2mod delete;
3mod insert;
4mod r#move;
5mod put;
6mod replace;
7mod set_clear;
8mod support;
9
10use crate::error::PatchError;
11use crate::model::PatchOp;
12use draxl_ast::File;
13
14/// Applies a single patch operation to a file.
15pub fn apply_op(file: &mut File, op: PatchOp) -> Result<(), PatchError> {
16    match op {
17        PatchOp::Insert { dest, node } => insert::apply_insert(file, dest, node),
18        PatchOp::Put { slot, node } => put::apply_put(file, slot, node),
19        PatchOp::Replace {
20            target_id,
21            replacement,
22        } => replace::apply_replace(file, &target_id, replacement),
23        PatchOp::Delete { target_id } => delete::apply_delete(file, &target_id),
24        PatchOp::Move { target_id, dest } => r#move::apply_move(file, &target_id, dest),
25        PatchOp::Set { path, value } => set_clear::apply_set(file, path, value),
26        PatchOp::Clear { path } => set_clear::apply_clear(file, path),
27        PatchOp::Attach { node_id, target_id } => attach::apply_attach(file, &node_id, &target_id),
28        PatchOp::Detach { node_id } => attach::apply_detach(file, &node_id),
29    }
30}
31
32/// Applies a sequence of patch operations in order.
33pub fn apply_ops(
34    file: &mut File,
35    ops: impl IntoIterator<Item = PatchOp>,
36) -> Result<(), PatchError> {
37    for op in ops {
38        apply_op(file, op)?;
39    }
40    Ok(())
41}