2020-02-11 10:01:39 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
use super::*;
|
2019-11-06 12:17:49 +02:00
|
|
|
use crate::documents::BuildXML;
|
2019-11-11 09:48:28 +02:00
|
|
|
use crate::types::{AlignmentType, SpecialIndentType};
|
2019-11-06 12:17:49 +02:00
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[derive(Serialize, Debug, Clone, PartialEq)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-12-06 19:15:21 +02:00
|
|
|
pub struct ParagraphProperty {
|
2020-02-11 10:01:39 +02:00
|
|
|
pub run_property: RunProperty,
|
2020-05-27 14:43:25 +03:00
|
|
|
pub style: Option<ParagraphStyle>,
|
2020-02-11 10:01:39 +02:00
|
|
|
pub numbering_property: Option<NumberingProperty>,
|
|
|
|
pub alignment: Option<Justification>,
|
|
|
|
pub indent: Option<Indent>,
|
2019-11-11 06:05:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:15:21 +02:00
|
|
|
impl Default for ParagraphProperty {
|
2019-11-11 06:05:07 +02:00
|
|
|
fn default() -> Self {
|
2019-11-11 07:39:22 +02:00
|
|
|
ParagraphProperty {
|
|
|
|
run_property: RunProperty::new(),
|
2020-05-27 14:43:25 +03:00
|
|
|
style: None,
|
2019-12-06 12:18:48 +02:00
|
|
|
numbering_property: None,
|
2019-11-11 09:48:28 +02:00
|
|
|
alignment: None,
|
|
|
|
indent: None,
|
2019-11-11 07:39:22 +02:00
|
|
|
}
|
2019-11-11 06:05:07 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 12:17:49 +02:00
|
|
|
|
|
|
|
// 17.3.1.26
|
|
|
|
// pPr (Paragraph Properties)
|
|
|
|
// This element specifies a set of paragraph properties which shall be applied to the contents of the parent
|
|
|
|
// paragraph after all style/numbering/table properties have been applied to the text. These properties are defined
|
|
|
|
// as direct formatting, since they are directly applied to the paragraph and supersede any formatting from styles.
|
2019-12-06 19:15:21 +02:00
|
|
|
impl ParagraphProperty {
|
|
|
|
pub fn new() -> ParagraphProperty {
|
2019-11-11 06:05:07 +02:00
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
2019-12-06 12:18:48 +02:00
|
|
|
pub fn align(mut self, alignment_type: AlignmentType) -> Self {
|
2019-11-11 06:05:07 +02:00
|
|
|
self.alignment = Some(Justification::new(alignment_type.to_string()));
|
|
|
|
self
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
2019-11-11 08:36:26 +02:00
|
|
|
|
2019-12-06 12:18:48 +02:00
|
|
|
pub fn style(mut self, style_id: &str) -> Self {
|
2020-05-27 14:43:25 +03:00
|
|
|
self.style = Some(ParagraphStyle::new(Some(style_id)));
|
2019-11-11 08:36:26 +02:00
|
|
|
self
|
|
|
|
}
|
2019-11-11 09:48:28 +02:00
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
pub fn indent(
|
|
|
|
mut self,
|
2020-03-10 04:56:12 +02:00
|
|
|
left: Option<i32>,
|
2020-02-11 10:01:39 +02:00
|
|
|
special_indent: Option<SpecialIndentType>,
|
2020-02-28 12:52:41 +02:00
|
|
|
end: Option<i32>,
|
2020-03-10 04:56:12 +02:00
|
|
|
start_chars: Option<i32>,
|
2020-02-11 10:01:39 +02:00
|
|
|
) -> Self {
|
2020-03-10 04:56:12 +02:00
|
|
|
self.indent = Some(Indent::new(left, special_indent, end, start_chars));
|
2019-11-11 09:48:28 +02:00
|
|
|
self
|
|
|
|
}
|
2019-12-06 12:18:48 +02:00
|
|
|
|
2019-12-06 19:15:21 +02:00
|
|
|
pub fn numbering(mut self, id: NumberingId, level: IndentLevel) -> Self {
|
2020-03-13 10:01:25 +02:00
|
|
|
self.numbering_property = Some(NumberingProperty::new().add_num(id, level));
|
2019-12-06 12:18:48 +02:00
|
|
|
self
|
|
|
|
}
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:15:21 +02:00
|
|
|
impl BuildXML for ParagraphProperty {
|
2019-11-06 12:17:49 +02:00
|
|
|
fn build(&self) -> Vec<u8> {
|
2019-11-11 08:36:26 +02:00
|
|
|
XMLBuilder::new()
|
2020-09-02 09:54:43 +03:00
|
|
|
.open_paragraph_property()
|
|
|
|
.add_child(&self.run_property)
|
2020-05-27 14:43:25 +03:00
|
|
|
.add_optional_child(&self.style)
|
2019-12-06 12:18:48 +02:00
|
|
|
.add_optional_child(&self.numbering_property)
|
2019-11-11 08:36:26 +02:00
|
|
|
.add_optional_child(&self.alignment)
|
2019-11-11 09:48:28 +02:00
|
|
|
.add_optional_child(&self.indent)
|
2019-11-11 08:36:26 +02:00
|
|
|
.close()
|
|
|
|
.build()
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
2019-11-07 06:57:58 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
use pretty_assertions::assert_eq;
|
2019-11-06 12:17:49 +02:00
|
|
|
use std::str;
|
|
|
|
|
|
|
|
#[test]
|
2019-11-11 08:36:26 +02:00
|
|
|
fn test_default() {
|
2019-11-06 12:17:49 +02:00
|
|
|
let c = ParagraphProperty::new();
|
|
|
|
let b = c.build();
|
2020-06-08 07:41:13 +03:00
|
|
|
assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:pPr><w:rPr /></w:pPr>"#);
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
2019-11-11 08:36:26 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_alignment() {
|
|
|
|
let c = ParagraphProperty::new();
|
|
|
|
let b = c.align(AlignmentType::Right).build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2020-05-27 14:43:25 +03:00
|
|
|
r#"<w:pPr><w:rPr /><w:jc w:val="right" /></w:pPr>"#
|
2019-11-11 09:48:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_indent() {
|
|
|
|
let c = ParagraphProperty::new();
|
2020-03-10 04:56:12 +02:00
|
|
|
let b = c.indent(Some(20), None, None, None).build();
|
2019-11-11 09:48:28 +02:00
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2020-05-27 14:43:25 +03:00
|
|
|
r#"<w:pPr><w:rPr /><w:ind w:left="20" w:right="0" /></w:pPr>"#
|
2020-02-11 10:01:39 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_indent_json() {
|
|
|
|
let c = ParagraphProperty::new();
|
2020-03-10 04:56:12 +02:00
|
|
|
let b = c.indent(Some(20), Some(SpecialIndentType::FirstLine(10)), None, None);
|
2020-02-11 10:01:39 +02:00
|
|
|
assert_eq!(
|
|
|
|
serde_json::to_string(&b).unwrap(),
|
2020-06-08 07:41:13 +03:00
|
|
|
r#"{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"fonts":null},"style":null,"numberingProperty":null,"alignment":null,"indent":{"start":20,"startChars":null,"end":null,"specialIndent":{"type":"firstLine","val":10}}}"#
|
2019-11-11 08:36:26 +02:00
|
|
|
);
|
|
|
|
}
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|