Skip to content

Instantly share code, notes, and snippets.

@kbknapp
Last active April 3, 2017 18:18
Show Gist options
  • Save kbknapp/43461effd98399c3bbf48c81913a77d3 to your computer and use it in GitHub Desktop.
Save kbknapp/43461effd98399c3bbf48c81913a77d3 to your computer and use it in GitHub Desktop.
fn main() {
if let Err(e) = run(&matches) {
println!("error: {}", e);
process::exit(1);
}
}
fn run(matches: &ArgMatches) -> Result<(), &'static str> {
let mut sheet = handle_init(&matches, timesheet::Timesheet::load_from_file())?;
match matches.subcommand() {
("begin", Some(..)) => {
t_sheet.new_session();
}
("end", Some(..)) => {
sheet.finalize_last();
}
("pause", Some(..)) => {
if !sheet.push_event(timesheet::Event::Pause {
time: timesheet::get_seconds(),
}) {
return Err("Can't pause now!");
}
}
("proceed", Some(..)) => {
if !sheet.push_event(timesheet::Event::Proceed {
time: timesheet::get_seconds(),
}) {
return Err("Can't proceed now!");
}
}
("meta", Some(sub_arg)) => {
let metatext = sub_arg.value_of("text").unwrap();
if !sheet.push_event(timesheet::Event::Meta { text: metatext.to_string() }) {
return Err("Can't meta now!");
}
}
("commit", Some(sub_arg)) => {
let commit_hash = sub_arg.value_of("hash").unwrap();
let hash_parsed = u64::from_str_radix(commit_hash, 16).unwrap();
if !sheet.push_event(timesheet::Event::Commit { hash: hash_parsed }) {
return Err("Can't commit now!");
}
}
("branch", Some(sub_arg)) => {
let branch_name = sub_arg.value_of("name").unwrap();
if !sheet.push_event(timesheet::Event::Branch {
name: branch_name.to_string(),
}) {
return Err("Can't change branch now!");
}
}
("status", Some(which)) => {
match which.value_of("which") {
Some("session") => println!("{:?}", sheet.last_session_status()),
Some("sheet") => println!("{:?}", sheet.timesheet_status()),
Some(text) => println!("What do you mean by {}?", text),
None => {}
}
}
("clear", Some(..)) => {
println!("Clearing sessions!");
timesheet::Timesheet::clear_sessions();
}
_ => {unreachable!()}
}
Ok(())
}
fn handle_init(matches: &ArgMatches, t_sheet: Option<timesheet::TimeSheet>) -> Result<timesheet::TimeSheet, &'static str> {
if let Some(init_m) = matches.subcommand_matches("init") {
if let Some(ts) = t_sheet {
println!("Already initialised!");
return Ok(ts);
}
match timesheet::Timesheet::init(init_m.value_of("name")) {
Some(ts) => {
println!("Init successful.");
return Ok(ts);
},
None => return Err("Could not initialize."),
}
}
t_sheet.ok_or("No sheet open! Did you init?")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment