Skip to main content

easy_docx_template/
lib.rs

1mod docx;
2mod utils;
3mod placeholder_helpers;
4
5pub use docx::*;
6
7#[cfg(test)]
8mod tests {
9    use super::*;
10
11    #[test]
12    fn test_with_json() {
13        // 1. Loading docx file
14        let mut docx: DOCX = DOCX::new("example/test.docx".to_string());
15        docx.read();
16
17        // 2. Adding placeholders
18        docx.add_placeholders_from_json(r#"{
19        "exam": {
20                "level": "form 2-A",
21                "variant": "1 variant",
22                "title": "Math exam",
23                "subject": "math",
24                "image_subtitle": "Hello world!",
25                "users": [
26                    { "first": "Ivan", "last": "Ivanov" },
27                    { "first": "Petr", "last": "Petrov" }
28                ],
29                "down": "made with love",
30                "test1": "2424"
31            }
32        }"#);
33
34        // 4. Add image placeholder
35        docx.add_image_placeholder("image1.jpeg", "example/replace_image1.png");
36
37        // 5. Init placeholders
38        docx.init_placeholders();
39
40        // 6. Save our docx file
41        docx.save("output.docx");
42        println!("✅ File saved: output.docx")
43    }
44
45    #[test]
46    fn test_default() {
47        // 1. Loading docx file
48        let mut docx = DOCX::new("example/test.docx".to_string());
49        docx.read();
50
51        // 2. Adding placeholders
52        docx.add_placeholder("{{exam.title}}", "Math exam");
53        docx.add_placeholder("{{exam.variant}}", "1 variant");
54        docx.add_placeholder("{{exam.subject}}", "Math");
55        docx.add_placeholder("{{exam.level}}", "1-A form");
56        docx.add_placeholder("{{exam.image_subtitle}}", "Hello world!");
57        docx.add_placeholder("{{exam.down}}", "made with love");
58
59        // 3. Add image placeholder
60        docx.add_image_placeholder("image1.jpeg", "example/replace_image1.png");
61
62        // 4. Init placeholders
63        docx.init_placeholders();
64
65        // 5. Save our docx file
66        docx.save("output.docx");
67        println!("✅ File saved: output.docx")
68    }
69
70    #[test]
71    fn test_with_each_blocks() {
72        let mut docx: DOCX = DOCX::new("example/test.docx".to_string());
73        docx.read();
74        docx.add_placeholders_from_json(r#"{
75        "exam": {
76                "level": "form 2-A",
77                "variant": "1 variant",
78                "title": "Math exam",
79                "subject": "math",
80                "image_subtitle": "Hello world!",
81                  "users": [
82                    {
83                      "name": "Alice",
84                      "age": 28,
85                      "pets": [
86                        {
87                          "type": "Cat",
88                          "name": "Misty",
89                          "toys": [
90                            { "title": "Ball" },
91                            { "title": "Mouse" }
92                          ]
93                        },
94                        {
95                          "type": "Dog",
96                          "name": "Rex",
97                          "toys": [
98                            { "title": "Bone" }
99                          ]
100                        }
101                      ]
102                    },
103                    {
104                      "name": "Bob",
105                      "age": 34,
106                      "pets": [
107                        {
108                          "type": "Fish",
109                          "name": "Nemo",
110                          "toys": []
111                        }
112                      ]
113                    }
114                  ],
115                "options": ["okak", "bubu"],
116                "down": "made with love"
117            }
118        }"#);
119        // 5. Init placeholders
120        docx.init_placeholders();
121
122        // 6. Save our docx file
123        docx.save("output.docx");
124        println!("✅ File saved: output.docx")
125    }
126}