docx-reader 0.1.1

A .docx file reader in rust
Documentation
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;

use super::*;

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Document {
	pub children: Vec<DocumentChild>,
	pub section_property: SectionProperty,
	pub has_numbering: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub enum DocumentChild {
	Paragraph(Box<Paragraph>),
	Table(Box<Table>),
	StructuredDataTag(Box<StructuredDataTag>),
	TableOfContents(Box<TableOfContents>),
}

impl Serialize for DocumentChild {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		match *self {
			DocumentChild::Paragraph(ref p) => {
				let mut t = serializer.serialize_struct("Paragraph", 2)?;
				t.serialize_field("type", "paragraph")?;
				t.serialize_field("data", p)?;
				t.end()
			}
			DocumentChild::Table(ref c) => {
				let mut t = serializer.serialize_struct("Table", 2)?;
				t.serialize_field("type", "table")?;
				t.serialize_field("data", c)?;
				t.end()
			}
			DocumentChild::StructuredDataTag(ref r) => {
				let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
				t.serialize_field("type", "structuredDataTag")?;
				t.serialize_field("data", r)?;
				t.end()
			}
			DocumentChild::TableOfContents(ref r) => {
				let mut t = serializer.serialize_struct("TableOfContents", 2)?;
				t.serialize_field("type", "tableOfContents")?;
				t.serialize_field("data", r)?;
				t.end()
			}
		}
	}
}

impl Default for Document {
	fn default() -> Self {
		Self {
			children: Vec::new(),
			section_property: SectionProperty::new(),
			has_numbering: false,
		}
	}
}

impl Document {
	pub fn new() -> Document {
		Default::default()
	}

	pub fn add_paragraph(mut self, p: Paragraph) -> Self {
		if p.has_numbering {
			self.has_numbering = true
		}
		self.children.push(DocumentChild::Paragraph(Box::new(p)));
		self
	}

	pub fn add_table(mut self, t: Table) -> Self {
		if t.has_numbering {
			self.has_numbering = true
		}
		self.children.push(DocumentChild::Table(Box::new(t)));
		self
	}

	pub fn page_size(mut self, size: PageSize) -> Self {
		self.section_property = self.section_property.page_size(size);
		self
	}

	pub fn page_margin(mut self, margin: crate::types::PageMargin) -> Self {
		self.section_property = self.section_property.page_margin(margin);
		self
	}

	pub fn page_orient(mut self, o: crate::types::PageOrientationType) -> Self {
		self.section_property = self.section_property.page_orient(o);
		self
	}

	pub fn doc_grid(mut self, doc_grid: DocGrid) -> Self {
		self.section_property = self.section_property.doc_grid(doc_grid);
		self
	}

	pub fn default_section_property(mut self, property: SectionProperty) -> Self {
		self.section_property = property;
		self
	}

	pub fn header(mut self, h: Header, rid: &str) -> Self {
		self.section_property = self.section_property.header(h, rid);
		self
	}

	pub fn first_header(mut self, h: Header, rid: &str) -> Self {
		self.section_property = self.section_property.first_header(h, rid);
		self
	}

	pub fn even_header(mut self, h: Header, rid: &str) -> Self {
		self.section_property = self.section_property.even_header(h, rid);
		self
	}

	pub fn footer(mut self, h: Footer, rid: &str) -> Self {
		self.section_property = self.section_property.footer(h, rid);
		self
	}

	pub fn first_footer(mut self, h: Footer, rid: &str) -> Self {
		self.section_property = self.section_property.first_footer(h, rid);
		self
	}

	pub fn even_footer(mut self, h: Footer, rid: &str) -> Self {
		self.section_property = self.section_property.even_footer(h, rid);
		self
	}

	pub fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
		if t.has_numbering {
			self.has_numbering = true
		}
		self.children
			.push(DocumentChild::StructuredDataTag(Box::new(t)));
		self
	}

	pub fn add_table_of_contents(mut self, t: TableOfContents) -> Self {
		self.children
			.push(DocumentChild::TableOfContents(Box::new(t)));
		self
	}

	pub fn columns(mut self, col: usize) -> Self {
		self.section_property.columns = col;
		self
	}

	pub fn text_direction(mut self, direction: String) -> Self {
		self.section_property.text_direction = direction;
		self
	}
}