61 lines
1.6 KiB
Rust
61 lines
1.6 KiB
Rust
use indicatif::{ProgressBar, ProgressStyle};
|
|
use std::{error::Error, future::Future};
|
|
|
|
pub async fn with_spinner<F, Fut, T>(
|
|
start_msg: &str,
|
|
finish_msg: &str,
|
|
f: F,
|
|
) -> Result<T, Box<dyn Error>>
|
|
where
|
|
F: FnOnce() -> Fut,
|
|
Fut: Future<Output = Result<T, Box<dyn Error>>>,
|
|
{
|
|
let pb = ProgressBar::new_spinner();
|
|
pb.set_message(start_msg.to_string());
|
|
pb.enable_steady_tick(std::time::Duration::from_millis(100));
|
|
|
|
let result = f().await;
|
|
|
|
pb.finish_and_clear();
|
|
if !finish_msg.is_empty() {
|
|
println!("{} {}", dialoguer::console::style("✔").green(), finish_msg);
|
|
}
|
|
|
|
result
|
|
}
|
|
|
|
pub async fn with_progressbar<F, Fut, T>(
|
|
start_msg: &str,
|
|
finish_msg: &str,
|
|
len: u64,
|
|
f: F,
|
|
) -> Result<T, Box<dyn Error>>
|
|
where
|
|
F: FnOnce(ProgressBar) -> Fut,
|
|
Fut: Future<Output = Result<T, Box<dyn Error>>>,
|
|
{
|
|
let pb = ProgressBar::new(len);
|
|
pb.set_style(
|
|
ProgressStyle::default_bar()
|
|
.template("{msg}\n{bar:40.green/white} {bytes}/{total_bytes} ({bytes_per_sec})")?,
|
|
);
|
|
pb.set_message(start_msg.to_string());
|
|
|
|
let result = f(pb.clone()).await;
|
|
let formatted_finish_msg = format!("{} {}", dialoguer::console::style("✔").green(), finish_msg);
|
|
pb.finish_with_message(formatted_finish_msg);
|
|
|
|
result
|
|
}
|
|
|
|
pub fn verify_sha1(data: &[u8], expected_hex: &str) -> bool {
|
|
let mut hasher = Sha1::new();
|
|
hasher.update(data);
|
|
hex::encode(hasher.finalize()) == expected_hex
|
|
}
|
|
|
|
pub fn verify_sha512(data: &[u8], expected_hex: &str) -> bool {
|
|
let mut hasher = Sha512::new();
|
|
hasher.update(data);
|
|
hex::encode(hasher.finalize()) == expected_hex
|
|
}
|