1use std::path::PathBuf;
7
8use crate::body;
9use crate::frontmatter::{self, Frontmatter};
10
11#[derive(Debug, Clone)]
12pub struct Doc {
13 pub path: PathBuf,
14 pub frontmatter: Frontmatter,
15 pub body: String,
16 pub title: String,
17}
18
19impl Doc {
20 pub fn parse(path: PathBuf, text: &str) -> Result<Doc, String> {
22 let display = path.display().to_string();
23 let (frontmatter, body) = frontmatter::parse(text, &display).map_err(|e| e.0)?;
24 let title = body::title(&body);
25 Ok(Doc {
26 path,
27 frontmatter,
28 body,
29 title,
30 })
31 }
32
33 pub fn id(&self) -> Option<&str> {
34 self.frontmatter.id()
35 }
36
37 pub fn status(&self) -> Option<&str> {
38 self.frontmatter.status()
39 }
40
41 pub fn to_text(&self) -> String {
43 frontmatter::serialize(&self.frontmatter, &self.body)
44 }
45}