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, LowerLanguage};
13
14/// Applies a single patch operation to a file.
15pub fn apply_op(file: &mut File, op: PatchOp) -> Result<(), PatchError> {
16    apply_op_for_language(LowerLanguage::Rust, file, op)
17}
18
19/// Applies a single patch operation to a file using the selected lower language.
20pub fn apply_op_for_language(
21    language: LowerLanguage,
22    file: &mut File,
23    op: PatchOp,
24) -> Result<(), PatchError> {
25    match op {
26        PatchOp::Insert { dest, node } => insert::apply_insert(language, file, dest, node),
27        PatchOp::Put { slot, node } => put::apply_put(language, file, slot, node),
28        PatchOp::Replace {
29            target_id,
30            replacement,
31        } => replace::apply_replace(file, &target_id, replacement),
32        PatchOp::Delete { target_id } => delete::apply_delete(language, file, &target_id),
33        PatchOp::Move { target_id, dest } => r#move::apply_move(language, file, &target_id, dest),
34        PatchOp::Set { path, value } => set_clear::apply_set(language, file, path, value),
35        PatchOp::Clear { path } => set_clear::apply_clear(language, file, path),
36        PatchOp::Attach { node_id, target_id } => {
37            attach::apply_attach(language, file, &node_id, &target_id)
38        }
39        PatchOp::Detach { node_id } => attach::apply_detach(language, file, &node_id),
40    }
41}
42
43/// Applies a sequence of patch operations in order.
44pub fn apply_ops(
45    file: &mut File,
46    ops: impl IntoIterator<Item = PatchOp>,
47) -> Result<(), PatchError> {
48    apply_ops_for_language(LowerLanguage::Rust, file, ops)
49}
50
51/// Applies a sequence of patch operations in order using the selected lower language.
52pub fn apply_ops_for_language(
53    language: LowerLanguage,
54    file: &mut File,
55    ops: impl IntoIterator<Item = PatchOp>,
56) -> Result<(), PatchError> {
57    for op in ops {
58        apply_op_for_language(language, file, op)?;
59    }
60    Ok(())
61}