terdoc_types/
template.rs

1// SPDX-FileCopyrightText: OpenTalk GmbH <mail@opentalk.eu>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use serde::{Deserialize, Serialize};
5
6use crate::InputFormat;
7
8/// The description of a template
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct Template {
11    /// Content of the template, including the placeholders
12    pub content: String,
13
14    /// The format of the template
15    pub format: InputFormat,
16
17    /// Flag indicating whether to perform autoescaping on the template
18    #[serde(default, skip_serializing_if = "crate::utils::is_default")]
19    pub autoescape: bool,
20}
21
22#[cfg(test)]
23mod tests {
24    use pretty_assertions::assert_eq;
25    use serde_json::json;
26
27    use super::*;
28
29    #[test]
30    fn serialize_roundtrip() {
31        let template = Template {
32            content: "Hello world".to_string(),
33            format: InputFormat::Markdown,
34            autoescape: true,
35        };
36
37        let json = json!({
38            "content": "Hello world",
39            "format": "markdown",
40            "autoescape": true
41        });
42
43        assert_eq!(json, serde_json::to_value(&template).unwrap());
44        assert_eq!(template, serde_json::from_value(json).unwrap());
45    }
46
47    #[test]
48    fn deserialize_with_default() {
49        let template = Template {
50            content: "Hello world".to_string(),
51            format: InputFormat::Markdown,
52            autoescape: false,
53        };
54
55        let json = json!({
56            "content": "Hello world",
57            "format": "markdown",
58        });
59
60        assert_eq!(template, serde_json::from_value(json).unwrap());
61    }
62}