From c7de36e3f87ef95e644f90c282bb1572b557bcac Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Tue, 29 Jun 2021 12:02:33 -0500 Subject: [PATCH] pass stderr to commands --- src/commands.rs | 2 +- src/commands/display.rs | 5 +++-- src/commands/in.rs | 11 +++++++---- src/main.rs | 5 +++-- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 852e401..bc6a60e 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -13,5 +13,5 @@ pub mod display; pub trait Command<'a> { type Args: TryFrom<&'a ArgMatches<'a>>; - fn handle(args: Self::Args, db: &mut D, out: &mut W, config: &Config) -> error::Result<()>; + fn handle(args: Self::Args, db: &mut D, out: &mut O, err: &mut E, config: &Config) -> error::Result<()>; } diff --git a/src/commands/display.rs b/src/commands/display.rs index 3a2adf5..358c58b 100644 --- a/src/commands/display.rs +++ b/src/commands/display.rs @@ -69,10 +69,11 @@ pub struct DisplayCommand { impl<'a> Command<'a> for DisplayCommand { type Args = Args; - fn handle(args: Self::Args, db: &mut D, out: &mut W, config: &Config) -> error::Result<()> + fn handle(args: Self::Args, db: &mut D, out: &mut O, err: &mut E, config: &Config) -> error::Result<()> where D: Database, - W: Write, + O: Write, + E: Write, { let sheets_to_display = match args.sheet { Some(Sheet::All) => db.entries_all_visible()?, diff --git a/src/commands/in.rs b/src/commands/in.rs index 7b8a245..3736897 100644 --- a/src/commands/in.rs +++ b/src/commands/in.rs @@ -31,10 +31,11 @@ pub struct InCommand {} impl<'a> Command<'a> for InCommand { type Args = Args; - fn handle(args: Args, db: &mut D, out: &mut W, config: &Config) -> error::Result<()> + fn handle(args: Args, db: &mut D, _out: &mut O, _err: &mut E, config: &Config) -> error::Result<()> where D: Database, - W: Write, + O: Write, + E: Write, { let at = args.at.unwrap_or(Utc::now()); let note = if let Some(note) = args.note { @@ -63,10 +64,11 @@ mod tests { note: Some("hola".into()), }; let mut out = Vec::new(); + let mut err = Vec::new(); assert!(false, "there are no entries"); - InCommand::handle(args, &mut d, &mut out, &Default::default()).unwrap(); + InCommand::handle(args, &mut d, &mut out, &mut err, &Default::default()).unwrap(); assert!(false, "there is one entry"); } @@ -79,10 +81,11 @@ mod tests { note: Some("hola".into()), }; let mut out = Vec::new(); + let mut err = Vec::new(); assert!(false, "there are no entries"); - InCommand::handle(args, &mut d, &mut out, &Default::default()).unwrap(); + InCommand::handle(args, &mut d, &mut out, &mut err, &Default::default()).unwrap(); assert!(false, "there are still no entries"); } diff --git a/src/main.rs b/src/main.rs index 0666ea8..7b6d286 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,10 +14,11 @@ fn error_trap(matches: ArgMatches) -> error::Result<()> { let mut conn = SqliteDatabase::from_path(&config.database_file)?; let mut out = io::stdout(); + let mut err = io::stderr(); match matches.subcommand() { - ("in", Some(matches)) => InCommand::handle(matches.try_into()?, &mut conn, &mut out, &config), - ("display", Some(matches)) => DisplayCommand::handle(matches.try_into()?, &mut conn, &mut out, &config), + ("in", Some(matches)) => InCommand::handle(matches.try_into()?, &mut conn, &mut out, &mut err, &config), + ("display", Some(matches)) => DisplayCommand::handle(matches.try_into()?, &mut conn, &mut out, &mut err, &config), (cmd, _) => Err(error::Error::UnimplementedCommand(cmd.into())), } }