Skip to main content

jellyflow_core/ops/mutation/planner/
bindings.rs

1use crate::core::{Binding, BindingId};
2use crate::ops::{GraphOp, GraphTransaction};
3
4use super::GraphMutationPlanner;
5use crate::ops::mutation::GraphMutationError;
6
7impl GraphMutationPlanner<'_> {
8    pub fn add_binding_op(
9        &self,
10        id: BindingId,
11        binding: Binding,
12    ) -> Result<GraphOp, GraphMutationError> {
13        if self.graph.bindings.contains_key(&id) {
14            return Err(GraphMutationError::BindingAlreadyExists(id));
15        }
16        Ok(GraphOp::AddBinding { id, binding })
17    }
18
19    pub fn add_binding_tx(
20        &self,
21        id: BindingId,
22        binding: Binding,
23        label: impl Into<String>,
24    ) -> Result<GraphTransaction, GraphMutationError> {
25        Ok(GraphTransaction::new()
26            .with_label(label)
27            .with_ops([self.add_binding_op(id, binding)?]))
28    }
29
30    pub fn remove_binding_op(&self, id: BindingId) -> Result<GraphOp, GraphMutationError> {
31        let binding = self
32            .graph
33            .bindings
34            .get(&id)
35            .cloned()
36            .ok_or(GraphMutationError::MissingBinding(id))?;
37        Ok(GraphOp::RemoveBinding { id, binding })
38    }
39
40    pub fn remove_binding_tx(
41        &self,
42        id: BindingId,
43        label: impl Into<String>,
44    ) -> Result<GraphTransaction, GraphMutationError> {
45        Ok(GraphTransaction::new()
46            .with_label(label)
47            .with_ops([self.remove_binding_op(id)?]))
48    }
49}