1use serde::{Deserialize, Serialize};
5
6use crate::InputFormat;
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct Template {
11 pub content: String,
13
14 pub format: InputFormat,
16
17 #[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}