]> piware.de Git - learn-rust.git/commitdiff
concepts: Add Post.reject() transition
authorMartin Pitt <martin@piware.de>
Sun, 29 Aug 2021 14:21:01 +0000 (16:21 +0200)
committerMartin Pitt <martin@piware.de>
Sun, 29 Aug 2021 14:21:01 +0000 (16:21 +0200)
concepts/src/lib.rs
concepts/src/main.rs

index bb15eeb814ea45d9aee64ed43497f586df33ba65..923b80b451cbe86402e4c649859da5705c11d878 100644 (file)
@@ -137,11 +137,16 @@ impl Post {
     pub fn approve(&mut self) {
         self.state = self.state.approve();
     }
     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>;
 }
 
 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 {
 
     #[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 {})
     }
         // don't change state
         Box::new(Self {})
     }
+
+    fn reject(&self) -> Box<dyn State> {
+        Box::new(Self {})
+    }
 }
 
 struct PendingReview {}
 }
 
 struct PendingReview {}
@@ -170,6 +179,10 @@ impl State for PendingReview {
     fn approve(&self) -> Box<dyn State> {
         Box::new(Published {})
     }
     fn approve(&self) -> Box<dyn State> {
         Box::new(Published {})
     }
+
+    fn reject(&self) -> Box<dyn State> {
+        Box::new(Draft {})
+    }
 }
 
 struct Published {}
 }
 
 struct Published {}
@@ -182,6 +195,10 @@ impl State for Published {
         Box::new(Published {})
     }
 
         Box::new(Published {})
     }
 
+    fn reject(&self) -> Box<dyn State> {
+        Box::new(Self {})
+    }
+
     fn content<'a>(&self, post: &'a Post) -> &'a str {
         &post.content
     }
     fn content<'a>(&self, post: &'a Post) -> &'a str {
         &post.content
     }
index f9eafe95978b7bb802c7f1b8ffd3088cf2c19792..3e9bbbd0cd042234615d20dd563baf6278258464 100644 (file)
@@ -256,6 +256,12 @@ fn test_dyn_traits() {
     post.request_review();
     assert_eq!("", post.content());
 
     post.request_review();
     assert_eq!("", post.content());
 
+    post.reject();
+    assert_eq!("", post.content());
+
+    post.request_review();
+    assert_eq!("", post.content());
+
     post.approve();
     assert_eq!(text, post.content());
 }
     post.approve();
     assert_eq!(text, post.content());
 }