moduforge_transform/
step.rs

1use std::{any::Any, sync::Arc};
2
3use moduforge_model::{schema::Schema, tree::Tree};
4use std::fmt::Debug;
5
6use crate::TransformResult;
7
8pub trait Step: Any + Send + Sync + Debug + 'static {
9    fn name(&self) -> String;
10    fn apply(
11        &self,
12        dart: &mut Tree,
13        schema: Arc<Schema>,
14    ) -> TransformResult<StepResult>;
15    fn serialize(&self) -> Option<Vec<u8>>;
16    fn invert(
17        &self,
18        dart: &Arc<Tree>,
19    ) -> Option<Arc<dyn Step>>;
20}
21impl dyn Step {
22    pub fn downcast_ref<E: Step>(&self) -> Option<&E> {
23        <dyn Any>::downcast_ref::<E>(self)
24    }
25}
26
27#[derive(Debug, Clone)]
28pub struct StepResult {
29    pub failed: Option<String>,
30}
31
32impl StepResult {
33    pub fn ok() -> Self {
34        StepResult { failed: None }
35    }
36
37    pub fn fail(message: String) -> Self {
38        StepResult { failed: Some(message) }
39    }
40}