1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::engine::Engine;
use crate::Pod;
use std::collections::HashMap;
use toml::Value;

/// [`Engine`](crate::engine::Engine) for the [TOML](https://toml.io/) configuration format.
pub struct TOML;

impl Engine for TOML {
    fn parse(content: &str) -> Pod {
        match toml::from_str::<Value>(content) {
            Ok(value) => value.into(),
            Err(_) => Pod::Null,
        }
    }
}

impl From<Value> for Pod {
    fn from(toml_value: Value) -> Self {
        match toml_value {
            Value::String(val) => Pod::String(val),
            Value::Integer(val) => Pod::Integer(val),
            Value::Float(val) => Pod::Float(val),
            Value::Boolean(val) => Pod::Boolean(val),
            Value::Array(val) => val
                .iter()
                .map(|elem| elem.into())
                .collect::<Vec<Pod>>()
                .into(),
            Value::Table(val) => val
                .iter()
                .map(|(key, elem)| (key.to_owned(), elem.into()))
                .collect::<HashMap<String, Pod>>()
                .into(),
            Value::Datetime(val) => Pod::String(val.to_string()),
        }
    }
}

impl From<&Value> for Pod {
    fn from(val: &Value) -> Self {
        val.to_owned().into()
    }
}

#[cfg(test)]
mod test {
    use crate::engine::toml::TOML;
    use crate::entity::ParsedEntityStruct;
    use crate::matter::Matter;
    use serde::Deserialize;

    #[test]
    fn test_matter() {
        let matter: Matter<TOML> = Matter::new();
        let input = r#"---
title = "TOML"
description = "Front matter"
categories = "front matter toml"
---

# This file has toml front matter!
"#;
        #[derive(Deserialize, PartialEq, Debug)]
        struct FrontMatter {
            title: String,
            description: String,
            categories: String,
        }
        let data_expected = FrontMatter {
            title: "TOML".to_string(),
            description: "Front matter".to_string(),
            categories: "front matter toml".to_string(),
        };
        let result: ParsedEntityStruct<FrontMatter> = matter.parse_with_struct(input).unwrap();
        assert_eq!(result.data, data_expected);
    }
}