docx_rs/documents/
numberings.rs

1use super::*;
2use crate::documents::BuildXML;
3use crate::types::*;
4use crate::xml_builder::*;
5use std::io::Write;
6
7use serde::Serialize;
8
9#[derive(Debug, Clone, PartialEq, Serialize, Default)]
10#[serde(rename_all = "camelCase")]
11pub struct Numberings {
12    pub abstract_nums: Vec<AbstractNumbering>,
13    pub numberings: Vec<Numbering>,
14}
15
16impl Numberings {
17    pub fn new() -> Self {
18        Default::default()
19    }
20
21    pub fn add_abstract_numbering(mut self, n: AbstractNumbering) -> Self {
22        self.abstract_nums.push(n);
23        self
24    }
25
26    pub fn add_numbering(mut self, n: Numbering) -> Self {
27        self.numberings.push(n);
28        self
29    }
30}
31
32impl BuildXML for Numberings {
33    fn build_to<W: Write>(
34        &self,
35        stream: xml::writer::EventWriter<W>,
36    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
37        XMLBuilder::from(stream)
38            .declaration(Some(true))?
39            .open_numbering()?
40            .add_child(&create_default_numbering())?
41            .add_children(&self.abstract_nums)?
42            .add_child(&Numbering::new(1, 1))?
43            .add_children(&self.numberings)?
44            .close()?
45            .into_inner()
46    }
47}
48
49fn create_default_numbering() -> AbstractNumbering {
50    AbstractNumbering::new(1)
51        .add_level(
52            Level::new(
53                0,
54                Start::new(1),
55                NumberFormat::new("decimal"),
56                LevelText::new("%1."),
57                LevelJc::new("left"),
58            )
59            .indent(Some(420), Some(SpecialIndentType::Hanging(420)), None, None),
60        )
61        .add_level(
62            Level::new(
63                1,
64                Start::new(1),
65                NumberFormat::new("decimal"),
66                LevelText::new("(%2)"),
67                LevelJc::new("left"),
68            )
69            .indent(Some(840), Some(SpecialIndentType::Hanging(420)), None, None),
70        )
71        .add_level(
72            Level::new(
73                2,
74                Start::new(1),
75                NumberFormat::new("decimalEnclosedCircle"),
76                LevelText::new("%3"),
77                LevelJc::new("left"),
78            )
79            .indent(
80                Some(1260),
81                Some(SpecialIndentType::Hanging(420)),
82                None,
83                None,
84            ),
85        )
86        .add_level(
87            Level::new(
88                3,
89                Start::new(1),
90                NumberFormat::new("decimal"),
91                LevelText::new("%4."),
92                LevelJc::new("left"),
93            )
94            .indent(
95                Some(1680),
96                Some(SpecialIndentType::Hanging(420)),
97                None,
98                None,
99            ),
100        )
101        .add_level(
102            Level::new(
103                4,
104                Start::new(1),
105                NumberFormat::new("decimal"),
106                LevelText::new("(%5)"),
107                LevelJc::new("left"),
108            )
109            .indent(
110                Some(2100),
111                Some(SpecialIndentType::Hanging(420)),
112                None,
113                None,
114            ),
115        )
116        .add_level(
117            Level::new(
118                5,
119                Start::new(1),
120                NumberFormat::new("decimalEnclosedCircle"),
121                LevelText::new("%6"),
122                LevelJc::new("left"),
123            )
124            .indent(
125                Some(2520),
126                Some(SpecialIndentType::Hanging(420)),
127                None,
128                None,
129            ),
130        )
131        .add_level(
132            Level::new(
133                6,
134                Start::new(1),
135                NumberFormat::new("decimal"),
136                LevelText::new("%7."),
137                LevelJc::new("left"),
138            )
139            .indent(
140                Some(2940),
141                Some(SpecialIndentType::Hanging(420)),
142                None,
143                None,
144            ),
145        )
146        .add_level(
147            Level::new(
148                7,
149                Start::new(1),
150                NumberFormat::new("decimal"),
151                LevelText::new("(%8)"),
152                LevelJc::new("left"),
153            )
154            .indent(
155                Some(3360),
156                Some(SpecialIndentType::Hanging(420)),
157                None,
158                None,
159            ),
160        )
161        .add_level(
162            Level::new(
163                8,
164                Start::new(1),
165                NumberFormat::new("decimalEnclosedCircle"),
166                LevelText::new("%9"),
167                LevelJc::new("left"),
168            )
169            .indent(
170                Some(3780),
171                Some(SpecialIndentType::Hanging(420)),
172                None,
173                None,
174            ),
175        )
176}