tiempo-rs/src/commands/resume.rs

148 lines
4.2 KiB
Rust
Raw Normal View History

2021-07-26 16:55:24 -05:00
use std::convert::TryFrom;
2021-08-25 14:30:27 -05:00
use std::io::{BufRead, Write};
2021-07-26 16:55:24 -05:00
use clap::ArgMatches;
use chrono::{DateTime, Utc};
use crate::error::{Error, Result};
use crate::timeparse::parse_time;
use crate::database::Database;
2021-07-28 20:51:33 -05:00
use crate::models::Entry;
2021-08-25 14:30:27 -05:00
use crate::io::Streams;
2021-07-26 16:55:24 -05:00
2021-08-25 14:30:27 -05:00
use super::{Command, Facts, r#in, sheet};
2021-07-26 16:55:24 -05:00
#[derive(Default)]
pub struct Args {
id: Option<u64>,
at: Option<DateTime<Utc>>,
}
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
type Error = Error;
fn try_from(matches: &'a ArgMatches) -> Result<Self> {
Ok(Args {
2021-12-13 14:20:56 -06:00
at: matches.value_of("at").map(parse_time).transpose()?,
2021-07-26 16:55:24 -05:00
id: matches.value_of("id").map(|i| i.parse().unwrap()),
})
}
}
2021-08-25 14:30:27 -05:00
fn resume<D, I, O, E>(args: Args, streams: &mut Streams<D, I, O, E>, facts: &Facts, entry: Entry) -> Result<()>
2021-07-28 20:51:33 -05:00
where
D: Database,
2021-08-25 14:30:27 -05:00
I: BufRead,
2021-07-28 20:51:33 -05:00
O: Write,
E: Write,
{
writeln!(
2021-08-25 14:30:27 -05:00
streams.out,
2021-07-28 20:51:33 -05:00
"Resuming \"{}\" from entry #{}",
2021-08-02 19:10:34 -05:00
entry.note.clone().unwrap_or_else(|| "".to_owned()), entry.id
2021-07-28 20:51:33 -05:00
)?;
r#in::InCommand::handle(r#in::Args {
at: args.at,
note: entry.note,
2021-08-25 14:30:27 -05:00
}, streams, facts)
2021-07-28 20:51:33 -05:00
}
2021-07-26 16:55:24 -05:00
pub struct ResumeCommand;
impl<'a> Command<'a> for ResumeCommand {
type Args = Args;
2021-08-25 14:30:27 -05:00
fn handle<D, I, O, E>(args: Args, streams: &mut Streams<D, I, O, E>, facts: &Facts) -> Result<()>
2021-07-26 16:55:24 -05:00
where
D: Database,
2021-08-25 14:30:27 -05:00
I: BufRead,
2021-07-26 16:55:24 -05:00
O: Write,
E: Write,
{
2021-08-25 14:30:27 -05:00
let current_sheet = streams.db.current_sheet()?.unwrap_or_else(|| "default".to_owned());
2021-07-26 16:55:24 -05:00
2021-07-28 20:51:33 -05:00
// First try to process using the given id
if let Some(entry_id) = args.id {
2021-08-25 14:30:27 -05:00
if let Some(entry) = streams.db.entry_by_id(entry_id)? {
2021-07-28 20:51:33 -05:00
if entry.sheet != current_sheet {
// first swith to the sheet
sheet::SheetCommand::handle(sheet::Args {
sheet: Some(entry.sheet.clone()),
2021-08-25 14:30:27 -05:00
}, streams, facts)?;
2021-07-28 20:51:33 -05:00
}
2021-08-25 14:30:27 -05:00
return resume(args, streams, facts, entry);
2021-07-26 16:55:24 -05:00
} else {
2021-08-25 14:30:27 -05:00
writeln!(streams.out, "The entry with id '{}' could not be found to be resumed. Perhaps it was deleted?", entry_id)?;
2021-07-26 16:55:24 -05:00
2021-07-28 20:51:33 -05:00
return Ok(());
2021-07-26 16:55:24 -05:00
}
2021-07-28 20:51:33 -05:00
}
// No id specified, try to find something suitable to switch to in the
// database
2021-08-25 14:30:27 -05:00
if let Some(entry) = streams.db.last_checkout_of_sheet(&current_sheet)? {
resume(args, streams, facts, entry)
2021-07-26 16:55:24 -05:00
} else {
2021-08-25 14:30:27 -05:00
writeln!(streams.out, "No entry to resume in the sheet '{}'. Perhaps start a new one?
2021-07-28 20:51:33 -05:00
Hint: use t in", current_sheet)?;
2021-07-26 16:55:24 -05:00
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use chrono::Duration;
2021-07-28 20:51:33 -05:00
use pretty_assertions::assert_eq;
2021-07-26 16:55:24 -05:00
2021-08-03 20:56:40 -05:00
use crate::test_utils::Ps;
2021-07-26 16:55:24 -05:00
use super::*;
#[test]
fn resume_an_entry() {
let args = Default::default();
2021-08-25 14:30:27 -05:00
let mut streams = Streams::fake(b"");
let facts = Facts::new();
let one_hour_ago = facts.now - Duration::hours(1);
let two_hours_ago = facts.now - Duration::hours(2);
2021-07-26 16:55:24 -05:00
2021-08-25 14:30:27 -05:00
streams.db.entry_insert(two_hours_ago, Some(one_hour_ago), Some("fake note".into()), "default").unwrap();
2021-07-26 16:55:24 -05:00
2021-08-25 14:30:27 -05:00
assert_eq!(streams.db.entries_full(None, None).unwrap().len(), 1);
2021-07-26 16:55:24 -05:00
2021-08-25 14:30:27 -05:00
ResumeCommand::handle(args, &mut streams, &facts).unwrap();
2021-07-26 16:55:24 -05:00
2021-08-25 14:30:27 -05:00
let all_entries = streams.db.entries_full(None, None).unwrap();
2021-07-26 16:55:24 -05:00
assert_eq!(all_entries.len(), 2);
assert_eq!(all_entries[1].id, 2);
2021-08-25 14:30:27 -05:00
assert_eq!(all_entries[1].start, facts.now);
2021-07-26 16:55:24 -05:00
assert_eq!(all_entries[1].end, None);
assert_eq!(all_entries[1].note, Some("fake note".into()));
assert_eq!(all_entries[1].sheet, "default");
2021-08-25 14:30:27 -05:00
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("Resuming \"fake note\" from entry #1
2021-07-26 16:55:24 -05:00
Checked into sheet \"default\".\n"));
2021-08-25 14:30:27 -05:00
assert_eq!(Ps(&String::from_utf8_lossy(&streams.err)), Ps(""));
2021-07-26 16:55:24 -05:00
}
#[test]
fn no_entries_to_resume() {
let args = Default::default();
2021-08-25 14:30:27 -05:00
let mut streams = Streams::fake(b"");
let facts = Facts::new();
2021-07-26 16:55:24 -05:00
2021-08-25 14:30:27 -05:00
ResumeCommand::handle(args, &mut streams, &facts).unwrap();
2021-07-26 16:55:24 -05:00
2021-08-25 14:30:27 -05:00
assert_eq!(Ps(&String::from_utf8_lossy(&streams.out)), Ps("\
2021-07-28 20:51:33 -05:00
No entry to resume in the sheet 'default'. Perhaps start a new one?
Hint: use t in
2021-07-26 16:55:24 -05:00
"));
}
}