use crate::delimiter::Delimiter;
#[derive(Debug, Clone)]
pub struct LoadOptions {
pub delimiter: Delimiter,
pub has_header: bool,
pub skip_rows: usize,
pub max_rows: Option<usize>,
pub sheet_index: Option<usize>,
pub sheet_name: Option<String>,
pub infer_schema: bool,
pub infer_schema_length: Option<usize>,
}
impl Default for LoadOptions {
fn default() -> Self {
Self {
delimiter: Delimiter::Auto,
has_header: true,
skip_rows: 0,
max_rows: None,
sheet_index: None,
sheet_name: None,
infer_schema: true,
infer_schema_length: Some(1000),
}
}
}
impl LoadOptions {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub const fn with_delimiter(mut self, delimiter: Delimiter) -> Self {
self.delimiter = delimiter;
self
}
#[must_use]
pub const fn with_header(mut self, has_header: bool) -> Self {
self.has_header = has_header;
self
}
#[must_use]
pub const fn with_skip_rows(mut self, skip_rows: usize) -> Self {
self.skip_rows = skip_rows;
self
}
#[must_use]
pub const fn with_max_rows(mut self, max_rows: Option<usize>) -> Self {
self.max_rows = max_rows;
self
}
#[must_use]
pub const fn with_sheet_index(mut self, index: usize) -> Self {
self.sheet_index = Some(index);
self
}
#[must_use]
pub fn with_sheet_name(mut self, name: impl Into<String>) -> Self {
self.sheet_name = Some(name.into());
self
}
#[must_use]
pub const fn with_infer_schema(mut self, infer: bool) -> Self {
self.infer_schema = infer;
self
}
#[must_use]
pub const fn with_infer_schema_length(mut self, length: Option<usize>) -> Self {
self.infer_schema_length = length;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_options() {
let opts = LoadOptions::default();
assert_eq!(opts.delimiter, Delimiter::Auto);
assert!(opts.has_header);
assert_eq!(opts.skip_rows, 0);
assert_eq!(opts.max_rows, None);
assert!(opts.infer_schema);
}
#[test]
fn test_builder_chain() {
let opts = LoadOptions::new()
.with_delimiter(Delimiter::Tab)
.with_header(false)
.with_skip_rows(2)
.with_max_rows(Some(100))
.with_sheet_name("Sheet2");
assert_eq!(opts.delimiter, Delimiter::Tab);
assert!(!opts.has_header);
assert_eq!(opts.skip_rows, 2);
assert_eq!(opts.max_rows, Some(100));
assert_eq!(opts.sheet_name, Some("Sheet2".to_string()));
}
}