mf_transform/
step.rs

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