use serde::{Deserialize, Serialize};
use server_less::cli;
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Comment {
pub id: String,
pub body: String,
}
impl std::fmt::Display for Comment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.id, self.body)
}
}
#[derive(Clone, Default)]
pub struct CommentService;
#[cli(name = "comments", version = "0.1.0")]
impl CommentService {
pub fn list(&self) -> Vec<Comment> {
vec![Comment {
id: "c1".to_string(),
body: "first!".to_string(),
}]
}
pub fn add(&self, body: String) -> Comment {
Comment {
id: "c2".to_string(),
body,
}
}
}
#[derive(Clone, Default)]
pub struct PostService {
comments: CommentService,
}
#[cli(name = "posts", version = "0.1.0")]
impl PostService {
pub fn list(&self) -> Vec<String> {
vec!["hello-world".to_string()]
}
pub fn create(&self, title: String) -> String {
format!("created: {title}")
}
pub fn comments(&self) -> &CommentService {
&self.comments
}
}
#[derive(Clone, Default)]
pub struct BlogApp {
posts: PostService,
}
#[cli(name = "blog", version = "0.1.0", description = "A tiny blog CLI")]
impl BlogApp {
pub fn health(&self) -> String {
"ok".to_string()
}
pub fn posts(&self) -> &PostService {
&self.posts
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let app = BlogApp::default();
app.cli_run()
}