dwh 0.7.1

A library to use the digitronic protocol dwh
Documentation
use clap::Parser;
use dwh::{
    backup::{BackupArgs, DownloadEvent},
    zip::ZipEvent,
    FileWalkEvent,
};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};

fn progress_style() -> ProgressStyle {
    ProgressStyle::with_template(
        "{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes}",
    )
    .unwrap()
    .progress_chars("#>-")
}

fn progress_bar(len: u64) -> ProgressBar {
    let p = ProgressBar::new(len);
    p.set_style(progress_style());
    p
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let backup_args = BackupArgs::parse();
    let m = MultiProgress::new();
    let progress = m.add(progress_bar(0));
    let progress_total = m.add(progress_bar(0));

    //m.add()
    let result = dwh::backup::backup(backup_args, &|event| match event {
        dwh::backup::BackupEvent::FileWalkEvent(event) => match event {
            FileWalkEvent::FetchDirectoryLines(dir, count) => {
                m.println(format!("Fetch directory lines: {dir} -> {count}"))
                    .ok();
            }
            FileWalkEvent::DirectoryLinesFound(dir, count) => {
                m.println(format!("Found items in directory: {dir} -> {count}"))
                    .ok();
            }
        },
        dwh::backup::BackupEvent::DownloadEvent(event) => match event {
            DownloadEvent::TotalSize(bytes) => {
                progress_total.set_length(bytes as u64);
            }
            DownloadEvent::TotalProgress(bytes) => {
                progress_total.set_position(bytes as u64);
            }
            DownloadEvent::DownloadFile(filename, bytes) => {
                progress.set_length(bytes as u64);
                m.println(format!("Download file: {filename}")).ok();
            }
            DownloadEvent::DownloadFileProgress(bytes) => {
                progress.set_position(bytes as u64);
            }
        },
        dwh::backup::BackupEvent::ZipEvent(event) => match event {
            ZipEvent::TotalSize(bytes) => {
                progress_total.set_length(bytes as u64);
            }
            ZipEvent::TotalProgress(bytes) => {
                progress_total.set_position(bytes as u64);
            }
            ZipEvent::ZipFile(filename, bytes) => {
                progress.set_length(bytes as u64);
                m.println(format!("zip file {filename}")).ok();
            }
            ZipEvent::ZipFileProgress(bytes) => progress.set_position(bytes as u64),
            ZipEvent::ZipDir(directory_name) => {
                m.println(format!("zip directory {directory_name}")).ok();
            }
        },
    })
    .await?;
    if result.failed_files.len() > 0 {
        println!("These files failed to download:");
        for file in result.failed_files {
            println!("{}", file)
        }
        anyhow::bail!("Returning with error because some files failed");
    }
    Ok(())
}