fn run(opt: Opt) -> Result<(), std::io::Error> {
    match opt.cmd {
        Command::Add { name, email } => {
            let mut recs = load_records(opt.data_file.clone(), opt.verbose)?;
            let next_id = recs.next_id();
            recs.add(Record {
                id: next_id,
                name,
                email,
            });
            save_records(opt.data_file, recs)?;
        }
        Command::Edit { id, name, email } => {
            let mut recs = load_records(opt.data_file.clone(), opt.verbose)?;
            recs.edit(id, &name, email);
            save_records(opt.data_file, recs)?;
        }
        Command::List { .. } => {
            let recs = load_records(opt.data_file, opt.verbose)?;
            for record in recs.into_vec() {
                println!("{:?}", record);
            }
        }
        Command::Remove { id } => {
            let mut recs = load_records(opt.data_file.clone(), opt.verbose)?;
            if recs.remove(id).is_some() {
                save_records(opt.data_file, recs)?;
                println!("record deleted");
            } else {
                println!("record not found");
            }
        }
        Command::Search { query } => {
            let recs = load_records(opt.data_file, opt.verbose)?;
            let results = recs.search(&query);
            if results.is_empty() {
                println!("no records found");
            } else {
                for rec in results {
                    println!("{:?}", rec);
                }
            }
        }
    }
    Ok(())
}
/// Runs the program. This is so we can utilize the question mark operator.
Runs the program. This is so we can utilize the question mark operator.
Runs the program . This is so we can utilize the question mark operator .
fn run(opt: Opt) -> Result<(), std::io::Error> {
    match opt.cmd {
        Command::Add { name, email } => {
            let mut recs = load_records(opt.data_file.clone(), opt.verbose)?;
            let next_id = recs.next_id();
            recs.add(Record {
                id: next_id,
                name,
                email,
            });
            save_records(opt.data_file, recs)?;
        }
        Command::Edit { id, name, email } => {
            let mut recs = load_records(opt.data_file.clone(), opt.verbose)?;
            recs.edit(id, &name, email);
            save_records(opt.data_file, recs)?;
        }
        Command::List { .. } => {
            let recs = load_records(opt.data_file, opt.verbose)?;
            for record in recs.into_vec() {
                println!("{:?}", record);
            }
        }
        Command::Remove { id } => {
            let mut recs = load_records(opt.data_file.clone(), opt.verbose)?;
            if recs.remove(id).is_some() {
                save_records(opt.data_file, recs)?;
                println!("record deleted");
            } else {
                println!("record not found");
            }
        }
        Command::Search { query } => {
            let recs = load_records(opt.data_file, opt.verbose)?;
            let results = recs.search(&query);
            if results.is_empty() {
                println!("no records found");
            } else {
                for rec in results {
                    println!("{:?}", rec);
                }
            }
        }
    }
    Ok(())
}
fn run ( opt : Opt ) -> Result < ( ) , std :: io :: Error > { match opt . cmd { Command :: Add { name , email } => { let mut recs = load_records ( opt . data_file . clone ( ) , opt . verbose ) ? ; let next_id = recs . next_id ( ) ; recs . add ( Record { id : next_id , name , email , } ) ; save_records ( opt . data_file , recs ) ? ; } Command :: Edit { id , name , email } => { let mut recs = load_records ( opt . data_file . clone ( ) , opt . verbose ) ? ; recs . edit ( id , & name , email ) ; save_records ( opt . data_file , recs ) ? ; } Command :: List { .. } => { let recs = load_records ( opt . data_file , opt . verbose ) ? ; for record in recs . into_vec ( ) { println ! ( " " record ) ; } } Command :: Remove { id } => { let mut recs = load_records ( opt . data_file . clone ( ) , opt . verbose ) ? ; if recs . remove ( id ) . is_some ( ) { save_records ( opt . data_file , recs ) ? ; println ! ( " " ) ; } else { println ! ( " " ) ; } } Command :: Search { query } => { let recs = load_records ( opt . data_file , opt . verbose ) ? ; let results = recs . search ( & query ) ; if results . is_empty ( ) { println ! ( " " ) ; } else { for rec in results { println ! ( " " rec ) ; } } } } Ok ( ( ) ) }
Runs the program.
Runs the program .
opt
Opt