@@ -4,43 +4,37 @@ pub struct Post {
4
4
}
5
5
6
6
impl Post {
7
- pub fn new ( ) -> Self {
7
+ fn new ( ) -> Self {
8
8
Post {
9
9
state : Some ( Box :: new ( Draft { } ) ) ,
10
10
content : String :: new ( ) ,
11
11
}
12
12
}
13
13
14
- pub fn add_text ( & mut self , text : & str ) {
15
- self . content . push_str ( text) ;
16
- }
17
-
18
- pub fn content ( & self ) -> & str {
14
+ fn content ( & self ) -> & str {
19
15
self . state . as_ref ( ) . unwrap ( ) . content ( self )
20
16
}
21
17
22
- pub fn request_review ( & mut self ) {
23
- if let Some ( state) = self . state . take ( ) {
24
- self . state = Some ( state. request_review ( ) )
25
- }
18
+ fn add_text ( & mut self , text : & str ) {
19
+ self . content . push_str ( text) ;
26
20
}
27
- pub fn approve ( & mut self ) {
21
+
22
+ fn request_review ( & mut self ) {
28
23
if let Some ( state) = self . state . take ( ) {
29
- self . state = Some ( state. approve ( ) ) ;
24
+ self . state = Some ( state. request_review ( ) ) ;
30
25
}
31
26
}
32
27
33
- pub fn reject ( & mut self ) {
28
+ fn approve ( & mut self ) {
34
29
if let Some ( state) = self . state . take ( ) {
35
- self . state = Some ( state. reject ( ) ) ;
30
+ self . state = Some ( state. approve ( ) ) ;
36
31
}
37
32
}
38
33
}
39
34
40
35
trait State {
41
36
fn request_review ( self : Box < Self > ) -> Box < dyn State > ;
42
37
fn approve ( self : Box < Self > ) -> Box < dyn State > ;
43
- fn reject ( self : Box < Self > ) -> Box < dyn State > ;
44
38
fn content < ' a > ( & self , post : & ' a Post ) -> & ' a str {
45
39
""
46
40
}
@@ -50,32 +44,24 @@ struct Draft {}
50
44
51
45
impl State for Draft {
52
46
fn request_review ( self : Box < Self > ) -> Box < dyn State > {
53
- Box :: new ( PendingReview { } )
47
+ Box :: new ( Pending { } )
54
48
}
55
49
56
50
fn approve ( self : Box < Self > ) -> Box < dyn State > {
57
51
self
58
52
}
59
-
60
- fn reject ( self : Box < Self > ) -> Box < dyn State > {
61
- self
62
- }
63
53
}
64
54
65
- struct PendingReview { }
55
+ struct Pending { }
66
56
67
- impl State for PendingReview {
57
+ impl State for Pending {
68
58
fn request_review ( self : Box < Self > ) -> Box < dyn State > {
69
59
self
70
60
}
71
61
72
62
fn approve ( self : Box < Self > ) -> Box < dyn State > {
73
63
Box :: new ( Published { } )
74
64
}
75
-
76
- fn reject ( self : Box < Self > ) -> Box < dyn State > {
77
- Box :: new ( Draft { } )
78
- }
79
65
}
80
66
81
67
struct Published { }
@@ -92,50 +78,36 @@ impl State for Published {
92
78
fn content < ' a > ( & self , post : & ' a Post ) -> & ' a str {
93
79
& post. content
94
80
}
95
-
96
- fn reject ( self : Box < Self > ) -> Box < dyn State > {
97
- self
98
- }
99
81
}
100
82
101
83
#[ cfg( test) ]
102
84
mod tests {
103
85
use super :: * ;
104
86
105
87
#[ test]
106
- fn test_draft_content ( ) {
88
+ fn creating_a_post_should_set_state_to_draft ( ) {
107
89
let mut post = Post :: new ( ) ;
108
- post. add_text ( "Hello World " ) ;
90
+ post. add_text ( "Hey, this is a blog post " ) ;
109
91
110
92
assert_eq ! ( "" , post. content( ) ) ;
111
93
}
112
94
113
95
#[ test]
114
- fn test_pending_review ( ) {
96
+ fn requesting_review_on_a_post_should_set_state_to_pending ( ) {
115
97
let mut post = Post :: new ( ) ;
116
- post. add_text ( "Hello World " ) ;
98
+ post. add_text ( "Hey, this is a blog post " ) ;
117
99
post. request_review ( ) ;
118
100
119
101
assert_eq ! ( "" , post. content( ) ) ;
120
102
}
121
-
122
- #[ test]
123
- fn test_approved_review ( ) {
124
- let mut post = Post :: new ( ) ;
125
- post. add_text ( "Hello World" ) ;
126
- post. request_review ( ) ;
127
- post. approve ( ) ;
128
103
129
- assert_eq ! ( "Hello World" , post. content( ) ) ;
130
- }
131
-
132
104
#[ test]
133
- fn test_rejected_review ( ) {
105
+ fn approving_a_review_should_set_state_to_published ( ) {
134
106
let mut post = Post :: new ( ) ;
135
- post. add_text ( "Hello World " ) ;
107
+ post. add_text ( "Hey, this is a blog post " ) ;
136
108
post. request_review ( ) ;
137
- post. reject ( ) ;
109
+ post. approve ( ) ;
138
110
139
- assert_eq ! ( "" , post. content( ) ) ;
111
+ assert_eq ! ( "Hey, this is a blog post " , post. content( ) ) ;
140
112
}
141
113
}
0 commit comments