]> piware.de Git - learn-rust.git/blobdiff - concepts/src/lib.rs
concepts: Add Post.reject() transition
[learn-rust.git] / concepts / src / lib.rs
index bb15eeb814ea45d9aee64ed43497f586df33ba65..923b80b451cbe86402e4c649859da5705c11d878 100644 (file)
@@ -137,11 +137,16 @@ impl Post {
     pub fn approve(&mut self) {
         self.state = self.state.approve();
     }
+
+    pub fn reject(&mut self) {
+        self.state = self.state.reject();
+    }
 }
 
 trait State {
     fn request_review(&self) -> Box<dyn State>;
     fn approve(&self) -> Box<dyn State>;
+    fn reject(&self) -> Box<dyn State>;
 
     #[allow(unused_variables)]
     fn content<'a>(&self, post: &'a Post) -> &'a str {
@@ -159,6 +164,10 @@ impl State for Draft {
         // don't change state
         Box::new(Self {})
     }
+
+    fn reject(&self) -> Box<dyn State> {
+        Box::new(Self {})
+    }
 }
 
 struct PendingReview {}
@@ -170,6 +179,10 @@ impl State for PendingReview {
     fn approve(&self) -> Box<dyn State> {
         Box::new(Published {})
     }
+
+    fn reject(&self) -> Box<dyn State> {
+        Box::new(Draft {})
+    }
 }
 
 struct Published {}
@@ -182,6 +195,10 @@ impl State for Published {
         Box::new(Published {})
     }
 
+    fn reject(&self) -> Box<dyn State> {
+        Box::new(Self {})
+    }
+
     fn content<'a>(&self, post: &'a Post) -> &'a str {
         &post.content
     }