use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::Arc;
use laravel::config::Section;
use crate::excel::Type;
use crate::spreadsheet::Properties;
use crate::value::Value;
#[derive(Debug, Clone, Section)]
pub struct ExcelConfig {
#[env("EXCEL_CHUNK_SIZE", default = 1000)]
pub chunk_size: u64,
#[section(default = ExportsConfig::default())]
pub exports: ExportsConfig,
#[section(default = ImportsConfig::default())]
pub imports: ImportsConfig,
#[section(default = default_extension_detector())]
pub extension_detector: BTreeMap<String, Type>,
#[section(default = ValueBinderKind::Default)]
pub value_binder: ValueBinderKind,
#[section(default = TransactionsConfig::default())]
pub transactions: TransactionsConfig,
#[section(default = TemporaryFilesConfig::default())]
pub temporary_files: TemporaryFilesConfig,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ExportsConfig {
pub pre_calculate_formulas: bool,
pub strict_null_comparison: bool,
pub csv: CsvExportSettings,
pub properties: Properties,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CsvExportSettings {
pub delimiter: String,
pub enclosure: String,
pub line_ending: String,
pub use_bom: bool,
pub include_separator_line: bool,
pub excel_compatibility: bool,
pub output_encoding: String,
pub test_auto_detect: bool,
}
impl Default for CsvExportSettings {
fn default() -> Self {
Self {
delimiter: ",".to_owned(),
enclosure: "\"".to_owned(),
line_ending: "\n".to_owned(),
use_bom: false,
include_separator_line: false,
excel_compatibility: false,
output_encoding: String::new(),
test_auto_detect: true,
}
}
}
#[derive(Debug, Clone)]
pub struct ImportsConfig {
pub read_only: bool,
pub ignore_empty: bool,
pub heading_row_formatter: String,
pub csv: CsvImportSettings,
pub properties: Properties,
pub cell_middleware: Vec<CellMiddleware>,
}
impl Default for ImportsConfig {
fn default() -> Self {
Self {
read_only: true,
ignore_empty: false,
heading_row_formatter: "slug".to_owned(),
csv: CsvImportSettings::default(),
properties: Properties::default(),
cell_middleware: Vec::new(),
}
}
}
#[derive(Clone)]
pub struct CellMiddleware(Arc<dyn Fn(Value) -> Value + Send + Sync>);
impl CellMiddleware {
pub fn new(middleware: impl Fn(Value) -> Value + Send + Sync + 'static) -> Self {
Self(Arc::new(middleware))
}
pub(crate) fn call(&self, value: Value) -> Value {
(self.0)(value)
}
}
impl std::fmt::Debug for CellMiddleware {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("CellMiddleware")
}
}
pub fn trim_cell_value() -> CellMiddleware {
CellMiddleware::new(|value| match value {
Value::String(text) => Value::String(
text.trim_matches(|c: char| c.is_whitespace() || c == '\u{FEFF}' || c == '\u{200B}')
.to_owned(),
),
other => other,
})
}
pub fn convert_empty_cell_values_to_null() -> CellMiddleware {
CellMiddleware::new(|value| match value {
Value::String(text) if text.is_empty() => Value::Null,
other => other,
})
}
#[derive(Debug, Clone, PartialEq)]
pub struct CsvImportSettings {
pub delimiter: Option<String>,
pub enclosure: String,
pub escape_character: String,
pub contiguous: bool,
pub input_encoding: String,
}
impl Default for CsvImportSettings {
fn default() -> Self {
Self {
delimiter: None,
enclosure: "\"".to_owned(),
escape_character: "\\".to_owned(),
contiguous: false,
input_encoding: "guess".to_owned(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ValueBinderKind {
#[default]
Default,
String,
Advanced,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionsConfig {
pub handler: Option<String>,
pub connection: Option<String>,
}
impl Default for TransactionsConfig {
fn default() -> Self {
Self {
handler: Some("db".to_owned()),
connection: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TemporaryFilesConfig {
pub local_path: PathBuf,
pub remote_disk: Option<String>,
pub remote_prefix: Option<String>,
pub force_resync_remote: bool,
}
impl Default for TemporaryFilesConfig {
fn default() -> Self {
Self {
local_path: PathBuf::from("framework/cache/laravel-excel"),
remote_disk: None,
remote_prefix: None,
force_resync_remote: false,
}
}
}
fn default_extension_detector() -> BTreeMap<String, Type> {
[
("xlsx", Type::Xlsx),
("xlsm", Type::Xlsx),
("xltx", Type::Xlsx),
("xltm", Type::Xlsx),
("xls", Type::Xls),
("xlt", Type::Xls),
("ods", Type::Ods),
("ots", Type::Ods),
("slk", Type::Slk),
("xml", Type::Xml),
("gnumeric", Type::Gnumeric),
("htm", Type::Html),
("html", Type::Html),
("csv", Type::Csv),
("tsv", Type::Tsv),
("pdf", Type::Dompdf),
]
.into_iter()
.map(|(extension, kind)| (extension.to_owned(), kind))
.collect()
}
pub(crate) fn current() -> ExcelConfig {
laravel::config().section::<ExcelConfig>().clone()
}