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 {
// don't change state
Box::new(Self {})
}
+
+ fn reject(&self) -> Box<dyn State> {
+ Box::new(Self {})
+ }
}
struct PendingReview {}
fn approve(&self) -> Box<dyn State> {
Box::new(Published {})
}
+
+ fn reject(&self) -> Box<dyn State> {
+ Box::new(Draft {})
+ }
}
struct Published {}
Box::new(Published {})
}
+ fn reject(&self) -> Box<dyn State> {
+ Box::new(Self {})
+ }
+
fn content<'a>(&self, post: &'a Post) -> &'a str {
&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());
}