use std::path::Path;
use serde::Serialize;
use serde::de::DeserializeOwned;
use crate::streaming::{StreamingRows, StreamingStructuredRows, StreamingTypedRows};
use crate::writer::XlsxWriter;
use crate::{
AnalysisResult, ByteQuerySummary, DynamicRow, ExcelRange, QueryPlan, RagChunk, RagExport,
RagExportOptions, RagManifest, ReadOptions, Result, SheetInfo, StructuredRow, WriteOptions,
};
pub struct MiniExcel;
impl MiniExcel {
pub fn get_sheet_names(path: impl AsRef<Path>) -> Result<Vec<String>> {
crate::streaming::sheet_names(path)
}
pub fn get_sheet_names_from_bytes(bytes: &[u8]) -> Result<Vec<String>> {
crate::streaming::sheet_names_from_bytes(bytes)
}
pub fn get_sheet_info(path: impl AsRef<Path>) -> Result<Vec<SheetInfo>> {
crate::streaming::sheet_info(path)
}
pub fn get_sheet_info_from_bytes(bytes: &[u8]) -> Result<Vec<SheetInfo>> {
crate::streaming::sheet_info_from_bytes(bytes)
}
pub fn get_sheet_dimensions(path: impl AsRef<Path>) -> Result<Vec<ExcelRange>> {
crate::streaming::sheet_dimensions(path)
}
pub fn get_sheet_dimensions_from_bytes(bytes: &[u8]) -> Result<Vec<ExcelRange>> {
crate::streaming::sheet_dimensions_from_bytes(bytes)
}
pub fn query(
path: impl AsRef<Path>,
) -> Result<Box<dyn Iterator<Item = Result<DynamicRow>> + Send>> {
Self::query_with_options(path, &ReadOptions::default())
}
pub fn query_with_options(
path: impl AsRef<Path>,
options: &ReadOptions,
) -> Result<Box<dyn Iterator<Item = Result<DynamicRow>> + Send>> {
Ok(Box::new(StreamingRows::open(path, options)?))
}
pub fn query_structured(
path: impl AsRef<Path>,
) -> Result<Box<dyn Iterator<Item = Result<StructuredRow>> + Send>> {
Self::query_structured_with_options(path, &ReadOptions::default())
}
pub fn query_structured_with_options(
path: impl AsRef<Path>,
options: &ReadOptions,
) -> Result<Box<dyn Iterator<Item = Result<StructuredRow>> + Send>> {
Ok(Box::new(StreamingStructuredRows::open(path, options)?))
}
pub fn get_columns(path: impl AsRef<Path>, options: &ReadOptions) -> Result<Vec<String>> {
let mut rows = Self::query_with_options(path, options)?;
Ok(rows.next().transpose()?.map_or_else(Vec::new, |row| row.into_keys().collect()))
}
pub fn query_bytes(bytes: &[u8], options: &ReadOptions) -> Result<Vec<DynamicRow>> {
crate::streaming::query_bytes(bytes, options)
}
pub fn visit_rows_from_bytes<F>(
bytes: &[u8],
options: &ReadOptions,
mut visitor: F,
) -> Result<ByteQuerySummary>
where
F: FnMut(usize, &DynamicRow) -> Result<bool>,
{
crate::streaming::visit_dynamic_rows(bytes, options, |_, excel_row, row| {
visitor(excel_row, &row)
})
}
pub fn analyze_with_options(
path: impl AsRef<Path>,
options: &ReadOptions,
plan: &QueryPlan,
) -> Result<AnalysisResult> {
crate::analytics::analyze_path(path, options, plan)
}
pub fn analyze_bytes(
bytes: &[u8],
options: &ReadOptions,
plan: &QueryPlan,
) -> Result<AnalysisResult> {
crate::analytics::analyze_bytes(bytes, options, plan)
}
pub fn export_rag(
path: impl AsRef<Path>,
options: &ReadOptions,
export_options: &RagExportOptions,
) -> Result<RagExport> {
crate::rag::export_path(path, options, export_options)
}
pub fn visit_rag_chunks_from_bytes<F>(
bytes: &[u8],
options: &ReadOptions,
export_options: &RagExportOptions,
visitor: F,
) -> Result<RagManifest>
where
F: FnMut(&RagChunk) -> Result<()>,
{
crate::rag::export_bytes(bytes, options, export_options, visitor)
}
pub fn query_as<T>(path: impl AsRef<Path>) -> Result<Box<dyn Iterator<Item = Result<T>> + Send>>
where
T: DeserializeOwned + 'static,
{
Self::query_as_with_options(path, &ReadOptions::default())
}
pub fn query_as_with_options<T>(
path: impl AsRef<Path>,
options: &ReadOptions,
) -> Result<Box<dyn Iterator<Item = Result<T>> + Send>>
where
T: DeserializeOwned + 'static,
{
Ok(Box::new(StreamingTypedRows::open(path, options)?))
}
pub fn save_as(path: impl AsRef<Path>, rows: &[DynamicRow]) -> Result<()> {
Self::save_as_with_options(path, rows, &WriteOptions::default())
}
pub fn save_as_with_options(
path: impl AsRef<Path>,
rows: &[DynamicRow],
options: &WriteOptions,
) -> Result<()> {
let mut writer = XlsxWriter::new();
writer.add_rows(rows, options)?;
writer.save(path)
}
pub fn save_as_bytes(rows: &[DynamicRow], options: &WriteOptions) -> Result<Vec<u8>> {
let mut writer = XlsxWriter::new();
writer.add_rows(rows, options)?;
writer.save_to_bytes()
}
pub fn save_as_with_schema(
path: impl AsRef<Path>,
schema: &[String],
rows: &[DynamicRow],
options: &WriteOptions,
) -> Result<()> {
let mut writer = XlsxWriter::new();
writer.add_rows_with_schema(schema, rows, options)?;
writer.save(path)
}
pub fn save_as_serialized<T>(path: impl AsRef<Path>, rows: &[T]) -> Result<()>
where
T: Serialize,
{
Self::save_as_serialized_with_options(path, rows, &WriteOptions::default())
}
pub fn save_as_serialized_with_options<T>(
path: impl AsRef<Path>,
rows: &[T],
options: &WriteOptions,
) -> Result<()>
where
T: Serialize,
{
let mut writer = XlsxWriter::new();
writer.add_serialized(rows, options)?;
writer.save(path)
}
}