pub trait Reporter: Clone + Send + Sync {
#[must_use]
fn fork(&self) -> Self {
self.to_owned()
}
#[must_use]
fn sub(&self) -> Self {
self.fork()
}
fn send(&self, state: ReportState);
fn send_mut(&mut self, state: ReportState) {
self.send(state)
}
}
pub(crate) trait Progress: Reporter {
fn set_message(&self, msg: String) {
self.send(ReportState::SetMessage(msg));
}
fn set_sub_message(&self, msg: String) {
self.send(ReportState::SetSubMessage(msg));
}
fn set_max_progress(&self, value: f64) {
self.send(ReportState::SetMaxProgress(value));
}
fn add_max_progress(&self, value: f64) {
self.send(ReportState::AddMaxProgress(value));
}
fn set_progress(&self, value: f64) {
self.send(ReportState::SetProgress(value));
}
fn add_progress(&self, value: f64) {
self.send(ReportState::AddProgress(value));
}
fn set_indeterminate_progress(&self) {
self.send(ReportState::SetIndeterminateProgress);
}
fn hide_progress(&self) {
self.send(ReportState::SetIndeterminateProgress);
}
fn remove_progress(self) {
self.send(ReportState::RemoveProgress);
}
}
impl<R: Reporter> Reporter for Option<R> {
fn fork(&self) -> Self {
self.as_ref().map(|s| s.fork())
}
fn sub(&self) -> Self {
self.as_ref().map(|s| s.sub())
}
fn send(&self, state: ReportState) {
if let Some(s) = &self {
s.send(state);
}
}
fn send_mut(&mut self, state: ReportState) {
if let Some(s) = self {
s.send_mut(state);
}
}
}
impl<R: Reporter> Progress for R {}
pub const NR: Option<()> = None;
impl Reporter for () {
fn send(&self, _: ReportState) {}
}
#[derive(Debug, Clone)]
pub enum ReportState {
SetMessage(String),
SetSubMessage(String),
SetMaxProgress(f64),
AddMaxProgress(f64),
SetProgress(f64),
AddProgress(f64),
SetIndeterminateProgress,
HideProgress,
RemoveProgress,
}