diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1f70177..5d9f874 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,8 @@ - - - + + @@ -217,7 +200,7 @@ - + @@ -822,37 +805,21 @@ - - + + - - - - - - - - - - - - - - - - - - - - + + + + - - + + diff --git a/blog/Cargo.toml b/blog/Cargo.toml index c299135..8d5df54 100644 --- a/blog/Cargo.toml +++ b/blog/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "blog" -version = "0.1.0" +version = "0.2.0" authors = ["Timothy Warren "] edition = "2018" diff --git a/blog/src/lib.rs b/blog/src/lib.rs index 87947cf..a07ef8c 100644 --- a/blog/src/lib.rs +++ b/blog/src/lib.rs @@ -1,81 +1,44 @@ pub struct Post { - state: Option>, + content: String, +} + +pub struct DraftPost { content: String, } impl Post { - pub fn new() -> Post { - Post { - state: Some(Box::new(Draft {})), + pub fn new() -> DraftPost { + DraftPost { content: String::new(), } } + pub fn content(&self) -> &str { + &self.content + } +} + +impl DraftPost { pub fn add_text(&mut self, text: &str) { self.content.push_str(text); } - pub fn content(&self) -> &str { - self.state.as_ref().unwrap().content(&self) - } - - pub fn request_review(&mut self) { - if let Some(s) = self.state.take() { - self.state = Some(s.request_review()) - } - } - - pub fn approve(&mut self) { - if let Some(s) = self.state.take() { - self.state = Some(s.approve()) + pub fn request_review(self) -> PendingReviewPost { + PendingReviewPost { + content: self.content, } } } -trait State { - fn request_review(self: Box) -> Box; - fn approve(self: Box) -> Box; - fn content<'a>(&self, post: &'a Post) -> &'a str { - "" +pub struct PendingReviewPost { + content: String, +} + +impl PendingReviewPost { + pub fn approve(self) -> Post { + Post { + content: self.content, + } } } -struct Draft {} - -impl State for Draft { - fn request_review(self: Box) -> Box { - Box::new(PendingReview {}) - } - - fn approve(self: Box) -> Box { - self - } -} - -struct PendingReview {} - -impl State for PendingReview { - fn request_review(self: Box) -> Box { - self - } - - fn approve(self: Box) -> Box { - Box::new(Published {}) - } -} - -struct Published {} - -impl State for Published { - fn request_review(self: Box) -> Box { - self - } - - fn approve(self: Box) -> Box { - self - } - - fn content<'a>(&self, post: &'a Post) -> &'a str { - &post.content - } -} diff --git a/blog/src/main.rs b/blog/src/main.rs index 14b4c08..720c55e 100644 --- a/blog/src/main.rs +++ b/blog/src/main.rs @@ -4,11 +4,10 @@ fn main() { let mut post = Post::new(); post.add_text("I ate a salad for lunch today"); - assert_eq!("", post.content()); - post.request_review(); - assert_eq!("", post.content()); + let post = post.request_review(); + + let post = post.approve(); - post.approve(); assert_eq!("I ate a salad for lunch today", post.content()); }