2020-02-11 10:01:39 +02:00
|
|
|
use serde::ser::{SerializeStruct, Serializer};
|
|
|
|
use serde::Serialize;
|
|
|
|
|
2019-12-06 12:18:48 +02:00
|
|
|
use super::{IndentLevel, NumberingId};
|
|
|
|
use crate::documents::BuildXML;
|
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2019-12-06 19:15:21 +02:00
|
|
|
pub struct NumberingProperty {
|
2020-03-13 10:01:25 +02:00
|
|
|
pub id: Option<NumberingId>,
|
|
|
|
pub level: Option<IndentLevel>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for NumberingProperty {
|
|
|
|
fn default() -> Self {
|
|
|
|
NumberingProperty {
|
|
|
|
id: None,
|
|
|
|
level: None,
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 12:18:48 +02:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:15:21 +02:00
|
|
|
impl NumberingProperty {
|
2020-03-13 10:01:25 +02:00
|
|
|
pub fn new() -> NumberingProperty {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_num(mut self, id: NumberingId, level: IndentLevel) -> NumberingProperty {
|
|
|
|
self.id = Some(id);
|
|
|
|
self.level = Some(level);
|
|
|
|
self
|
2019-12-06 12:18:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 19:15:21 +02:00
|
|
|
impl BuildXML for NumberingProperty {
|
2019-12-06 12:18:48 +02:00
|
|
|
fn build(&self) -> Vec<u8> {
|
|
|
|
let b = XMLBuilder::new();
|
|
|
|
b.open_numbering_property()
|
2020-03-13 10:01:25 +02:00
|
|
|
.add_optional_child(&self.id)
|
|
|
|
.add_optional_child(&self.level)
|
2019-12-06 12:18:48 +02:00
|
|
|
.close()
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
impl Serialize for NumberingProperty {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
let mut t = serializer.serialize_struct("NumberProperty", 2)?;
|
2020-03-13 10:01:25 +02:00
|
|
|
let mut id: Option<usize> = None;
|
|
|
|
if let Some(n) = &self.id {
|
|
|
|
id = Some(n.id);
|
|
|
|
}
|
|
|
|
t.serialize_field("id", &id)?;
|
|
|
|
|
|
|
|
let mut level: Option<usize> = None;
|
|
|
|
if let Some(n) = &self.level {
|
|
|
|
level = Some(n.val);
|
|
|
|
}
|
|
|
|
t.serialize_field("level", &level)?;
|
2020-02-11 10:01:39 +02:00
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 12:18:48 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
#[cfg(test)]
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_num_property() {
|
2020-03-13 10:01:25 +02:00
|
|
|
let c = NumberingProperty::new().add_num(NumberingId::new(0), IndentLevel::new(3));
|
2019-12-06 12:18:48 +02:00
|
|
|
let b = c.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2019-12-06 19:15:21 +02:00
|
|
|
r#"<w:numPr><w:numId w:val="0" /><w:ilvl w:val="3" /></w:numPr>"#
|
2019-12-06 12:18:48 +02:00
|
|
|
);
|
|
|
|
}
|
2020-03-13 10:01:25 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty_num_property() {
|
|
|
|
let c = NumberingProperty::new();
|
|
|
|
let b = c.build();
|
|
|
|
assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:numPr />"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_num_property_json() {
|
|
|
|
let c = NumberingProperty::new().add_num(NumberingId::new(0), IndentLevel::new(3));
|
|
|
|
assert_eq!(serde_json::to_string(&c).unwrap(), r#"{"id":0,"level":3}"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty_num_property_json() {
|
|
|
|
let c = NumberingProperty::new();
|
|
|
|
assert_eq!(
|
|
|
|
serde_json::to_string(&c).unwrap(),
|
|
|
|
r#"{"id":null,"level":null}"#
|
|
|
|
);
|
|
|
|
}
|
2019-12-06 12:18:48 +02:00
|
|
|
}
|