Skip to content

Commit ee2de81

Browse files
feat(rustlang book): Type State Pattern reimplemantation
1 parent 372a65f commit ee2de81

File tree

2 files changed

+21
-49
lines changed

2 files changed

+21
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub mod post;
1+
pub mod post;

rustlang_book/object_oriented_features/blog/src/post.rs

+20-48
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,37 @@ pub struct Post {
44
}
55

66
impl Post {
7-
pub fn new() -> Self {
7+
fn new() -> Self {
88
Post {
99
state: Some(Box::new(Draft {})),
1010
content: String::new(),
1111
}
1212
}
1313

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 {
1915
self.state.as_ref().unwrap().content(self)
2016
}
2117

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);
2620
}
27-
pub fn approve(&mut self) {
21+
22+
fn request_review(&mut self) {
2823
if let Some(state) = self.state.take() {
29-
self.state = Some(state.approve());
24+
self.state = Some(state.request_review());
3025
}
3126
}
3227

33-
pub fn reject(&mut self) {
28+
fn approve(&mut self) {
3429
if let Some(state) = self.state.take() {
35-
self.state = Some(state.reject());
30+
self.state = Some(state.approve());
3631
}
3732
}
3833
}
3934

4035
trait State {
4136
fn request_review(self: Box<Self>) -> Box<dyn State>;
4237
fn approve(self: Box<Self>) -> Box<dyn State>;
43-
fn reject(self: Box<Self>) -> Box<dyn State>;
4438
fn content<'a>(&self, post: &'a Post) -> &'a str {
4539
""
4640
}
@@ -50,32 +44,24 @@ struct Draft {}
5044

5145
impl State for Draft {
5246
fn request_review(self: Box<Self>) -> Box<dyn State> {
53-
Box::new(PendingReview {})
47+
Box::new(Pending {})
5448
}
5549

5650
fn approve(self: Box<Self>) -> Box<dyn State> {
5751
self
5852
}
59-
60-
fn reject(self: Box<Self>) -> Box<dyn State> {
61-
self
62-
}
6353
}
6454

65-
struct PendingReview {}
55+
struct Pending {}
6656

67-
impl State for PendingReview {
57+
impl State for Pending {
6858
fn request_review(self: Box<Self>) -> Box<dyn State> {
6959
self
7060
}
7161

7262
fn approve(self: Box<Self>) -> Box<dyn State> {
7363
Box::new(Published {})
7464
}
75-
76-
fn reject(self: Box<Self>) -> Box<dyn State> {
77-
Box::new(Draft {})
78-
}
7965
}
8066

8167
struct Published {}
@@ -92,50 +78,36 @@ impl State for Published {
9278
fn content<'a>(&self, post: &'a Post) -> &'a str {
9379
&post.content
9480
}
95-
96-
fn reject(self: Box<Self>) -> Box<dyn State> {
97-
self
98-
}
9981
}
10082

10183
#[cfg(test)]
10284
mod tests {
10385
use super::*;
10486

10587
#[test]
106-
fn test_draft_content() {
88+
fn creating_a_post_should_set_state_to_draft() {
10789
let mut post = Post::new();
108-
post.add_text("Hello World");
90+
post.add_text("Hey, this is a blog post");
10991

11092
assert_eq!("", post.content());
11193
}
11294

11395
#[test]
114-
fn test_pending_review() {
96+
fn requesting_review_on_a_post_should_set_state_to_pending() {
11597
let mut post = Post::new();
116-
post.add_text("Hello World");
98+
post.add_text("Hey, this is a blog post");
11799
post.request_review();
118100

119101
assert_eq!("", post.content());
120102
}
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();
128103

129-
assert_eq!("Hello World", post.content());
130-
}
131-
132104
#[test]
133-
fn test_rejected_review() {
105+
fn approving_a_review_should_set_state_to_published() {
134106
let mut post = Post::new();
135-
post.add_text("Hello World");
107+
post.add_text("Hey, this is a blog post");
136108
post.request_review();
137-
post.reject();
109+
post.approve();
138110

139-
assert_eq!("", post.content());
111+
assert_eq!("Hey, this is a blog post", post.content());
140112
}
141113
}

0 commit comments

Comments
 (0)