its manual testing but it is working

main
Bel LaPointe 2023-11-27 09:30:05 -07:00
parent 482eba9fd4
commit 631b4cab32
1 changed files with 37 additions and 10 deletions

View File

@ -2,7 +2,7 @@ use serde::{Serialize, Deserialize};
use std::io::{Read, Write};
use std::fs::File;
use std::time::{SystemTime, UNIX_EPOCH, Duration};
use std::ops::Add;
use std::ops::{Add, Sub};
use clap::Parser;
#[derive(Debug, Parser)]
@ -13,6 +13,9 @@ struct Flags {
#[arg(short = 'l', long = "log")]
log: bool,
#[arg(short = 's', long = "since")]
since: Option<String>,
#[arg(short = 'a', long = "add")]
add: Option<String>,
@ -23,18 +26,42 @@ struct Flags {
fn main() {
let flags = Flags::parse();
match flags.add.clone() {
Some(add) => {
let tag = flags.tag.clone().unwrap_or("".to_string());
add(&flags.f, &flags.add, &flags.tag).unwrap();
log(&flags.f, &flags.since).unwrap();
let mut tsheet = load(flags.f.clone()).unwrap();
tsheet.add(add, tag);
save(flags.f.clone(), tsheet).unwrap();
println!("{:?}", flags);
}
fn add(f: &String, x: &Option<String>, tag: &Option<String>) -> Result<(), String> {
match x {
Some(x) => {
let mut tsheet = load(&f)?;
tsheet.add(
x.to_string(),
tag.clone().unwrap_or("".to_string()),
);
save(&f, tsheet)?;
},
None => {},
};
Ok(())
}
println!("{:?}", flags);
fn log(f: &String, since: &Option<String>) -> Result<(), String> {
let since = parse_time(since)?;
let tsheet = load(&f)?;
let tsheet = tsheet.since(since);
println!("{:?}", tsheet);
Err("not impl".to_string())
}
fn parse_time(since: &Option<String>) -> Result<SystemTime, String> {
match since {
Some(since) => {
Err("not impl".to_string())
},
None => Ok(SystemTime::now().sub(Duration::from_secs(60*60*24*7))),
}
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
@ -49,7 +76,7 @@ struct X {
tag: String,
}
fn save(path: String, tsheet: TSheet) -> Result<(), String> {
fn save(path: &String, tsheet: TSheet) -> Result<(), String> {
match File::create(path.clone()) {
Ok(mut writer) => _save(&mut writer, tsheet),
Err(reason) => Err(format!("failed to open {} to save tsheet: {}", path, reason)),
@ -64,7 +91,7 @@ fn _save(writer: &mut dyn Write, tsheet: TSheet) -> Result<(), String> {
}
}
fn load(path: String) -> Result<TSheet, String> {
fn load(path: &String) -> Result<TSheet, String> {
match File::open(path.clone()) {
Ok(mut reader) => _load(&mut reader),
Err(reason) => Err(format!("failed to read tsheet {}: {}", path, reason)),