#[cfg(feature = "formulas")]
use crate::formula::CustomFunctions;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Stage {
Reading,
Writing,
Recalculating,
}
#[derive(Debug, Clone, Copy)]
pub struct Progress<'a> {
pub stage: Stage,
pub done: usize,
pub total: Option<usize>,
pub what: &'a str,
}
impl Progress<'_> {
#[must_use]
pub fn fraction(&self) -> Option<f64> {
let total = self.total?;
if total == 0 {
return Some(1.0);
}
#[expect(
clippy::cast_precision_loss,
reason = "counts of sheets, parts and formulas are far below 2^53"
)]
Some((self.done as f64) / (total as f64))
}
}
#[non_exhaustive]
pub struct RowBatch<'a> {
pub row: crate::coordinate::Row,
pub sheet: &'a crate::model::Worksheet,
pub styles: &'a crate::style::StyleTable,
pub epoch: crate::shared::date::Epoch,
}
pub type RowSink<'a> = &'a dyn Fn(&RowBatch<'_>);
#[derive(Default, Clone, Copy)]
pub struct Options<'a> {
progress: Option<&'a dyn Fn(Progress<'_>)>,
rows: Option<(usize, RowSink<'a>)>,
password: Option<&'a str>,
#[cfg(feature = "formulas")]
functions: Option<&'a CustomFunctions>,
#[cfg(not(feature = "formulas"))]
lifetime: std::marker::PhantomData<&'a ()>,
}
impl<'a> Options<'a> {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn reporting(self, report: &'a dyn Fn(Progress<'_>)) -> Self {
Self {
progress: Some(report),
..self
}
}
#[must_use]
pub fn streaming(self, sheet: usize, sink: RowSink<'a>) -> Self {
Self {
rows: Some((sheet, sink)),
..self
}
}
#[must_use]
pub fn password(self, password: &'a str) -> Self {
Self {
password: Some(password),
..self
}
}
#[must_use]
pub const fn given_password(&self) -> Option<&'a str> {
self.password
}
#[must_use]
pub fn streamed(&self) -> Option<(usize, RowSink<'a>)> {
self.rows
}
#[must_use]
pub fn row_sink(&self, sheet: usize) -> Option<RowSink<'a>> {
self.rows
.and_then(|(wanted, sink)| (wanted == sheet).then_some(sink))
}
#[cfg(feature = "formulas")]
#[must_use]
pub fn with_functions(self, functions: &'a CustomFunctions) -> Self {
Self {
functions: Some(functions),
..self
}
}
#[cfg(feature = "formulas")]
#[must_use]
pub const fn functions(&self) -> Option<&'a CustomFunctions> {
self.functions
}
pub fn report(&self, stage: Stage, done: usize, total: Option<usize>, what: &str) {
if let Some(report) = self.progress {
report(Progress {
stage,
done,
total,
what,
});
}
}
}
impl std::fmt::Debug for Options<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Options")
.field("reporting", &self.progress.is_some())
.field("streaming", &self.rows.map(|(sheet, _)| sheet))
.finish()
}
}