use serde::{Serialize, Serializer};
use std::io::Write;
use crate::documents::BuildXML;
use crate::escape::escape;
use crate::xml_builder::*;
#[derive(Debug, Clone, PartialEq)]
pub struct ParagraphStyle {
pub val: String,
}
impl Default for ParagraphStyle {
fn default() -> Self {
ParagraphStyle {
val: "Normal".to_owned(),
}
}
}
impl ParagraphStyle {
pub fn new(val: Option<impl Into<String>>) -> ParagraphStyle {
if let Some(v) = val {
ParagraphStyle {
val: escape(&v.into()),
}
} else {
Default::default()
}
}
}
impl BuildXML for ParagraphStyle {
fn build_to<W: Write>(
&self,
stream: crate::xml::writer::EventWriter<W>,
) -> crate::xml::writer::Result<crate::xml::writer::EventWriter<W>> {
XMLBuilder::from(stream)
.paragraph_style(&self.val)?
.into_inner()
}
}
impl Serialize for ParagraphStyle {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.val)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_p_style() {
let c = ParagraphStyle::new(Some("Heading"));
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:pStyle w:val="Heading" />"#
);
}
}