2019-12-06 12:18:48 +02:00
|
|
|
use crate::documents::{BuildXML, Level};
|
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-12-08 21:14:27 +02:00
|
|
|
pub struct Numbering {
|
2019-12-06 19:15:21 +02:00
|
|
|
id: usize,
|
2019-12-08 21:14:27 +02:00
|
|
|
levels: Vec<Level>,
|
2019-12-06 12:18:48 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Numbering {
|
2019-12-06 19:15:21 +02:00
|
|
|
pub fn new(id: usize) -> Self {
|
2019-12-06 12:18:48 +02:00
|
|
|
Self { id, levels: vec![] }
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn add_level(mut self, level: Level) -> Self {
|
2019-12-06 12:18:48 +02:00
|
|
|
self.levels.push(level);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl BuildXML for Numbering {
|
2019-12-06 12:18:48 +02:00
|
|
|
fn build(&self) -> Vec<u8> {
|
2019-12-06 19:15:21 +02:00
|
|
|
let id = format!("{}", self.id);
|
2019-12-06 12:18:48 +02:00
|
|
|
let mut b = XMLBuilder::new();
|
2019-12-06 19:15:21 +02:00
|
|
|
b = b.open_abstract_num(&id);
|
2019-12-06 12:18:48 +02:00
|
|
|
for l in &self.levels {
|
|
|
|
b = b.add_child(l);
|
|
|
|
}
|
2019-12-06 19:15:21 +02:00
|
|
|
b.close().open_num(&id).abstract_num_id(&id).close().build()
|
2019-12-06 12:18:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
#[cfg(test)]
|
|
|
|
use crate::documents::{Level, LevelJc, LevelText, NumberFormat, Start};
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_numbering() {
|
2019-12-06 19:15:21 +02:00
|
|
|
let mut c = Numbering::new(0);
|
2019-12-06 12:18:48 +02:00
|
|
|
c = c.add_level(Level::new(
|
|
|
|
1,
|
|
|
|
Start::new(1),
|
|
|
|
NumberFormat::new("decimal"),
|
|
|
|
LevelText::new("%4."),
|
|
|
|
LevelJc::new("left"),
|
|
|
|
));
|
|
|
|
let b = c.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
|
|
|
r#"<w:abstractNum w:abstractNumId="0"><w:lvl w:ilvl="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%4." /><w:lvlJc w:val="left" /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr></w:lvl></w:abstractNum>
|
|
|
|
<w:num w:numId="0">
|
|
|
|
<w:abstractNumId w:val="0" />
|
|
|
|
</w:num>"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|