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                    { "first": "Ivan", "last": "Ivanov" },
83                    { "first": "Petr", "last": "Petrov" }
84                ],
85                "down": "made with love"
86            }
87        }"#);
88        // 5. Init placeholders
89        docx.init_placeholders();
90
91        // 6. Save our docx file
92        docx.save("output.docx");
93        println!("✅ File saved: output.docx")
94    }
95}