2021-06-18 11:27:19 -05:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
use clap::ArgMatches;
|
|
|
|
|
2021-06-21 11:12:30 -05:00
|
|
|
use crate::database::Database;
|
|
|
|
use crate::error;
|
|
|
|
use crate::types::Time;
|
|
|
|
use crate::editor;
|
|
|
|
use crate::commands::Command;
|
2021-06-21 17:38:51 -05:00
|
|
|
use crate::config::Config;
|
2021-06-18 11:27:19 -05:00
|
|
|
|
|
|
|
pub struct Args {
|
|
|
|
at: Option<Time>,
|
|
|
|
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;
|
|
|
|
|
2021-06-21 17:38:51 -05:00
|
|
|
fn handle<D, W>(args: Args, db: &mut D, out: &mut W, config: &Config) -> error::Result<()>
|
2021-06-18 11:27:19 -05:00
|
|
|
where
|
|
|
|
D: Database,
|
|
|
|
W: Write,
|
|
|
|
{
|
|
|
|
let at = args.at.unwrap_or(Time::now());
|
|
|
|
let note = if let Some(note) = args.note {
|
|
|
|
note
|
|
|
|
} else {
|
2021-06-21 19:54:10 -05:00
|
|
|
editor::get_string(config)?
|
2021-06-18 11:27:19 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
db.entry_insert(at, note)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::database::SqliteDatabase;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_handles_new_entry() {
|
|
|
|
let mut d = SqliteDatabase::from_memory().unwrap();
|
|
|
|
let args = Args {
|
|
|
|
at: None,
|
|
|
|
note: Some("hola".into()),
|
|
|
|
};
|
2021-06-21 11:12:30 -05:00
|
|
|
let mut out = Vec::new();
|
2021-06-18 11:27:19 -05:00
|
|
|
|
|
|
|
assert!(false, "there are no entries");
|
|
|
|
|
2021-06-21 17:38:51 -05:00
|
|
|
InCommand::handle(args, &mut d, &mut out, &Default::default()).unwrap();
|
2021-06-18 11:27:19 -05:00
|
|
|
|
|
|
|
assert!(false, "there is one entry");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
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();
|
|
|
|
|
|
|
|
assert!(false, "there are no entries");
|
|
|
|
|
2021-06-21 17:38:51 -05:00
|
|
|
InCommand::handle(args, &mut d, &mut out, &Default::default()).unwrap();
|
2021-06-18 11:27:19 -05:00
|
|
|
|
|
|
|
assert!(false, "there are still no entries");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_no_time_given_uses_now() {
|
|
|
|
assert!(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sets_given_time() {
|
|
|
|
assert!(false);
|
|
|
|
}
|
|
|
|
}
|