use super::FlashLayout;
use std::time::Duration;
pub struct FlashProgress<'a> {
handler: Box<dyn FnMut(ProgressEvent) + 'a>,
}
impl<'a> Default for FlashProgress<'a> {
fn default() -> Self {
Self::empty()
}
}
impl<'a> FlashProgress<'a> {
pub fn new(handler: impl FnMut(ProgressEvent) + 'a) -> Self {
Self {
handler: Box::new(handler),
}
}
pub fn empty() -> Self {
Self {
handler: Box::new(|_| {}),
}
}
pub fn emit(&mut self, event: ProgressEvent) {
(self.handler)(event);
}
pub(super) fn initialized(&mut self, phases: Vec<FlashLayout>) {
self.emit(ProgressEvent::FlashLayoutReady {
flash_layout: phases,
});
}
pub(super) fn add_progress_bar(&mut self, operation: ProgressOperation, total: Option<u64>) {
self.emit(ProgressEvent::AddProgressBar { operation, total });
}
pub(super) fn started(&mut self, operation: ProgressOperation) {
self.emit(ProgressEvent::Started(operation));
}
pub(super) fn progressed(&mut self, operation: ProgressOperation, size: u64, time: Duration) {
self.emit(ProgressEvent::Progress {
operation,
size,
time,
});
}
pub(super) fn failed(&mut self, operation: ProgressOperation) {
self.emit(ProgressEvent::Failed(operation));
}
pub(super) fn finished(&mut self, operation: ProgressOperation) {
self.emit(ProgressEvent::Finished(operation));
}
pub(super) fn message(&mut self, message: String) {
self.emit(ProgressEvent::DiagnosticMessage { message });
}
pub(super) fn started_erasing(&mut self) {
self.started(ProgressOperation::Erase);
}
pub(super) fn started_filling(&mut self) {
self.started(ProgressOperation::Fill);
}
pub(super) fn started_programming(&mut self) {
self.started(ProgressOperation::Program);
}
pub(crate) fn started_verifying(&mut self) {
self.started(ProgressOperation::Verify);
}
pub(super) fn sector_erased(&mut self, size: u64, time: Duration) {
self.progressed(ProgressOperation::Erase, size, time);
}
pub(super) fn page_filled(&mut self, size: u64, time: Duration) {
self.progressed(ProgressOperation::Fill, size, time);
}
pub(super) fn page_programmed(&mut self, size: u64, time: Duration) {
self.progressed(ProgressOperation::Program, size, time);
}
pub(super) fn page_verified(&mut self, size: u64, time: Duration) {
self.progressed(ProgressOperation::Verify, size, time);
}
pub(super) fn failed_erasing(&mut self) {
self.failed(ProgressOperation::Erase);
}
pub(super) fn failed_filling(&mut self) {
self.failed(ProgressOperation::Fill);
}
pub(super) fn failed_programming(&mut self) {
self.failed(ProgressOperation::Program);
}
pub(super) fn failed_verifying(&mut self) {
self.failed(ProgressOperation::Verify);
}
pub(super) fn finished_programming(&mut self) {
self.finished(ProgressOperation::Program);
}
pub(super) fn finished_erasing(&mut self) {
self.finished(ProgressOperation::Erase);
}
pub(super) fn finished_filling(&mut self) {
self.finished(ProgressOperation::Fill);
}
pub(super) fn finished_verifying(&mut self) {
self.finished(ProgressOperation::Verify);
}
}
#[derive(Clone, Copy, Debug)]
pub enum ProgressOperation {
Fill,
Erase,
Program,
Verify,
}
#[derive(Debug, Clone)]
pub enum ProgressEvent {
FlashLayoutReady {
flash_layout: Vec<FlashLayout>,
},
AddProgressBar {
operation: ProgressOperation,
total: Option<u64>,
},
Started(ProgressOperation),
Progress {
operation: ProgressOperation,
size: u64,
time: Duration,
},
Failed(ProgressOperation),
Finished(ProgressOperation),
DiagnosticMessage {
message: String,
},
}