From a3463adee5cfcbc422886653787fee12e594d5f6 Mon Sep 17 00:00:00 2001 From: hybrid Date: Sat, 7 Nov 2026 05:53:30 +0300 Subject: add: streams, thread2async, oop, blog --- src/blog/Cargo.toml | 6 +++ src/blog/src/lib.rs | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/blog/src/main.rs | 21 ++++++++++ 3 files changed, 135 insertions(+) create mode 100644 src/blog/Cargo.toml create mode 100644 src/blog/src/lib.rs create mode 100644 src/blog/src/main.rs (limited to 'src/blog') diff --git a/src/blog/Cargo.toml b/src/blog/Cargo.toml new file mode 100644 index 0000000..790832e --- /dev/null +++ b/src/blog/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/blog/src/lib.rs b/src/blog/src/lib.rs new file mode 100644 index 0000000..c66e850 --- /dev/null +++ b/src/blog/src/lib.rs @@ -0,0 +1,108 @@ +pub struct Post { + state: Option>, + + content: String, +} + +impl Post { + pub fn new() -> Post { + Post { + state: Some(Box::new(Draft {})), + content: String::new(), + } + } + + 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 reject(&mut self) { + if let Some(s) = self.state.take() { + self.state = Some(s.reject()) + } + } +} + +trait State { + fn request_review(self: Box) -> Box; + fn approve(self: Box) -> Box; + fn reject(self: Box) -> Box; + fn content<'a>(&self, _post: &'a Post) -> &'a str { + "" + } +} + +struct Draft {} + +impl State for Draft { + fn request_review(self: Box) -> Box { + Box::new(PendingReview { approves: 0 }) + } + + fn approve(self: Box) -> Box { + self + } + + fn reject(self: Box) -> Box { + self + } +} + +struct PendingReview { + approves: u8, +} + +impl State for PendingReview { + fn request_review(self: Box) -> Box { + self + } + + fn approve(self: Box) -> Box { + match self.approves { + 0 => { + Box::new(PendingReview { approves: 1 }) + }, + _ => Box::new(Published {}), + } + } + + fn reject(self: Box) -> Box { + Box::new(Draft {}) + } +} + +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 + } + + fn reject(self: Box) -> Box { + self + } +} diff --git a/src/blog/src/main.rs b/src/blog/src/main.rs new file mode 100644 index 0000000..9b5563e --- /dev/null +++ b/src/blog/src/main.rs @@ -0,0 +1,21 @@ +use blog::Post; + +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()); + + post.reject(); + assert_eq!("", post.content()); + + post.request_review(); + assert_eq!("", post.content()); + + post.approve(); + post.approve(); + assert_eq!("I ate a salad for lunch today", post.content()); +} -- cgit v1.3.1