use std::path::PathBuf;
use crate::payload::Limits;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RenderOptions {
pub template_dir: Option<PathBuf>,
pub template_name: String,
pub inline_css: bool,
pub limits: Option<Limits>,
}
impl Default for RenderOptions {
fn default() -> Self {
Self {
template_dir: None,
template_name: "tabular.jinja".to_string(),
inline_css: false,
limits: None,
}
}
}
impl RenderOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_template_dir(mut self, directory: impl Into<PathBuf>) -> Self {
self.template_dir = Some(directory.into());
self
}
pub fn with_template_name(mut self, name: impl Into<String>) -> Self {
self.template_name = name.into();
self
}
pub fn with_inline_css(mut self, inline_css: bool) -> Self {
self.inline_css = inline_css;
self
}
pub fn with_limits(mut self, limits: Limits) -> Self {
self.limits = Some(limits);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CsvOptions {
pub delimiter: char,
pub bom: bool,
pub limits: Option<Limits>,
}
impl Default for CsvOptions {
fn default() -> Self {
Self {
delimiter: ',',
bom: false,
limits: None,
}
}
}
impl CsvOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_delimiter(mut self, delimiter: char) -> Self {
self.delimiter = delimiter;
self
}
pub fn with_bom(mut self, bom: bool) -> Self {
self.bom = bom;
self
}
pub fn with_limits(mut self, limits: Limits) -> Self {
self.limits = Some(limits);
self
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct XlsxOptions {
pub limits: Option<Limits>,
}
impl XlsxOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_limits(mut self, limits: Limits) -> Self {
self.limits = Some(limits);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextOptions {
pub sanitize: bool,
pub limits: Option<Limits>,
}
impl Default for TextOptions {
fn default() -> Self {
Self {
sanitize: true,
limits: None,
}
}
}
impl TextOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_sanitize(mut self, sanitize: bool) -> Self {
self.sanitize = sanitize;
self
}
pub fn with_limits(mut self, limits: Limits) -> Self {
self.limits = Some(limits);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn render_options_defaults_and_builders() {
let options = RenderOptions::default();
assert!(options.template_dir.is_none());
assert_eq!(options.template_name, "tabular.jinja");
assert!(!options.inline_css);
assert!(options.limits.is_none());
let options = RenderOptions::new()
.with_template_dir("templates")
.with_template_name("custom.jinja")
.with_inline_css(true)
.with_limits(Limits {
max_rows: Some(1),
max_bytes: None,
});
assert_eq!(options.template_dir.as_deref(), Some(std::path::Path::new("templates")));
assert_eq!(options.template_name, "custom.jinja");
assert!(options.inline_css);
assert_eq!(options.limits.and_then(|limits| limits.max_rows), Some(1));
}
#[test]
fn csv_and_xlsx_options() {
let csv = CsvOptions::new().with_delimiter(';').with_bom(true).with_limits(Limits {
max_rows: Some(2),
max_bytes: None,
});
assert_eq!(csv.delimiter, ';');
assert!(csv.bom);
assert_eq!(csv.limits.and_then(|limits| limits.max_rows), Some(2));
let xlsx = XlsxOptions::new().with_limits(Limits {
max_rows: Some(3),
max_bytes: None,
});
assert_eq!(xlsx.limits.and_then(|limits| limits.max_rows), Some(3));
}
#[test]
fn text_options_defaults_and_builders() {
let options = TextOptions::default();
assert!(options.sanitize);
assert!(options.limits.is_none());
let options = TextOptions::new().with_sanitize(false).with_limits(Limits {
max_rows: Some(4),
max_bytes: None,
});
assert!(!options.sanitize);
assert_eq!(options.limits.and_then(|limits| limits.max_rows), Some(4));
}
}