metamuffin's personal website


About 1741753982690 milliseconds have passed since midnight of the january the first in 1970.
------------------------------------------------------
pub struct ArticleMeta {
    pub title: String,
    pub canonical_name: String,
    pub date: iso8601::Date,
}

pub async fn article_metadata(path: PathBuf) -> anyhow::Result<ArticleMeta> {
    let f = File::open(&path).await.context("article not found")?;
    let mut f = BufReader::new(f);
    let mut buf = String::new();
    f.read_line(&mut buf).await.context("cant read the file")?; // assume the 1st line has the title
    Ok(ArticleMeta {
        title: String::from(buf[2..].trim()),
        canonical_name: path
            .file_stem()
            .ok_or(anyhow!("no file stem"))?
            .to_str()
            .ok_or(anyhow!("this file's name is broken"))?
            .to_string(),
        date: iso8601::date(
            &path
                .file_name()
                .ok_or(anyhow!("file has no name"))?
                .to_str()
                .ok_or(anyhow!("this file's name is broken"))?[0..10],
        )
        .map_err(|e| anyhow!("file date wrong: {e}"))?,
    })
}