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

69 lines
1.7 KiB
Rust
Raw Normal View History

2020-08-19 18:53:35 +03:00
use super::*;
2019-11-14 08:54:39 +02:00
use crate::documents::BuildXML;
use crate::xml_builder::*;
use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
2019-11-14 08:54:39 +02:00
pub struct Settings {
default_tab_stop: DefaultTabStop,
zoom: Zoom,
2020-08-19 18:53:35 +03:00
doc_id: Option<DocId>,
2019-11-14 08:54:39 +02:00
}
impl Settings {
pub fn new() -> Settings {
Default::default()
}
2020-08-19 18:53:35 +03:00
pub fn doc_id(mut self, id: impl Into<String>) -> Self {
self.doc_id = Some(DocId::new(id.into()));
self
}
2019-11-14 08:54:39 +02:00
}
impl Default for Settings {
fn default() -> Self {
Self {
default_tab_stop: DefaultTabStop::new(709),
zoom: Zoom::new(100),
2020-08-19 18:53:35 +03:00
doc_id: None,
2019-11-14 08:54:39 +02:00
}
}
}
impl BuildXML for Settings {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.declaration(Some(true))
.open_settings()
.add_child(&self.default_tab_stop)
.add_child(&self.zoom)
2020-08-19 18:53:35 +03:00
.add_optional_child(&self.doc_id)
2019-11-14 08:54:39 +02:00
.close()
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_settings() {
let c = Settings::new();
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2020-08-19 18:53:35 +03:00
<w:settings 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"><w:defaultTabStop w:val="709" /><w:zoom w:percent="100" /></w:settings>"#
2019-11-14 08:54:39 +02:00
);
}
}