use std::path::PathBuf;
use easydoc_core::DocxRow;
use easydoc_core::Result;
use easydoc_core::style::TableStyle;
use crate::executor::table_executor::TableWriteExecutor;
pub struct TableWriteBuilder<'a, T: DocxRow> {
path: PathBuf,
data: &'a [T],
title: Option<String>,
style: TableStyle,
need_header: bool,
}
impl<'a, T: DocxRow> TableWriteBuilder<'a, T> {
#[must_use]
pub fn new(path: impl Into<PathBuf>, data: &'a [T]) -> Self {
Self {
path: path.into(),
data,
title: None,
style: TableStyle::default(),
need_header: true,
}
}
#[must_use]
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
#[must_use]
pub fn need_header(mut self, need: bool) -> Self {
self.need_header = need;
self
}
#[must_use]
pub fn header_style(mut self, style: TableStyle) -> Self {
self.style = style;
self
}
#[must_use]
pub fn banded_rows(mut self, enabled: bool) -> Self {
self.style.banded_rows = enabled;
self
}
pub fn do_write(self) -> Result<()> {
let executor = TableWriteExecutor::new(
self.path,
self.data,
self.title,
self.style,
self.need_header,
);
executor.execute()
}
pub fn do_write_to_bytes(self) -> Result<Vec<u8>> {
let executor = TableWriteExecutor::new(
self.path,
self.data,
self.title,
self.style,
self.need_header,
);
executor.execute_to_bytes()
}
pub fn do_write_to_writer<W: std::io::Write + std::io::Seek>(self, writer: W) -> Result<()> {
let executor = TableWriteExecutor::new(
self.path,
self.data,
self.title,
self.style,
self.need_header,
);
executor.execute_to_writer(writer)
}
}