From 6cf2fd87f634bb24ca0ab801a8ba3fbfff1ac418 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Sun, 29 Aug 2021 16:21:01 +0200 Subject: [PATCH 1/1] concepts: Add Post.reject() transition --- concepts/src/lib.rs | 17 +++++++++++++++++ concepts/src/main.rs | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/concepts/src/lib.rs b/concepts/src/lib.rs index bb15eeb..923b80b 100644 --- a/concepts/src/lib.rs +++ b/concepts/src/lib.rs @@ -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; fn approve(&self) -> Box; + fn reject(&self) -> Box; #[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 { + Box::new(Self {}) + } } struct PendingReview {} @@ -170,6 +179,10 @@ impl State for PendingReview { fn approve(&self) -> Box { Box::new(Published {}) } + + fn reject(&self) -> Box { + Box::new(Draft {}) + } } struct Published {} @@ -182,6 +195,10 @@ impl State for Published { Box::new(Published {}) } + fn reject(&self) -> Box { + Box::new(Self {}) + } + fn content<'a>(&self, post: &'a Post) -> &'a str { &post.content } diff --git a/concepts/src/main.rs b/concepts/src/main.rs index f9eafe9..3e9bbbd 100644 --- a/concepts/src/main.rs +++ b/concepts/src/main.rs @@ -256,6 +256,12 @@ fn test_dyn_traits() { 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()); } -- 2.39.2