1use std::collections::HashMap;
4
5use indexmap::IndexMap;
6
7use crate::parser::ParsedFile;
8use crate::schema::{FieldType, Schema};
9
10pub type Row = HashMap<String, Value>;
12
13#[derive(Debug, Clone, PartialEq)]
15pub enum Value {
16 Null,
17 String(String),
18 Int(i64),
19 Float(f64),
20 Bool(bool),
21 Date(chrono::NaiveDate),
22 DateTime(chrono::NaiveDateTime),
23 List(Vec<String>),
24 Dict(IndexMap<String, Value>),
25}
26
27impl Value {
28 pub fn as_str(&self) -> Option<&str> {
29 match self {
30 Value::String(s) => Some(s),
31 _ => None,
32 }
33 }
34
35 pub fn as_i64(&self) -> Option<i64> {
36 match self {
37 Value::Int(n) => Some(*n),
38 _ => None,
39 }
40 }
41
42 pub fn as_f64(&self) -> Option<f64> {
43 match self {
44 Value::Float(f) => Some(*f),
45 Value::Int(n) => Some(*n as f64),
46 _ => None,
47 }
48 }
49
50 pub fn as_bool(&self) -> Option<bool> {
51 match self {
52 Value::Bool(b) => Some(*b),
53 _ => None,
54 }
55 }
56
57 pub fn is_null(&self) -> bool {
58 matches!(self, Value::Null)
59 }
60
61 pub fn to_display_string(&self) -> String {
62 match self {
63 Value::Null => String::new(),
64 Value::String(s) => s.clone(),
65 Value::Int(n) => n.to_string(),
66 Value::Float(f) => format!("{}", f),
67 Value::Bool(b) => b.to_string(),
68 Value::Date(d) => d.format("%Y-%m-%d").to_string(),
69 Value::DateTime(dt) => dt.format("%Y-%m-%dT%H:%M:%S").to_string(),
70 Value::List(items) => items.join(", "),
71 Value::Dict(map) => {
72 let pairs: Vec<String> = map.iter()
73 .map(|(k, v)| format!("{}: {}", k, v.to_display_string()))
74 .collect();
75 format!("{{{}}}", pairs.join(", "))
76 }
77 }
78 }
79}
80
81impl std::fmt::Display for Value {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 write!(f, "{}", self.to_display_string())
84 }
85}
86
87impl PartialOrd for Value {
88 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
89 match (self, other) {
90 (Value::Int(a), Value::Int(b)) => a.partial_cmp(b),
91 (Value::Float(a), Value::Float(b)) => a.partial_cmp(b),
92 (Value::Int(a), Value::Float(b)) => (*a as f64).partial_cmp(b),
93 (Value::Float(a), Value::Int(b)) => a.partial_cmp(&(*b as f64)),
94 (Value::String(a), Value::String(b)) => a.partial_cmp(b),
95 (Value::Bool(a), Value::Bool(b)) => a.partial_cmp(b),
96 (Value::Date(a), Value::Date(b)) => a.partial_cmp(b),
97 (Value::DateTime(a), Value::DateTime(b)) => a.partial_cmp(b),
98 (Value::Date(a), Value::DateTime(b)) => a.and_hms_opt(0, 0, 0).unwrap().partial_cmp(b),
99 (Value::DateTime(a), Value::Date(b)) => a.partial_cmp(&b.and_hms_opt(0, 0, 0).unwrap()),
100 _ => self.to_display_string().partial_cmp(&other.to_display_string()),
102 }
103 }
104}
105
106pub fn yaml_to_value_pub(val: &serde_yaml::Value, field_type: Option<&FieldType>) -> Value {
108 yaml_to_value(val, field_type)
109}
110
111fn yaml_to_value(val: &serde_yaml::Value, field_type: Option<&FieldType>) -> Value {
112 match val {
113 serde_yaml::Value::Null => Value::Null,
114 serde_yaml::Value::Bool(b) => Value::Bool(*b),
115 serde_yaml::Value::Number(n) => {
116 if let Some(FieldType::Float) = field_type {
117 Value::Float(n.as_f64().unwrap_or(0.0))
118 } else if let Some(i) = n.as_i64() {
119 Value::Int(i)
120 } else if let Some(u) = n.as_u64() {
121 Value::Int(u as i64)
122 } else if let Some(f) = n.as_f64() {
123 Value::Float(f)
124 } else {
125 Value::Null
126 }
127 }
128 serde_yaml::Value::String(s) => {
129 if let Some(FieldType::DateTime) = field_type {
131 if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S") {
132 return Value::DateTime(dt);
133 }
134 if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S%.f") {
135 return Value::DateTime(dt);
136 }
137 }
138 if let Some(FieldType::Date) = field_type {
140 if let Ok(date) = chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d") {
141 return Value::Date(date);
142 }
143 }
144 Value::String(s.clone())
145 }
146 serde_yaml::Value::Sequence(seq) => {
147 let items: Vec<String> = seq
148 .iter()
149 .filter_map(|v| v.as_str().map(|s| s.to_string()))
150 .collect();
151 Value::List(items)
152 }
153 serde_yaml::Value::Mapping(mapping) => {
154 if let Some(FieldType::Dict) = field_type {
155 let mut dict = IndexMap::new();
156 for (k, v) in mapping {
157 if let Some(key) = k.as_str() {
158 dict.insert(key.to_string(), yaml_to_value(v, None));
159 }
160 }
161 Value::Dict(dict)
162 } else {
163 Value::String(format!("{:?}", val))
164 }
165 }
166 _ => Value::String(format!("{:?}", val)),
167 }
168}
169
170pub fn to_row(parsed: &ParsedFile, schema: &Schema) -> Row {
172 let mut row = Row::new();
173 row.insert("path".to_string(), Value::String(parsed.path.clone()));
174
175 if let Some(fm_map) = parsed.raw_frontmatter.as_mapping() {
177 for (key_val, value) in fm_map {
178 if let Some(key) = key_val.as_str() {
179 let field_type = schema.frontmatter.get(key).map(|fd| &fd.field_type)
180 .or_else(|| {
181 if crate::stamp::TIMESTAMP_FIELDS.contains(&key) {
182 Some(&FieldType::DateTime)
183 } else {
184 None
185 }
186 });
187 row.insert(key.to_string(), yaml_to_value(value, field_type));
188 }
189 }
190 }
191
192 for section in &parsed.sections {
194 row.insert(
195 section.normalized_heading.clone(),
196 Value::String(section.body.clone()),
197 );
198 }
199
200 row
201}
202
203#[cfg(test)]
204mod tests {
205 use super::*;
206 use crate::parser::parse_text;
207 use crate::schema::*;
208 use indexmap::IndexMap;
209
210 fn test_schema() -> Schema {
211 let mut frontmatter = IndexMap::new();
212 frontmatter.insert("title".to_string(), FieldDef {
213 field_type: FieldType::String,
214 required: true,
215 enum_values: None,
216 });
217 frontmatter.insert("count".to_string(), FieldDef {
218 field_type: FieldType::Int,
219 required: true,
220 enum_values: None,
221 });
222
223 Schema {
224 table: "test".to_string(),
225 primary_key: "path".to_string(),
226 frontmatter,
227 h1_required: false,
228 sections: IndexMap::new(),
229 rules: Rules {
230 reject_unknown_frontmatter: false,
231 reject_unknown_sections: false,
232 reject_duplicate_sections: true,
233 normalize_numbered_headings: false,
234 },
235 }
236 }
237
238 #[test]
239 fn test_to_row_basic() {
240 let text = "---\ntitle: \"Hello\"\ncount: 42\n---\n\n## Summary\n\nA summary.\n";
241 let parsed = parse_text(text, "test.md", false);
242 let row = to_row(&parsed, &test_schema());
243 assert_eq!(row["path"], Value::String("test.md".into()));
244 assert_eq!(row["title"], Value::String("Hello".into()));
245 assert_eq!(row["count"], Value::Int(42));
246 assert_eq!(row["Summary"], Value::String("A summary.".into()));
247 }
248
249 #[test]
250 fn test_h1_not_in_row() {
251 let text = "---\ntitle: \"Test\"\ncount: 1\n---\n\n# My Title\n\n## Section\n\nBody.\n";
252 let parsed = parse_text(text, "test.md", false);
253 let row = to_row(&parsed, &test_schema());
254 assert!(!row.contains_key("h1"));
255 }
256
257 #[test]
258 fn test_date_coercion() {
259 let mut frontmatter = IndexMap::new();
260 frontmatter.insert("created".to_string(), FieldDef {
261 field_type: FieldType::Date,
262 required: true,
263 enum_values: None,
264 });
265
266 let schema = Schema {
267 table: "test".to_string(),
268 primary_key: "path".to_string(),
269 frontmatter,
270 h1_required: false,
271 sections: IndexMap::new(),
272 rules: Rules {
273 reject_unknown_frontmatter: false,
274 reject_unknown_sections: false,
275 reject_duplicate_sections: true,
276 normalize_numbered_headings: false,
277 },
278 };
279
280 let text = "---\ncreated: \"2026-04-04\"\n---\n";
281 let parsed = parse_text(text, "test.md", false);
282 let row = to_row(&parsed, &schema);
283 assert_eq!(
284 row["created"],
285 Value::Date(chrono::NaiveDate::from_ymd_opt(2026, 4, 4).unwrap())
286 );
287 }
288}