1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use std::fs;

pub struct NewPost;

impl NewPost {
    fn template(title: &str) -> String {
        format!(
            r#"+++
title: {}
date: {}
+++
"#,
            title,
            chrono::Local::now().format("%Y-%m-%d")
        )
    }

    pub fn run(&self, title: &str) -> crate::BlogResult<()> {
        let template = NewPost::template(title);
        fs::write(format!("./posts/{title}.md"), template)
            .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()))?;
        println!("Created new post {title}.md");
        Ok(())
    }
}