isis-report-lib 0.2.2

Tabular reports with hierarchical layouts: HTML, PDF, XLSX, CSV and plain text
Documentation
//! Ergonomic option structs for the typed `Report` API.

use std::path::PathBuf;

use crate::payload::Limits;

/// Options for HTML rendering.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RenderOptions {
    /// Directory with custom templates. `None` uses the bundled templates.
    pub template_dir: Option<PathBuf>,
    /// Template name inside `template_dir` or the bundled template set.
    pub template_name: String,
    /// Inline CSS variables and rules into the HTML.
    pub inline_css: bool,
    /// Optional operator quotas.
    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 {
    /// Start with default options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Use a custom template directory.
    pub fn with_template_dir(mut self, directory: impl Into<PathBuf>) -> Self {
        self.template_dir = Some(directory.into());
        self
    }

    /// Select the template name inside [`template_dir`](Self::template_dir) or
    /// the bundled template set.
    pub fn with_template_name(mut self, name: impl Into<String>) -> Self {
        self.template_name = name.into();
        self
    }

    /// Enable inline CSS. Requires the `email` feature.
    pub fn with_inline_css(mut self, inline_css: bool) -> Self {
        self.inline_css = inline_css;
        self
    }

    /// Tighten operator quotas for this render.
    pub fn with_limits(mut self, limits: Limits) -> Self {
        self.limits = Some(limits);
        self
    }
}

/// Options for CSV export.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CsvOptions {
    /// Field delimiter.
    pub delimiter: char,
    /// Prefix the output with a UTF-8 BOM.
    pub bom: bool,
    /// Optional operator quotas.
    pub limits: Option<Limits>,
}

impl Default for CsvOptions {
    fn default() -> Self {
        Self {
            delimiter: ',',
            bom: false,
            limits: None,
        }
    }
}

impl CsvOptions {
    /// Start with default options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the field delimiter (must be ASCII).
    pub fn with_delimiter(mut self, delimiter: char) -> Self {
        self.delimiter = delimiter;
        self
    }

    /// Prefix the output with a UTF-8 BOM.
    pub fn with_bom(mut self, bom: bool) -> Self {
        self.bom = bom;
        self
    }

    /// Tighten operator quotas for this export.
    pub fn with_limits(mut self, limits: Limits) -> Self {
        self.limits = Some(limits);
        self
    }
}

/// Options for XLSX export.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct XlsxOptions {
    /// Optional operator quotas.
    pub limits: Option<Limits>,
}

impl XlsxOptions {
    /// Start with default options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Tighten operator quotas for this export.
    pub fn with_limits(mut self, limits: Limits) -> Self {
        self.limits = Some(limits);
        self
    }
}

/// Options for plain-text export.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextOptions {
    /// Strip control characters and terminal escape sequences.
    pub sanitize: bool,
    /// Optional operator quotas.
    pub limits: Option<Limits>,
}

impl Default for TextOptions {
    fn default() -> Self {
        Self {
            sanitize: true,
            limits: None,
        }
    }
}

impl TextOptions {
    /// Start with default options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Strip control characters and terminal escape sequences.
    pub fn with_sanitize(mut self, sanitize: bool) -> Self {
        self.sanitize = sanitize;
        self
    }

    /// Tighten operator quotas for this export.
    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));
    }
}