-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path08-transition-with-data.rs
More file actions
61 lines (51 loc) · 1.64 KB
/
08-transition-with-data.rs
File metadata and controls
61 lines (51 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use statum::{machine, state, transition};
#[state]
enum State {
Draft(MyDraft),
InReview(DraftWithComment),
Published,
}
#[machine]
struct Machine<State> {}
struct MyDraft {
_title: String,
_content: String,
}
struct DraftWithComment {
_draft: MyDraft,
_comment: String,
}
#[transition]
impl Machine<Draft> {
pub fn _into_review(self) -> Machine<InReview> {
let my_draft: &MyDraft = &self.state_data;
let draft_with_comment = DraftWithComment {
_draft: MyDraft {
_title: my_draft._title.clone(),
_content: my_draft._content.clone(),
},
_comment: "This is a comment".to_owned(),
};
// NOTE: when transitioning to InReview, we need to provide the transition method with the
// next state's data. In this case, we are transitioning to InReview and we need to provide DraftWithComment
// This will make the data available for use in the next state
self.transition_with(draft_with_comment)
}
}
#[transition]
impl Machine<InReview> {
pub fn _into_published(self) -> Machine<Published> {
//NOTE: we can access the state data that the previous state transitioned with
let _draft_with_comment: &DraftWithComment = &self.state_data;
self.transition()
}
}
pub fn run() {
let draft = MyDraft {
_title: "My first article".to_owned(),
_content: "This is the content of my first article".to_owned(),
};
let machine = Machine::<Draft>::builder().state_data(draft).build();
let machine = machine._into_review();
let _machine = machine._into_published();
}