use chrono::{DateTime, Utc};
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Config {
pub strict_checks: bool,
pub auto_fix: bool,
pub verbosity: Verbosity,
pub write: WriteOptions,
pub read: ReadOptions,
}
impl Default for Config {
fn default() -> Self {
Self {
strict_checks: true,
auto_fix: false,
verbosity: Verbosity::Warn,
write: WriteOptions::default(),
read: ReadOptions::default(),
}
}
}
#[allow(dead_code)]
impl Config {
#[must_use]
pub(crate) fn new() -> Self {
Self::default()
}
#[must_use]
pub(crate) fn with_strict_checks(mut self, strict: bool) -> Self {
self.strict_checks = strict;
self
}
#[must_use]
pub(crate) fn with_auto_fix(mut self, auto_fix: bool) -> Self {
self.auto_fix = auto_fix;
self
}
#[must_use]
pub(crate) fn with_verbosity(mut self, verbosity: Verbosity) -> Self {
self.verbosity = verbosity;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Verbosity {
None,
Info,
#[default]
Warn,
Error,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub(crate) struct WriteOptions {
pub max_size_gb: Option<f64>,
pub created: Option<DateTime<Utc>>,
pub modified: Option<DateTime<Utc>>,
}
#[allow(dead_code)]
impl WriteOptions {
#[must_use]
pub(crate) fn new() -> Self {
Self::default()
}
#[must_use]
pub(crate) fn with_max_size_gb(mut self, max_size: f64) -> Self {
self.max_size_gb = Some(max_size);
self
}
#[must_use]
pub(crate) fn with_created(mut self, created: DateTime<Utc>) -> Self {
self.created = Some(created);
self
}
#[must_use]
pub(crate) fn with_modified(mut self, modified: DateTime<Utc>) -> Self {
self.modified = Some(modified);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ReadOptions {
pub text_mode: TextMode,
pub row_limit: Option<usize>,
pub preserve_blanks: bool,
}
impl Default for ReadOptions {
fn default() -> Self {
Self {
text_mode: TextMode::LossyUtf8,
row_limit: None,
preserve_blanks: false,
}
}
}
#[allow(dead_code)]
impl ReadOptions {
#[must_use]
pub(crate) fn new() -> Self {
Self::default()
}
#[must_use]
pub(crate) fn with_text_mode(mut self, mode: TextMode) -> Self {
self.text_mode = mode;
self
}
#[must_use]
pub(crate) fn with_row_limit(mut self, limit: usize) -> Self {
self.row_limit = Some(limit);
self
}
#[must_use]
pub(crate) fn with_preserve_blanks(mut self, preserve: bool) -> Self {
self.preserve_blanks = preserve;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TextMode {
StrictUtf8,
#[default]
LossyUtf8,
Latin1,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_defaults() {
let config = Config::default();
assert!(config.strict_checks);
assert!(!config.auto_fix);
assert_eq!(config.verbosity, Verbosity::Warn);
}
#[test]
fn test_read_options_builder() {
let opts = ReadOptions::new()
.with_text_mode(TextMode::StrictUtf8)
.with_row_limit(100)
.with_preserve_blanks(true);
assert_eq!(opts.text_mode, TextMode::StrictUtf8);
assert_eq!(opts.row_limit, Some(100));
assert!(opts.preserve_blanks);
}
}