Skip to main content

opys_engine/
doc.rs

1//! A single inventory document in memory — the unified representation of every
2//! configured type. This collapses the former `Feature` and `WorkItem`, which
3//! were identical `{path, frontmatter, body, title}` structs; a doc's *type* is
4//! derived from its ID prefix via the config, not stored here.
5
6use 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    /// Parse a file's text into a `Doc`, or return the parse-error message.
21    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    /// Serialized file text (canonical frontmatter + body).
42    pub fn to_text(&self) -> String {
43        frontmatter::serialize(&self.frontmatter, &self.body)
44    }
45}