terdoc_types/
render_task.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::{OutputFormat, Template};
7
8/// Data that is required to execute a rendering.
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct TaskData {
11    /// The output format of the rendered report
12    #[serde(default, skip_serializing_if = "crate::utils::is_default")]
13    pub output: OutputFormat,
14
15    /// The data which will be rendered into the template
16    pub data: serde_json::Value,
17
18    /// The template description
19    pub template: Template,
20}
21
22#[cfg(test)]
23mod tests {
24    use pretty_assertions::assert_eq;
25    use serde_json::json;
26
27    use super::*;
28    use crate::InputFormat;
29
30    #[test]
31    fn serialize_roundtrip_no_defaults() {
32        let template = Template {
33            content: "Hello {{ addressed }}".to_string(),
34            format: InputFormat::Markdown,
35            autoescape: false,
36        };
37
38        let query = TaskData {
39            data: json!({"addressed": "world"}),
40            template,
41            output: OutputFormat::Docx {
42                theme: Some("dark".to_owned().try_into().unwrap()),
43            },
44        };
45
46        let json = json!({
47            "data": {
48                "addressed": "world",
49            },
50            "template": {
51                "content": "Hello {{ addressed }}",
52                "format": "markdown",
53            },
54            "output": {
55                "format": "docx",
56                "theme": "dark",
57            }
58        });
59
60        assert_eq!(json, serde_json::to_value(&query).unwrap());
61        assert_eq!(query, serde_json::from_value(json).unwrap());
62    }
63
64    #[test]
65    fn serialize_roundtrip_skipped_defaults() {
66        let template = Template {
67            content: "Hello {{ addressed }}".to_string(),
68            format: InputFormat::Markdown,
69            autoescape: false,
70        };
71
72        let query = TaskData {
73            data: json!({"addressed": "world"}),
74            template,
75            output: Default::default(),
76        };
77
78        let json = json!({
79            "data": {
80                "addressed": "world",
81            },
82            "template": {
83                "content": "Hello {{ addressed }}",
84                "format": "markdown",
85            },
86        });
87
88        assert_eq!(json, serde_json::to_value(&query).unwrap());
89        assert_eq!(query, serde_json::from_value(json).unwrap());
90    }
91}