nargo_types/document/
mod.rs1use crate::{NargoValue, Span};
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
10pub struct Document {
11 pub meta: DocumentMeta,
13 pub frontmatter: FrontMatter,
15 pub content: String,
17 pub rendered_content: Option<String>,
19 pub span: Span,
21}
22
23impl Document {
24 pub fn new() -> Self {
26 Self::default()
27 }
28
29 pub fn with_path(mut self, path: String) -> Self {
31 self.meta.path = path;
32 self
33 }
34
35 pub fn with_frontmatter(mut self, frontmatter: FrontMatter) -> Self {
37 self.frontmatter = frontmatter;
38 self
39 }
40
41 pub fn with_content(mut self, content: String) -> Self {
43 self.content = content;
44 self
45 }
46
47 pub fn title(&self) -> Option<&str> {
49 self.frontmatter.title.as_deref().or_else(|| self.meta.title.as_deref())
50 }
51
52 pub fn description(&self) -> Option<&str> {
54 self.frontmatter.description.as_deref()
55 }
56
57 pub fn tags(&self) -> &[String] {
59 &self.frontmatter.tags
60 }
61
62 pub fn to_json(&self) -> serde_json::Result<String> {
64 serde_json::to_string(self)
65 }
66
67 pub fn to_json_pretty(&self) -> serde_json::Result<String> {
69 serde_json::to_string_pretty(self)
70 }
71}
72
73#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
75pub struct DocumentMeta {
76 pub path: String,
78 pub title: Option<String>,
80 pub lang: Option<String>,
82 pub last_updated: Option<i64>,
84 pub extra: HashMap<String, NargoValue>,
86}
87
88#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
90pub struct FrontMatter {
91 pub title: Option<String>,
93 pub description: Option<String>,
95 pub layout: Option<String>,
97 pub tags: Vec<String>,
99 pub sidebar: Option<bool>,
101 pub sidebar_order: Option<i32>,
103 pub custom: HashMap<String, NargoValue>,
105}
106
107impl FrontMatter {
108 pub fn new() -> Self {
110 Self::default()
111 }
112
113 pub fn with_title(mut self, title: String) -> Self {
115 self.title = Some(title);
116 self
117 }
118
119 pub fn with_description(mut self, description: String) -> Self {
121 self.description = Some(description);
122 self
123 }
124
125 pub fn with_layout(mut self, layout: String) -> Self {
127 self.layout = Some(layout);
128 self
129 }
130
131 pub fn add_tag(mut self, tag: String) -> Self {
133 self.tags.push(tag);
134 self
135 }
136}