docx-rs/docx-core/src/documents/styles.rs

64 lines
2.2 KiB
Rust
Raw Normal View History

2019-11-07 09:08:59 +02:00
use super::{DocDefaults, Style};
2019-11-07 06:57:58 +02:00
use crate::documents::BuildXML;
2019-11-14 12:21:45 +02:00
use crate::types::*;
2019-11-07 06:57:58 +02:00
use crate::xml_builder::*;
2019-11-07 11:45:03 +02:00
#[derive(Debug)]
2019-12-06 19:15:21 +02:00
pub struct Styles {
2019-11-07 06:57:58 +02:00
doc_defaults: DocDefaults,
2019-12-06 19:15:21 +02:00
styles: Vec<Style>,
2019-11-07 06:57:58 +02:00
}
2019-12-06 19:15:21 +02:00
impl Styles {
pub fn new() -> Styles {
2019-11-07 06:57:58 +02:00
Default::default()
}
2019-12-06 19:15:21 +02:00
pub fn add_style(mut self, style: Style) -> Self {
2019-11-07 06:57:58 +02:00
self.styles.push(style);
self
}
}
2019-12-06 19:15:21 +02:00
impl Default for Styles {
2019-11-07 06:57:58 +02:00
fn default() -> Self {
Self {
doc_defaults: DocDefaults::new(),
styles: vec![],
}
}
}
2019-12-06 19:15:21 +02:00
impl BuildXML for Styles {
2019-11-07 06:57:58 +02:00
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
2019-11-14 12:21:45 +02:00
let normal = Style::new("Normal", "Normal", StyleType::Paragraph);
2019-11-07 06:57:58 +02:00
b.open_styles()
.add_child(&self.doc_defaults)
2019-11-14 12:21:45 +02:00
.add_child(&normal)
2019-11-07 06:57:58 +02:00
.add_children(&self.styles)
.close()
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
2019-11-11 07:39:22 +02:00
use crate::types::StyleType;
2019-11-07 06:57:58 +02:00
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
2019-11-12 06:33:45 +02:00
fn test_style() {
2019-11-07 06:57:58 +02:00
let c = Styles::new().add_style(Style::new("Title", "TitleName", StyleType::Paragraph));
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
2019-11-14 12:21:45 +02:00
r#"<w:styles xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 w15"><w:docDefaults><w:rPrDefault><w:rPr /></w:rPrDefault></w:docDefaults><w:style w:type="paragraph" w:styleId="Normal"><w:name w:val="Normal" /><w:rPr /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:basedOn w:val="Normal" /><w:next w:val="Normal" /><w:qFormat /></w:style><w:style w:type="paragraph" w:styleId="Title"><w:name w:val="TitleName" /><w:rPr /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:basedOn w:val="Normal" /><w:next w:val="Normal" /><w:qFormat /></w:style></w:styles>"#
2019-11-07 06:57:58 +02:00
);
}
}