moduforge_core/transform/
mod.rs

1use std::sync::Arc;
2
3use attr_step::AttrStep;
4use mark_step::AddMarkStep;
5use node_step::{AddNodeStep, MoveNodeStep, RemoveNodeStep, ReplaceNodeStep};
6use serde::{Deserialize, Serialize};
7use step::{Step, StepResult};
8use transform::TransformError;
9
10use crate::model::{node_pool::Draft, patch::Patch, schema::Schema};
11pub mod attr_step;
12pub mod mark_step;
13pub mod node_step;
14pub mod step;
15pub mod transform;
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub enum ConcreteStep {
19    UpdateAttrs(AttrStep),
20    AddNodeStep(AddNodeStep),
21    AddMarkStep(AddMarkStep),
22    RemoveNodeStep(RemoveNodeStep),
23    PatchStep(PatchStep),
24    MoveNodeStep(MoveNodeStep),
25    ReplaceNodeStep(ReplaceNodeStep),
26    BatchStep(BatchStep),
27}
28impl Step for ConcreteStep {
29    fn apply(
30        &self,
31        dart: &mut Draft,
32        schema: std::sync::Arc<crate::model::schema::Schema>,
33    ) -> Result<step::StepResult, transform::TransformError> {
34        match self {
35            ConcreteStep::UpdateAttrs(attr_step) => {
36                attr_step.apply(dart, schema)
37            },
38            ConcreteStep::AddNodeStep(add_node_step) => {
39                add_node_step.apply(dart, schema)
40            },
41            ConcreteStep::AddMarkStep(add_mark_step) => {
42                add_mark_step.apply(dart, schema)
43            },
44            ConcreteStep::RemoveNodeStep(remove_node_step) => {
45                remove_node_step.apply(dart, schema)
46            },
47            ConcreteStep::PatchStep(patch_step) => {
48                patch_step.apply(dart, schema)
49            },
50            ConcreteStep::MoveNodeStep(move_node_step) => {
51                move_node_step.apply(dart, schema)
52            },
53            ConcreteStep::BatchStep(batch_step) => {
54                batch_step.apply(dart, schema)
55            },
56            ConcreteStep::ReplaceNodeStep(replace_node_step) => {
57                replace_node_step.apply(dart, schema)
58            },
59        }
60    }
61    fn to_concrete(&self) -> ConcreteStep {
62        self.clone()
63    }
64}
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct PatchStep {
67    pub patches: Vec<Patch>,
68}
69impl Step for PatchStep {
70    fn apply(
71        &self,
72        dart: &mut Draft,
73        _: std::sync::Arc<crate::model::schema::Schema>,
74    ) -> Result<step::StepResult, transform::TransformError> {
75        match dart.apply_patches(&self.patches) {
76            Ok(()) => Ok(dart.commit()),
77            Err(err) => Err(TransformError::new(err.to_string())),
78        }
79    }
80
81    fn to_concrete(&self) -> ConcreteStep {
82        ConcreteStep::PatchStep(self.clone())
83    }
84}
85/// 批量操作步骤
86#[derive(Debug, Serialize, Deserialize, Clone)]
87pub struct BatchStep {
88    steps: Vec<ConcreteStep>,
89}
90
91impl BatchStep {
92    pub fn new(steps: Vec<ConcreteStep>) -> Self {
93        BatchStep { steps }
94    }
95}
96impl Step for BatchStep {
97    fn apply(
98        &self,
99        dart: &mut Draft,
100        schema: Arc<Schema>,
101    ) -> Result<StepResult, TransformError> {
102        dart.begin = true;
103        for step in &self.steps {
104            let schema = schema.clone();
105            let result = match step {
106                ConcreteStep::UpdateAttrs(attr_step) => {
107                    attr_step.apply(dart, schema)
108                },
109                ConcreteStep::AddNodeStep(add_node_step) => {
110                    add_node_step.apply(dart, schema)
111                },
112                ConcreteStep::AddMarkStep(add_mark_step) => {
113                    add_mark_step.apply(dart, schema)
114                },
115                ConcreteStep::RemoveNodeStep(remove_node_step) => {
116                    remove_node_step.apply(dart, schema)
117                },
118                ConcreteStep::PatchStep(patch_step) => {
119                    patch_step.apply(dart, schema)
120                },
121                ConcreteStep::MoveNodeStep(move_node_step) => {
122                    move_node_step.apply(dart, schema)
123                },
124                ConcreteStep::ReplaceNodeStep(replace_node_step) => {
125                    replace_node_step.apply(dart, schema)
126                },
127                ConcreteStep::BatchStep(batch_setp) => {
128                    batch_setp.apply(dart, schema)
129                },
130            };
131            match result {
132                Ok(result) => {
133                    if let Some(message) = result.failed {
134                        return Ok(StepResult::fail(message));
135                    }
136                    // 继续执行下一个步骤
137                },
138                Err(err) => return Err(err),
139            }
140        }
141        dart.begin = false;
142        // 所有步骤执行成功,提交更改
143        Ok(dart.commit())
144    }
145
146    fn to_concrete(&self) -> ConcreteStep {
147        ConcreteStep::BatchStep(self.clone())
148    }
149}