X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=concepts%2Fsrc%2Flib.rs;fp=concepts%2Fsrc%2Flib.rs;h=5f1feea9404c8116e1cbb1106b825e5a6d52c677;hp=b0839ad391739cb186adcf2bb81c97484c5a3dfe;hb=d6fbfabfaf434ae365854f0587547cff3ca78dae;hpb=1962a5412d16f34c738e9f81bb5271da043a7505 diff --git a/concepts/src/lib.rs b/concepts/src/lib.rs index b0839ad..5f1feea 100644 --- a/concepts/src/lib.rs +++ b/concepts/src/lib.rs @@ -210,3 +210,46 @@ impl State for Published { &post.content } } + +// state encoded as types; this is the "approved" state +pub struct TPost { + content: String, +} + +impl TPost { + pub fn new() -> TPostDraft { + TPostDraft {content: String::new()} + } + + pub fn content(&self) -> &str { + &self.content + } +} + +pub struct TPostDraft { + content: String, +} + +impl TPostDraft { + pub fn add_text(&mut self, text: &str) { + self.content.push_str(text); + } + + pub fn request_review(self) -> TPostReview { + TPostReview {content: self.content} + } +} + +pub struct TPostReview { + content: String, +} + +impl TPostReview { + pub fn approve(self) -> TPost { + TPost {content: self.content} + } + + pub fn reject(self) -> TPostDraft { + TPostDraft {content: self.content} + } +}