summaryrefslogtreecommitdiffstats
path: root/src/blog
diff options
context:
space:
mode:
authorhybrid <hybrid@hybridlabs.cc>2026-11-07 05:53:30 +0300
committerhybrid <hybrid@hybridlabs.cc>2026-11-07 05:53:30 +0300
commita3463adee5cfcbc422886653787fee12e594d5f6 (patch)
tree10befc5aa83ab2d1abb09862d70cfeef4d82d55d /src/blog
parent62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2 (diff)
downloadrust-book-a3463adee5cfcbc422886653787fee12e594d5f6.tar.gz
rust-book-a3463adee5cfcbc422886653787fee12e594d5f6.tar.bz2
rust-book-a3463adee5cfcbc422886653787fee12e594d5f6.zip
add: streams, thread2async, oop, blog
Diffstat (limited to 'src/blog')
-rw-r--r--src/blog/Cargo.toml6
-rw-r--r--src/blog/src/lib.rs108
-rw-r--r--src/blog/src/main.rs21
3 files changed, 135 insertions, 0 deletions
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<Box<dyn State>>,
+
+ 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<Self>) -> Box<dyn State>;
+ fn approve(self: Box<Self>) -> Box<dyn State>;
+ fn reject(self: Box<Self>) -> Box<dyn State>;
+ fn content<'a>(&self, _post: &'a Post) -> &'a str {
+ ""
+ }
+}
+
+struct Draft {}
+
+impl State for Draft {
+ fn request_review(self: Box<Self>) -> Box<dyn State> {
+ Box::new(PendingReview { approves: 0 })
+ }
+
+ fn approve(self: Box<Self>) -> Box<dyn State> {
+ self
+ }
+
+ fn reject(self: Box<Self>) -> Box<dyn State> {
+ self
+ }
+}
+
+struct PendingReview {
+ approves: u8,
+}
+
+impl State for PendingReview {
+ fn request_review(self: Box<Self>) -> Box<dyn State> {
+ self
+ }
+
+ fn approve(self: Box<Self>) -> Box<dyn State> {
+ match self.approves {
+ 0 => {
+ Box::new(PendingReview { approves: 1 })
+ },
+ _ => Box::new(Published {}),
+ }
+ }
+
+ fn reject(self: Box<Self>) -> Box<dyn State> {
+ Box::new(Draft {})
+ }
+}
+
+struct Published {}
+
+impl State for Published {
+ fn request_review(self: Box<Self>) -> Box<dyn State> {
+ self
+ }
+
+ fn approve(self: Box<Self>) -> Box<dyn State> {
+ self
+ }
+
+ fn content<'a>(&self, post: &'a Post) -> &'a str {
+ &post.content
+ }
+
+ fn reject(self: Box<Self>) -> Box<dyn State> {
+ 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());
+}