#[cfg(feature = "formulas")]
use crate::formula::CustomFunctions;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
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))
}
}
#[derive(Default, Clone, Copy)]
pub struct Options<'a> {
progress: Option<&'a dyn Fn(Progress<'_>)>,
#[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
}
}
#[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())
.finish()
}
}