In chapter 17 of the book there is an exercise to turn the verbose OOP program into one without encapsulation and with type checking at compile time
https://doc.rust-lang.org/nightly/book/ch17-03-oo-design-patterns.html
This is what I've done, but I get a warning. I can get rid of the warning by making request_review private, but I don't understand why the warning appears only in that function, when other functions also expose a private field.
warning: private type PendingReviewPost in public interface (error E0446)
pub struct Post {
content: String,
}
pub struct DraftPost {
content: String,
}
struct PendingReviewPost {
content: String,
}
impl Post {
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 request_review(self) -> PendingReviewPost {
PendingReviewPost {
content: self.content,
}
}
}
impl PendingReviewPost {
pub fn approve(self) -> Post {
Post {
content: self.content,
}
}
}
let mut post = Post::new();
post.add_text("I ate a salad for lunch today");
let post = post.request_review();
let post = post.approve();
assert_eq!("I ate a salad for lunch today", post.content());
Edit: fixed code formatting