chord_flow/flow/step/
res.rs1use chrono::{DateTime, Utc};
2
3use chord_core::collection::TailDropVec;
4use chord_core::step::{ActionAsset, ActionState, StepAsset, StepId, StepState};
5use chord_core::value::Value;
6
7use crate::flow::step::arg::StepIdStruct;
8
9pub struct ActionAssetStruct {
10 aid: String,
11 start: DateTime<Utc>,
12 end: DateTime<Utc>,
13 explain: Value,
14 state: ActionState,
15}
16
17
18impl ActionAssetStruct {
19 pub fn new(aid: String, start: DateTime<Utc>, end: DateTime<Utc>, explain: Value, state: ActionState) -> ActionAssetStruct {
20 ActionAssetStruct {
21 aid,
22 start,
23 end,
24 explain,
25 state,
26 }
27 }
28
29 pub fn id(&self) -> &str {
30 &self.aid
31 }
32
33 pub fn explain(&self) -> &Value {
34 &self.explain
35 }
36
37 pub fn state(&self) -> &ActionState {
38 &self.state
39 }
40}
41
42impl ActionAsset for ActionAssetStruct {
43 fn id(&self) -> &str {
44 self.id()
45 }
46
47 fn start(&self) -> DateTime<Utc> {
48 self.start
49 }
50
51 fn end(&self) -> DateTime<Utc> {
52 self.end
53 }
54
55 fn explain(&self) -> &Value {
56 self.explain()
57 }
58
59 fn state(&self) -> &ActionState {
60 self.state()
61 }
62}
63
64
65pub struct StepAssetStruct {
66 id: StepIdStruct,
67 start: DateTime<Utc>,
68 end: DateTime<Utc>,
69 state: StepState,
70}
71
72impl StepAssetStruct {
73 pub fn new(
74 id: StepIdStruct,
75 start: DateTime<Utc>,
76 end: DateTime<Utc>,
77 action_asset_vec: Vec<ActionAssetStruct>,
78 ) -> StepAssetStruct {
79 let last_state_is_err = (&action_asset_vec).last().unwrap().state.is_err();
80
81 let aav: Vec<Box<dyn ActionAsset>> = action_asset_vec
82 .into_iter()
83 .map(
84 |a| Box::new(a) as Box<dyn ActionAsset>
85 )
86 .collect();
87
88
89 let state = if last_state_is_err {
90 StepState::Fail(TailDropVec::from(aav))
91 } else {
92 StepState::Ok(TailDropVec::from(aav))
93 };
94
95 StepAssetStruct {
96 id,
97 start,
98 end,
99 state,
100 }
101 }
102}
103
104impl StepAsset for StepAssetStruct {
105 fn id(&self) -> &dyn StepId {
106 &self.id
107 }
108
109 fn start(&self) -> DateTime<Utc> {
110 self.start
111 }
112
113 fn end(&self) -> DateTime<Utc> {
114 self.end
115 }
116
117 fn state(&self) -> &StepState {
118 &self.state
119 }
120}