Skip to content

Commit 3d40e40

Browse files
committed
add reject() method to State trait
1 parent 721b63b commit 3d40e40

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

c17_state_pattern/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impl Post {
2020
}
2121

2222
pub fn request_review(&mut self) {
23+
//self.state = Some(self.state.take().expect("Valid state").request_review());
2324
if let Some(s) = self.state.take() {
2425
self.state = Some(s.request_review())
2526
}
@@ -29,11 +30,31 @@ impl Post {
2930
self.state = Some(s.approve())
3031
}
3132
}
33+
34+
pub fn reject(&mut self) {
35+
if let Some(s) = self.state.take() {
36+
self.state = Some(s.reject())
37+
}
38+
}
3239
}
3340

41+
42+
/*
43+
Note the capitalized "self" in Box<Self>.
44+
45+
self is a keyword used within methods to represent the instance of the struct or enum on which the method is called.
46+
- It is similar to this in other languages.
47+
- It is a reference to the instance of the struct or enum on which the method is invoked.
48+
49+
Self is a special type in Rust that represents the type of the implementing struct or enum itself.
50+
- It is often used in trait definitions to refer to the type that is implementing the trait.
51+
- It is useful when you want to write generic code using traits and refer to the implementing type within the trait definition.
52+
*/
53+
3454
trait State {
3555
fn request_review(self: Box<Self>) -> Box<dyn State>;
3656
fn approve(self: Box<Self>) -> Box<dyn State>;
57+
fn reject(self: Box<Self>) -> Box<dyn State>;
3758
fn content<'a>(&self, _post: &'a Post) -> &'a str{
3859
""
3960
}
@@ -49,6 +70,10 @@ impl State for Draft {
4970
fn approve(self: Box<Self>) -> Box<dyn State> {
5071
self
5172
}
73+
74+
fn reject(self: Box<Self>) -> Box<dyn State>{
75+
self
76+
}
5277
}
5378

5479
struct PendingReview {}
@@ -60,6 +85,11 @@ impl State for PendingReview {
6085
fn approve(self: Box<Self>) -> Box<dyn State> {
6186
Box::new(Published {})
6287
}
88+
fn reject(self: Box<Self>) -> Box<dyn State>{
89+
Box::new(Draft{})
90+
91+
}
92+
6393
}
6494

6595
struct Published{}
@@ -75,4 +105,8 @@ impl State for Published {
75105
fn content<'a>(&self, post: &'a Post) -> &'a str {
76106
&post.content
77107
}
108+
109+
fn reject(self: Box<Self>) -> Box<dyn State>{
110+
self
111+
}
78112
}

c17_state_pattern/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ fn main() {
55

66
post.add_text("I ate a salad for lunch today");
77
println!("Added text to the post.");
8+
println!("Post content: {}", post.content());
89
assert_eq!("", post.content());
910

1011
post.request_review();
1112
println!("Post review requested.");
13+
println!("Post content: {}", post.content());
14+
assert_eq!("", post.content());
15+
16+
post.reject();
17+
println!("Post review rejected.");
18+
println!("Post content: {}", post.content());
19+
20+
post.request_review();
21+
println!("Post review requested.");
22+
println!("Post content: {}", post.content());
1223
assert_eq!("", post.content());
1324

1425
post.approve();

0 commit comments

Comments
 (0)