routee-compass-core 0.3.0

The core routing algorithms and data structures of the RouteE-Compass energy-aware routing engine
Documentation
use kdam::Bar;
use kdam::BarExt;

pub type ProgressBarOp<'a, R, E> = Box<dyn FnMut(Box<dyn FnMut()>) -> Result<R, E> + 'a>;

/// runs some closure expression which accepts a zero-arity function
/// callback. the callback should be invoked at the inner loop of
/// whatever iterative operation takes place, as it will trigger a
/// progress bar update. the callback should be called exactly "count" times.
pub fn with_progress_bar<'a, R, E>(
    mut closure: ProgressBarOp<'a, R, E>,
    error: Box<dyn Fn(String) -> E>,
    count: usize,
    message: String,
    animation: String,
) -> Result<R, E> {
    let mut pb = Bar::builder()
        .total(count)
        .animation(animation.as_str())
        .desc(message)
        .build()
        .map_err(|e| error(e))?;
    let cb = Box::new(move || {
        pb.update(1);
    });

    let result = closure(cb);

    print!("\n"); // create a newline once the progress bar is complete

    return result;
}