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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// a:p
use super::ParagraphProperties;
use super::Run;
use super::RunProperties;
use quick_xml::events::{BytesStart, Event};
use quick_xml::Reader;
use quick_xml::Writer;
use reader::driver::*;
use std::io::Cursor;
use writer::driver::*;

#[derive(Clone, Default, Debug)]
pub struct Paragraph {
    paragraph_properties: ParagraphProperties,
    run: Vec<Run>,
    end_para_run_properties: Option<RunProperties>,
}

impl Paragraph {
    pub fn get_paragraph_properties(&self) -> &ParagraphProperties {
        &self.paragraph_properties
    }

    pub fn get_paragraph_properties_mut(&mut self) -> &mut ParagraphProperties {
        &mut self.paragraph_properties
    }

    pub fn set_paragraph_properties(&mut self, value: ParagraphProperties) -> &mut Paragraph {
        self.paragraph_properties = value;
        self
    }

    pub fn get_run(&self) -> &Vec<Run> {
        &self.run
    }

    pub fn add_run(&mut self, value: Run) {
        self.run.push(value);
    }

    pub fn get_end_para_run_properties(&self) -> Option<&RunProperties> {
        self.end_para_run_properties.as_ref()
    }

    pub fn get_end_para_run_properties_mut(&mut self) -> Option<&mut RunProperties> {
        self.end_para_run_properties.as_mut()
    }

    pub fn set_end_para_run_properties(&mut self, value: RunProperties) -> &mut Paragraph {
        self.end_para_run_properties = Some(value);
        self
    }

    pub fn remove_end_para_run_properties(&mut self) {
        self.end_para_run_properties = None;
    }

    pub(crate) fn set_attributes<R: std::io::BufRead>(
        &mut self,
        reader: &mut Reader<R>,
        _e: &BytesStart,
    ) {
        xml_read_loop!(
            reader,
            Event::Start(ref e) => {
                match e.name().into_inner() {
                    b"a:pPr" => {
                        self.paragraph_properties.set_attributes(reader, e, false);
                    }
                    b"a:r" => {
                        let mut run = Run::default();
                        run.set_attributes(reader, e);
                        self.add_run(run);
                    }
                    b"a:endParaRPr" => {
                        let mut run_properties = RunProperties::default();
                        run_properties.set_attributes(reader, e, false);
                        self.set_end_para_run_properties(run_properties);
                    }
                    _ => (),
                }
            },
            Event::Empty(ref e) => {
                match e.name().into_inner() {
                    b"a:pPr" => {
                        self.paragraph_properties.set_attributes(reader, e, true);
                    }
                    b"a:endParaRPr" => {
                        let mut run_properties = RunProperties::default();
                        run_properties.set_attributes(reader, e, true);
                        self.set_end_para_run_properties(run_properties);
                    }
                    _ => (),
                }
            },
            Event::End(ref e) => {
                if e.name().into_inner() == b"a:p" {
                    return;
                }
            },
            Event::Eof => panic!("Error: Could not find {} end element", "a:p")
        );
    }

    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
        // a:p
        write_start_tag(writer, "a:p", vec![], false);

        // a:pPr
        self.paragraph_properties.write_to(writer);

        // a:r
        for run in &self.run {
            run.write_to(writer);
        }

        // a:endParaRPr
        if let Some(v) = &self.end_para_run_properties {
            v.write_to_end_para_rpr(writer);
        }

        write_end_tag(writer, "a:p");
    }
}