pub struct User {
pub id: u64,
pub name: String,
pub email: String,
}
pub struct Post {
pub id: u64,
pub title: String,
pub body: String,
pub author_id: u64,
}
pub enum Role {
Admin,
Moderator,
User,
}
impl Role {
pub fn can_delete(&self) -> bool {
match self {
Role::Admin => true,
Role::Moderator => true,
Role::User => false,
}
}
pub fn can_edit(&self) -> bool {
matches!(self, Role::Admin | Role::Moderator)
}
}
pub trait JsonSerializable {
fn to_json(&self) -> String;
}
impl JsonSerializable for User {
fn to_json(&self) -> String {
format!("{{ \"id\": {}, \"name\": \"{}\" }}", self.id, self.name)
}
}