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
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-FileCopyrightText: OpenTalk GmbH <mail@opentalk.eu>
// SPDX-License-Identifier: MIT OR Apache-2.0

use serde::{Deserialize, Serialize};

use crate::{OutputFormat, Template};

/// Data that is required to execute a rendering.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TaskData {
    /// The output format of the rendered report
    #[serde(default, skip_serializing_if = "crate::utils::is_default")]
    pub output: OutputFormat,

    /// The data which will be rendered into the template
    pub data: serde_json::Value,

    /// The template description
    pub template: Template,
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;
    use serde_json::json;

    use super::*;
    use crate::InputFormat;

    #[test]
    fn serialize_roundtrip_no_defaults() {
        let template = Template {
            content: "Hello {{ addressed }}".to_string(),
            format: InputFormat::Markdown,
            autoescape: false,
        };

        let query = TaskData {
            data: json!({"addressed": "world"}),
            template,
            output: OutputFormat::Docx {
                theme: Some("dark".to_owned().try_into().unwrap()),
            },
        };

        let json = json!({
            "data": {
                "addressed": "world",
            },
            "template": {
                "content": "Hello {{ addressed }}",
                "format": "markdown",
            },
            "output": {
                "format": "docx",
                "theme": "dark",
            }
        });

        assert_eq!(json, serde_json::to_value(&query).unwrap());
        assert_eq!(query, serde_json::from_value(json).unwrap());
    }

    #[test]
    fn serialize_roundtrip_skipped_defaults() {
        let template = Template {
            content: "Hello {{ addressed }}".to_string(),
            format: InputFormat::Markdown,
            autoescape: false,
        };

        let query = TaskData {
            data: json!({"addressed": "world"}),
            template,
            output: Default::default(),
        };

        let json = json!({
            "data": {
                "addressed": "world",
            },
            "template": {
                "content": "Hello {{ addressed }}",
                "format": "markdown",
            },
        });

        assert_eq!(json, serde_json::to_value(&query).unwrap());
        assert_eq!(query, serde_json::from_value(json).unwrap());
    }
}