blog_rs/commands/
new_post.rs

1use std::fs;
2
3pub struct NewPost;
4
5impl NewPost {
6    fn template(title: &str) -> String {
7        format!(
8            r#"+++
9title: {}
10date: {}
11+++
12"#,
13            title,
14            chrono::Local::now().format("%Y-%m-%d")
15        )
16    }
17
18    pub fn run(&self, title: &str) -> crate::BlogResult<()> {
19        let template = NewPost::template(title);
20        fs::write(format!("./posts/{title}.md"), template)
21            .map_err(|e| crate::BlogError::io(e, "Unable to create new post ensure the /posts directory exits and you are in your root".to_string()))?;
22        println!("Created new post {title}.md");
23        Ok(())
24    }
25}