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
use std::{collections::HashMap, path::Path};

use crate::{Docx, DocxResult, document::{Paragraph, Run}};

pub enum Part<'a> {
    Paragraph(&'a mut Paragraph<'a>),
    Run(&'a mut Run<'a>),
}

pub struct MergeGroups<'a> {
    pub name: String,
    pub contents: Vec<Part<'a>>
}

pub fn mail_merge<P>(template: &Docx, _map: HashMap<String, String>, path: P) -> DocxResult<()>
where
    P: AsRef<Path>,
{
    let mut docx = template.clone();
    let _dmap: HashMap<String, Vec<Part>> = HashMap::new();
    let is_merge_field = false;
    for c in docx.document.body.content.iter() {
        match c {
            crate::document::BodyContent::Paragraph(p) => {
                if ! is_merge_field {
                    let mut iter = p.content.iter().skip_while(|pc| {
                        if let crate::document::ParagraphContent::Run(r) = pc {
                            let mut v = true;
                            for rc in r.content.iter() {
                                if let crate::document::RunContent::FieldChar(fc) = rc {
                                    if let Some(ct) = &fc.ty {
                                        if let crate::document::CharType::Begin = ct {
                                            v =false;
                                            break;
                                        }
                                    }
                                };
                            };
                            v
                        } else {
                            true
                        }
                    });

                    if let Some(_pc) = iter.next() {

                    }
                } else {
                    // if it's close part, add merged content. Otherwise, ignore it.
                }
            },
            crate::document::BodyContent::Table(_t) => {}
            crate::document::BodyContent::SectionProperty(_) => {}
            crate::document::BodyContent::Sdt(_) => {}
        }
    }
    let _f = docx.write_file(path)?;
    Ok(())
}