X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=concepts%2Fsrc%2Flib.rs;h=923b80b451cbe86402e4c649859da5705c11d878;hp=bb15eeb814ea45d9aee64ed43497f586df33ba65;hb=6cf2fd87f634bb24ca0ab801a8ba3fbfff1ac418;hpb=c2e012ab539dff44f0690e5d7cab4880400454fe 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 }