#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum MessageLevel {
Info,
Failure,
Success,
}
pub trait Progress {
type SubProgress: Progress;
fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress;
fn init(&mut self, max: Option<u32>, unit: Option<&'static str>);
fn set(&mut self, step: u32);
fn message(&mut self, level: MessageLevel, message: impl Into<String>);
fn info(&mut self, message: impl Into<String>) {
self.message(MessageLevel::Info, message)
}
fn done(&mut self, message: impl Into<String>) {
self.message(MessageLevel::Success, message)
}
fn fail(&mut self, message: impl Into<String>) {
self.message(MessageLevel::Failure, message)
}
}
pub struct Discard;
impl Progress for Discard {
type SubProgress = Discard;
fn add_child(&mut self, _name: impl Into<String>) -> Self::SubProgress {
Discard
}
fn init(&mut self, _max: Option<u32>, _unit: Option<&'static str>) {}
fn set(&mut self, _step: u32) {}
fn message(&mut self, _level: MessageLevel, _message: impl Into<String>) {}
}
pub enum Either<L, R> {
Left(L),
Right(R),
}
impl<L, R> Progress for Either<L, R>
where
L: Progress,
R: Progress,
{
type SubProgress = Either<L::SubProgress, R::SubProgress>;
fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress {
match self {
Either::Left(l) => Either::Left(l.add_child(name)),
Either::Right(r) => Either::Right(r.add_child(name)),
}
}
fn init(&mut self, max: Option<u32>, unit: Option<&'static str>) {
match self {
Either::Left(l) => l.init(max, unit),
Either::Right(r) => r.init(max, unit),
}
}
fn set(&mut self, step: u32) {
match self {
Either::Left(l) => l.set(step),
Either::Right(r) => r.set(step),
}
}
fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
match self {
Either::Left(l) => l.message(level, message),
Either::Right(r) => r.message(level, message),
}
}
}
pub struct DoOrDiscard<T>(Either<T, Discard>);
impl<T> From<Option<T>> for DoOrDiscard<T>
where
T: Progress,
{
fn from(p: Option<T>) -> Self {
match p {
Some(p) => DoOrDiscard(Either::Left(p)),
None => DoOrDiscard(Either::Right(Discard)),
}
}
}
impl<T> Progress for DoOrDiscard<T>
where
T: Progress,
{
type SubProgress = DoOrDiscard<T::SubProgress>;
fn add_child(&mut self, name: impl Into<String>) -> Self::SubProgress {
DoOrDiscard(self.0.add_child(name))
}
fn init(&mut self, max: Option<u32>, unit: Option<&'static str>) {
self.0.init(max, unit)
}
fn set(&mut self, step: u32) {
self.0.set(step)
}
fn message(&mut self, level: MessageLevel, message: impl Into<String>) {
self.0.message(level, message)
}
}
#[cfg(feature = "progress-log")]
mod log;
#[cfg(feature = "progress-log")]
pub use self::log::Log;
#[cfg(feature = "progress-prodash")]
mod prodash;