pass stderr to commands

This commit is contained in:
Abraham Toriz 2021-06-29 12:02:33 -05:00
parent d8cd301888
commit c7de36e3f8
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
4 changed files with 14 additions and 9 deletions

View File

@ -13,5 +13,5 @@ pub mod display;
pub trait Command<'a> {
type Args: TryFrom<&'a ArgMatches<'a>>;
fn handle<D: Database, W: Write>(args: Self::Args, db: &mut D, out: &mut W, config: &Config) -> error::Result<()>;
fn handle<D: Database, O: Write, E: Write>(args: Self::Args, db: &mut D, out: &mut O, err: &mut E, config: &Config) -> error::Result<()>;
}

View File

@ -69,10 +69,11 @@ pub struct DisplayCommand {
impl<'a> Command<'a> for DisplayCommand {
type Args = Args;
fn handle<D, W>(args: Self::Args, db: &mut D, out: &mut W, config: &Config) -> error::Result<()>
fn handle<D, O, E>(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()?,

View File

@ -31,10 +31,11 @@ pub struct InCommand {}
impl<'a> Command<'a> for InCommand {
type Args = Args;
fn handle<D, W>(args: Args, db: &mut D, out: &mut W, config: &Config) -> error::Result<()>
fn handle<D, O, E>(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");
}

View File

@ -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())),
}
}