grrs-masterbongo 0.1.0

A simple implementation of grep
Documentation
use anyhow::{Context, Result};
use clap::Parser;
use std::env;
use std::path::PathBuf;
use std::thread;
use std::time::Duration;

/// A grep clone
#[derive(Debug, Parser)]
struct Cli {
    /// the pattern to look for
    pattern: String,
    /// the path to the file
    path: PathBuf,
    /// slow or not
    #[arg(short, long, default_value_t = false)]
    slow: bool,
    /// enable verbose output
    #[arg(short, long, default_value_t = false)]
    verbose: bool,
}

fn slow_func() {
    const N: u64 = 10;

    let pb = indicatif::ProgressBar::new(N);
    for _ in 0..N {
        thread::sleep(Duration::from_secs(1));
        pb.inc(1);
    }
    pb.finish_with_message("done");
}

// using anyhow it saves us from using Result<(), Box<dyn std::error::Error>>
fn main() -> Result<()> {
    let args = Cli::parse();

    if args.verbose {
        env::set_var("RUST_LOG", "debug");
    }
    // log level can be controlled via RUST_LOG env var
    env_logger::init();

    log::debug!("grrs starting up...");
    // context gives / shows us the original error, in this case std::io::Error
    let content = std::fs::read_to_string(&args.path)
        .with_context(|| format!("Error reading file: {:?}", &args.path))?;

    log::debug!("{:?}", args);

    grrs_masterbongo::find_matches(&content, &args.pattern, std::io::stdout());

    if args.slow {
        slow_func();
    }

    Ok(())
}