tiempo-rs/src/commands/in.rs

109 lines
2.5 KiB
Rust

use std::convert::TryFrom;
use std::io::Write;
use clap::ArgMatches;
use chrono::{DateTime, Utc};
use crate::database::Database;
use crate::error;
use crate::editor;
use crate::commands::Command;
use crate::config::Config;
pub struct Args {
at: Option<DateTime<Utc>>,
note: Option<String>,
}
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
type Error = error::Error;
fn try_from(matches: &'a ArgMatches) -> Result<Self, Self::Error> {
Ok(Args {
at: matches.value_of("at").map(|s| s.parse()).transpose()?,
note: matches.value_of("note").map(|s| s.into()),
})
}
}
pub struct InCommand {}
impl<'a> Command<'a> for InCommand {
type Args = Args;
fn handle<D, O, E>(args: Args, db: &mut D, _out: &mut O, _err: &mut E, config: &Config) -> error::Result<()>
where
D: Database,
O: Write,
E: Write,
{
let start = args.at.unwrap_or(Utc::now());
let sheet = db.current_sheet()?.unwrap_or("default".into());
let note = if let Some(note) = args.note {
note
} else {
editor::get_string(config)?
};
db.entry_insert(start, None, Some(note), sheet)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::database::SqliteDatabase;
#[test]
#[ignore]
fn test_handles_new_entry() {
let mut d = SqliteDatabase::from_memory().unwrap();
let args = Args {
at: None,
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, &mut err, &Default::default()).unwrap();
assert!(false, "there is one entry");
}
#[test]
#[ignore]
fn test_handles_already_running_entry() {
let mut d = SqliteDatabase::from_memory().unwrap();
let args = Args {
at: None,
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, &mut err, &Default::default()).unwrap();
assert!(false, "there are still no entries");
assert!(false, "a message is issued to the user warning a running entry in this sheet");
}
#[test]
#[ignore]
fn test_with_no_time_given_uses_now() {
assert!(false);
}
#[test]
#[ignore]
fn test_sets_given_time() {
assert!(false);
}
}