Compare commits
39 Commits
0f6f4edf88
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
252a03a83f | ||
|
|
087fa739ce | ||
|
|
64e30da785 | ||
|
|
a39187a417 | ||
|
|
caefbdbdd6 | ||
|
|
24621725ca | ||
|
|
181cfc1454 | ||
|
|
d9f84022a0 | ||
|
|
f9f3fa5212 | ||
|
|
5e832956db | ||
|
|
ad22e13ca3 | ||
|
|
12bdf43721 | ||
|
|
a693776a59 | ||
|
|
d97be3123b | ||
|
|
f6da50ff6f | ||
|
|
09e1c57f32 | ||
|
|
fcb144b437 | ||
|
|
e6ceef2ead | ||
|
|
7463ca2069 | ||
|
|
1235b38636 | ||
|
|
cf47c63bd7 | ||
|
|
0f9711aaa1 | ||
|
|
326b79a3b1 | ||
|
|
caa09089b0 | ||
|
|
d580122f76 | ||
|
|
a96c5dc1f8 | ||
|
|
b2c7df2361 | ||
|
|
faa884a645 | ||
|
|
d7a7a404ee | ||
|
|
2fc3fc605b | ||
|
|
b2df61a235 | ||
|
|
38e6e19d7b | ||
|
|
631b4cab32 | ||
|
|
482eba9fd4 | ||
|
|
f82418db47 | ||
|
|
673f511e3d | ||
|
|
3f83364f8f | ||
|
|
0722a6fc3b | ||
|
|
6955b7857c |
409
Cargo.lock
generated
409
Cargo.lock
generated
@@ -2,6 +2,171 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "android-tzdata"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
|
||||
|
||||
[[package]]
|
||||
name = "android_system_properties"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38"
|
||||
dependencies = [
|
||||
"android-tzdata",
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"wasm-bindgen",
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2275f18819641850fa26c89acc84d465c1bf91ce57bc2748b28c420473352f64"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "07cdf1b148b25c1e1f7a42225e30a0d99a615cd4637eae7365548dd4529b95bc"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.4.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation-sys"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.1"
|
||||
@@ -14,6 +179,35 @@ version = "0.14.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone"
|
||||
version = "0.1.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20"
|
||||
dependencies = [
|
||||
"android_system_properties",
|
||||
"core-foundation-sys",
|
||||
"iana-time-zone-haiku",
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
"windows-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone-haiku"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "2.1.0"
|
||||
@@ -30,6 +224,48 @@ version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.150"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.18.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.70"
|
||||
@@ -48,6 +284,35 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.15"
|
||||
@@ -87,6 +352,12 @@ dependencies = [
|
||||
"unsafe-libyaml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.39"
|
||||
@@ -102,6 +373,9 @@ dependencies = [
|
||||
name = "tsheet-cli"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_yaml",
|
||||
]
|
||||
@@ -117,3 +391,138 @@ name = "unsafe-libyaml"
|
||||
version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f28467d3e1d3c6586d8f25fa243f544f5800fec42d97032474e17222c2b75cfa"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-core"
|
||||
version = "0.51.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.48.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||
|
||||
@@ -6,5 +6,8 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4.31"
|
||||
clap = { version = "4.4.8", features = ["derive"] }
|
||||
regex = "1.10.4"
|
||||
serde = { version = "1.0.193", features = ["derive"] }
|
||||
serde_yaml = "0.9.27"
|
||||
|
||||
539
src/main.rs
539
src/main.rs
@@ -1,79 +1,496 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::io::{Read, Write};
|
||||
use chrono::{Local, TimeZone, Timelike};
|
||||
use clap::Parser;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_yaml::from_str;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::ops::{Add, Sub};
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
struct Flags {
|
||||
#[arg(short = 'f', long = "file")]
|
||||
f: String,
|
||||
|
||||
#[arg(short = 'l', long = "log")]
|
||||
log: bool,
|
||||
|
||||
#[arg(short = 's', long = "since")]
|
||||
since: Option<String>,
|
||||
|
||||
#[arg(short = 'a', long = "add")]
|
||||
add: Option<String>,
|
||||
|
||||
#[arg(short = 't', long = "tag")]
|
||||
tag: Option<String>,
|
||||
|
||||
#[arg(short = 'c', long = "clock")]
|
||||
clock: bool,
|
||||
|
||||
#[arg(short = 'd', long = "duration")]
|
||||
duration: Option<String>,
|
||||
|
||||
#[arg(short = 'v', long = "verbose")]
|
||||
verbose: bool,
|
||||
|
||||
#[arg(short = 'p', long = "precision")]
|
||||
precision: Option<u32>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let mut flags = Flags::parse();
|
||||
flags.log = flags.log || flags.since.is_some();
|
||||
|
||||
let duration = parse_duration(&flags.duration).unwrap();
|
||||
|
||||
clock(
|
||||
&flags.f,
|
||||
&(flags.clock || flags.duration.is_some()),
|
||||
&duration,
|
||||
)
|
||||
.unwrap();
|
||||
add(&flags.f, &flags.add, &flags.tag, &0).unwrap();
|
||||
log(
|
||||
&flags.f,
|
||||
&flags.log,
|
||||
&flags.since,
|
||||
&flags.verbose,
|
||||
&flags.precision.unwrap_or(0),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct Storage {
|
||||
adds: Vec<Add>,
|
||||
fn clock(f: &String, clock: &bool, duration: &u64) -> Result<(), String> {
|
||||
match clock {
|
||||
true => add(&f, &Some("".to_string()), &None, duration),
|
||||
false => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct Add {
|
||||
t: i64,
|
||||
add: String,
|
||||
tag: String,
|
||||
fn add(f: &String, x: &Option<String>, tag: &Option<String>, duration: &u64) -> Result<(), String> {
|
||||
match x {
|
||||
Some(x) => {
|
||||
let mut tsheet = load(&f)?;
|
||||
tsheet.add(
|
||||
x.to_string(),
|
||||
tag.clone().unwrap_or("".to_string()),
|
||||
*duration,
|
||||
);
|
||||
save(&f, tsheet)?;
|
||||
}
|
||||
None => {}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn save_storage(path: String, storage: Storage) -> Result<(), String> {
|
||||
match File::create(path.clone()) {
|
||||
Ok(mut writer) => _save_storage(&mut writer, storage),
|
||||
Err(reason) => Err(format!("failed to open {} to save storage: {}", path, reason)),
|
||||
}
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
struct Log {
|
||||
t: String,
|
||||
d: f32,
|
||||
xs: Vec<LogX>,
|
||||
}
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
struct LogX {
|
||||
d: f32,
|
||||
x: String,
|
||||
}
|
||||
|
||||
fn _save_storage(writer: &mut dyn Write, storage: Storage) -> Result<(), String> {
|
||||
let mut w = serde_yaml::Serializer::new(writer);
|
||||
match storage.serialize(&mut w) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(reason) => Err(format!("failed to serialize storage: {}", reason)),
|
||||
}
|
||||
}
|
||||
fn log(
|
||||
f: &String,
|
||||
enabled: &bool,
|
||||
since: &Option<String>,
|
||||
verbose: &bool,
|
||||
precision: &u32,
|
||||
) -> Result<(), String> {
|
||||
if !enabled {
|
||||
return Ok(());
|
||||
}
|
||||
let since = parse_time(since)?;
|
||||
if *verbose {
|
||||
eprintln!(
|
||||
"since = {} ({})",
|
||||
system_time_to_unix_seconds(&since),
|
||||
timestamp(&system_time_to_unix_seconds(&since), &2)
|
||||
);
|
||||
}
|
||||
|
||||
fn load_storage(path: String) -> Result<Storage, String> {
|
||||
match File::open(path.clone()) {
|
||||
Ok(mut reader) => _load_storage(&mut reader),
|
||||
Err(reason) => Err(format!("failed to read storage {}: {}", path, reason)),
|
||||
}
|
||||
}
|
||||
let tsheet = load(&f)?;
|
||||
let tsheet = tsheet.since(since);
|
||||
if *verbose {
|
||||
eprintln!("tsheet = {:?}", &tsheet);
|
||||
}
|
||||
|
||||
fn _load_storage(reader: &mut dyn Read) -> Result<Storage, String> {
|
||||
match serde_yaml::from_reader::<&mut dyn Read, Storage>(reader) {
|
||||
Ok(storage) => Ok(storage),
|
||||
Err(err) => Err(format!("failed to parse storage: {}", err)),
|
||||
}
|
||||
let mut result = vec![];
|
||||
let mut curr = Log {
|
||||
t: "".to_string(),
|
||||
d: 0.0,
|
||||
xs: vec![],
|
||||
};
|
||||
let mut currt = "".to_string();
|
||||
for i in 0..tsheet.xs.len() {
|
||||
let x = &tsheet.xs[i];
|
||||
if *verbose {
|
||||
eprintln!("{} != {}?", &curr.t, x.timestamp(&precision));
|
||||
}
|
||||
if currt != x.timestamp(&0) {
|
||||
if curr.xs.len() > 0 {
|
||||
if *verbose {
|
||||
eprintln!("push {:?}", &curr.xs);
|
||||
}
|
||||
result.push(curr.clone());
|
||||
}
|
||||
curr.xs.truncate(0);
|
||||
curr.t = x.timestamp(&precision);
|
||||
currt = x.timestamp(&0);
|
||||
curr.d = 0.0;
|
||||
}
|
||||
let mut d = 1.0;
|
||||
if x.x.len() == 0 {
|
||||
// clock ins get duration zero
|
||||
d = 0.0;
|
||||
} else if i > 0 && tsheet.xs[i].timestamp(&0) == tsheet.xs[i - 1].timestamp(&0) {
|
||||
// if same day as previous then use elapsed as duration
|
||||
d = ((tsheet.xs[i].t - tsheet.xs[i - 1].t) as f32 / (60.0 * 60.0)) as f32;
|
||||
}
|
||||
if *verbose {
|
||||
eprintln!("d={} x='{}'", &x.x, &d);
|
||||
}
|
||||
match x.x.len() {
|
||||
0 => {}
|
||||
_ => {
|
||||
curr.t = x.timestamp(&precision);
|
||||
currt = x.timestamp(&0);
|
||||
curr.xs.push(LogX {
|
||||
d: d,
|
||||
x: x.x.clone(),
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
if curr.xs.len() > 0 {
|
||||
if *verbose {
|
||||
eprintln!("push {:?} (final)", &curr.xs);
|
||||
}
|
||||
result.push(curr.clone());
|
||||
}
|
||||
let mut total_d = 0.0;
|
||||
for i in result.iter_mut() {
|
||||
i.d = i.xs.iter().map(|x| x.d).sum();
|
||||
total_d += i.d;
|
||||
if *verbose {
|
||||
eprintln!(
|
||||
"{} = {:?}",
|
||||
&i.d,
|
||||
&i.xs.iter().map(|x| x.d).collect::<Vec<_>>()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for log in &result {
|
||||
for x in log.xs.clone() {
|
||||
if x.x.len() > 0 {
|
||||
match precision {
|
||||
0 => println!("{} ({:.0}) {} ({:.1})", log.t, log.d, x.x, x.d),
|
||||
1 => println!("{} ({:.1}) {} ({:.2})", log.t, log.d, x.x, x.d),
|
||||
_ => println!("{} ({:.2}) {} ({:.3})", log.t, log.d, x.x, x.d),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if result.len() > 1 {
|
||||
let hours_per_day = 6;
|
||||
match precision {
|
||||
0 => eprintln!(
|
||||
"({:.0}h of {}h over {} dates)",
|
||||
total_d,
|
||||
hours_per_day * result.len(),
|
||||
result.len()
|
||||
),
|
||||
1 => eprintln!(
|
||||
"({:.1}h of {}h over {} dates)",
|
||||
total_d,
|
||||
hours_per_day * result.len(),
|
||||
result.len()
|
||||
),
|
||||
_ => eprintln!(
|
||||
"({:.2}h of {}h over {} dates)",
|
||||
total_d,
|
||||
hours_per_day * result.len(),
|
||||
result.len()
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
mod test_parse {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_save_load_empty() {
|
||||
let got = _load_storage(&mut "adds: []".as_bytes()).expect("failed to parse 'adds: []' storage");
|
||||
assert_eq!(got, Storage{adds: vec![]});
|
||||
|
||||
let mut w = vec![];
|
||||
_save_storage(&mut w, got).expect("failed saving storage to writer");
|
||||
assert_eq!(String::from_utf8(w).unwrap(), "adds: []\n".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_testdata_standalone_yaml() {
|
||||
let want = Storage{adds: vec![
|
||||
Add{t: 1, add: "def".to_string(), tag: "abc".to_string()},
|
||||
Add{t: 2, add: "ghi".to_string(), tag: "".to_string()},
|
||||
]};
|
||||
assert_eq!(
|
||||
load_storage("./src/testdata/standalone.yaml".to_string()).expect("cant load standalone.yaml"),
|
||||
want,
|
||||
);
|
||||
|
||||
let mut w = vec![];
|
||||
_save_storage(&mut w, want).expect("failed saving storage to writer");
|
||||
assert_eq!(String::from_utf8(w).unwrap(), "adds:\n- t: 1\n add: def\n tag: abc\n- t: 2\n add: ghi\n tag: ''\n".to_string());
|
||||
}
|
||||
#[test]
|
||||
fn test_duration() {
|
||||
assert_eq!(0, parse_duration(&None).unwrap());
|
||||
assert_eq!(1, parse_duration(&Some("1s".to_string())).unwrap());
|
||||
assert_eq!(33, parse_duration(&Some("33s".to_string())).unwrap());
|
||||
assert_eq!(666, parse_duration(&Some("666s".to_string())).unwrap());
|
||||
assert_eq!(60, parse_duration(&Some("1m".to_string())).unwrap());
|
||||
assert_eq!(62, parse_duration(&Some("1m2s".to_string())).unwrap());
|
||||
assert_eq!(3600, parse_duration(&Some("1h".to_string())).unwrap());
|
||||
assert_eq!(3723, parse_duration(&Some("1h2m3s".to_string())).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_duration(d: &Option<String>) -> Result<u64, String> {
|
||||
match d {
|
||||
Some(d) => {
|
||||
let mut sum: u64 = 0;
|
||||
let re = Regex::new(r"^(?<hours>[0-9]+h)?(?<minutes>[0-9]+m)?(?<seconds>[0-9]+s)?$")
|
||||
.unwrap();
|
||||
match re.captures(d) {
|
||||
Some(captures) => {
|
||||
match captures.name("seconds") {
|
||||
Some(n) => {
|
||||
let n = n.as_str().to_string();
|
||||
let n = n.trim_end_matches('s');
|
||||
sum += from_str::<u64>(n).unwrap();
|
||||
}
|
||||
None => {}
|
||||
};
|
||||
match captures.name("minutes") {
|
||||
Some(n) => {
|
||||
let n = n.as_str().to_string();
|
||||
let n = n.trim_end_matches('m');
|
||||
sum += 60 * from_str::<u64>(n).unwrap();
|
||||
}
|
||||
None => {}
|
||||
};
|
||||
match captures.name("hours") {
|
||||
Some(n) => {
|
||||
let n = n.as_str().to_string();
|
||||
let n = n.trim_end_matches('h');
|
||||
sum += 60 * 60 * from_str::<u64>(n).unwrap();
|
||||
}
|
||||
None => {}
|
||||
};
|
||||
Ok(sum)
|
||||
}
|
||||
None => Ok(0),
|
||||
}
|
||||
}
|
||||
None => Ok(0),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_time(since: &Option<String>) -> Result<SystemTime, String> {
|
||||
match since {
|
||||
Some(since) => match chrono::NaiveDate::parse_from_str(since, "%Y-%m-%d") {
|
||||
Ok(nd) => {
|
||||
let ndt = nd.and_hms_opt(1, 1, 1).unwrap();
|
||||
let dt = Local.from_local_datetime(&ndt).unwrap();
|
||||
Ok(UNIX_EPOCH.add(Duration::from_secs(dt.timestamp() as u64)))
|
||||
}
|
||||
Err(msg) => Err(format!("failed to parse {}: {}", since, msg)),
|
||||
},
|
||||
None => {
|
||||
Ok(SystemTime::now().sub(Duration::from_secs(Local::now().hour() as u64 * 60 * 60)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct TSheet {
|
||||
xs: Vec<X>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Ord, Eq, PartialOrd)]
|
||||
struct X {
|
||||
t: i64,
|
||||
x: String,
|
||||
tag: 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
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn _save(writer: &mut dyn Write, tsheet: TSheet) -> Result<(), String> {
|
||||
let mut w = serde_yaml::Serializer::new(writer);
|
||||
match tsheet.serialize(&mut w) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(reason) => Err(format!("failed to serialize tsheet: {}", reason)),
|
||||
}
|
||||
}
|
||||
|
||||
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)),
|
||||
}
|
||||
}
|
||||
|
||||
fn _load(reader: &mut dyn Read) -> Result<TSheet, String> {
|
||||
match serde_yaml::from_reader::<&mut dyn Read, TSheet>(reader) {
|
||||
Ok(tsheet) => Ok(tsheet),
|
||||
Err(err) => Err(format!("failed to parse tsheet: {}", err)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_save_load {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_empty() {
|
||||
let got = _load(&mut "xs: []".as_bytes()).expect("failed to parse 'xs: []' tsheet");
|
||||
assert_eq!(got, TSheet { xs: vec![] });
|
||||
|
||||
let mut w = vec![];
|
||||
_save(&mut w, got).expect("failed saving tsheet to writer");
|
||||
assert_eq!(String::from_utf8(w).unwrap(), "xs: []\n".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_testdata_standalone_yaml() {
|
||||
let want = TSheet {
|
||||
xs: vec![
|
||||
X {
|
||||
t: 1,
|
||||
x: "def".to_string(),
|
||||
tag: "abc".to_string(),
|
||||
},
|
||||
X {
|
||||
t: 2,
|
||||
x: "ghi".to_string(),
|
||||
tag: "".to_string(),
|
||||
},
|
||||
],
|
||||
};
|
||||
assert_eq!(
|
||||
load(&"./src/testdata/standalone.yaml".to_string()).expect("cant load standalone.yaml"),
|
||||
want,
|
||||
);
|
||||
|
||||
let mut w = vec![];
|
||||
_save(&mut w, want).expect("failed saving tsheet to writer");
|
||||
assert_eq!(
|
||||
String::from_utf8(w).unwrap(),
|
||||
"xs:\n- t: 1\n x: def\n tag: abc\n- t: 2\n x: ghi\n tag: ''\n".to_string()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl TSheet {
|
||||
fn since(&self, t: SystemTime) -> TSheet {
|
||||
let mut result = TSheet { xs: vec![] };
|
||||
self.xs
|
||||
.iter()
|
||||
.filter(|x| x.ts() >= t)
|
||||
.for_each(|x| result.xs.push(x.clone()));
|
||||
result
|
||||
}
|
||||
|
||||
fn add(&mut self, x: String, tag: String, duration: u64) {
|
||||
let now = system_time_to_unix_seconds(&SystemTime::now());
|
||||
self.xs.push(new_x(now - duration as i64, x, tag));
|
||||
}
|
||||
}
|
||||
|
||||
impl X {
|
||||
fn ts(&self) -> SystemTime {
|
||||
UNIX_EPOCH.add(Duration::from_secs(self.t.try_into().unwrap()))
|
||||
}
|
||||
|
||||
fn timestamp(&self, precision: &u32) -> String {
|
||||
timestamp(&self.t, &precision)
|
||||
}
|
||||
}
|
||||
|
||||
fn system_time_to_unix_seconds(st: &SystemTime) -> i64 {
|
||||
st.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64
|
||||
}
|
||||
|
||||
fn timestamp(t: &i64, precision: &u32) -> String {
|
||||
let dt = Local.timestamp_opt(*t, 0).unwrap();
|
||||
match precision {
|
||||
0 => dt.format("%Y-%m-%d").to_string(),
|
||||
1 => dt.format("%Y-%m-%dT%H:%M").to_string(),
|
||||
_ => dt.format("%Y-%m-%dT%H:%M:%S").to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn new_x(t: i64, x: String, tag: String) -> X {
|
||||
X {
|
||||
t: t,
|
||||
x: x,
|
||||
tag: tag,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_tsheet {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_add() {
|
||||
let mut given = TSheet {
|
||||
xs: vec![X {
|
||||
t: 1,
|
||||
x: "def".to_string(),
|
||||
tag: "abc".to_string(),
|
||||
}],
|
||||
};
|
||||
given.add("ghi".to_string(), "".to_string(), 0);
|
||||
assert_eq!(given.xs.len(), 2);
|
||||
assert!(given.xs[1].t != 1);
|
||||
assert_eq!(given.xs[1].x, "ghi".to_string());
|
||||
assert_eq!(given.xs[1].tag, "".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_since_date() {
|
||||
let given = TSheet {
|
||||
xs: vec![
|
||||
X {
|
||||
t: 1,
|
||||
x: "def".to_string(),
|
||||
tag: "abc".to_string(),
|
||||
},
|
||||
X {
|
||||
t: 2,
|
||||
x: "ghi".to_string(),
|
||||
tag: "".to_string(),
|
||||
},
|
||||
X {
|
||||
t: 3,
|
||||
x: "jkl".to_string(),
|
||||
tag: "".to_string(),
|
||||
},
|
||||
],
|
||||
};
|
||||
let want = TSheet {
|
||||
xs: vec![
|
||||
X {
|
||||
t: 2,
|
||||
x: "ghi".to_string(),
|
||||
tag: "".to_string(),
|
||||
},
|
||||
X {
|
||||
t: 3,
|
||||
x: "jkl".to_string(),
|
||||
tag: "".to_string(),
|
||||
},
|
||||
],
|
||||
};
|
||||
let got = given.since(UNIX_EPOCH.add(Duration::from_secs(2)));
|
||||
assert_eq!(got, want);
|
||||
}
|
||||
}
|
||||
|
||||
8
src/testdata/standalone.txt
vendored
8
src/testdata/standalone.txt
vendored
@@ -1,9 +1,9 @@
|
||||
- t: 1969-12-31
|
||||
d:
|
||||
adds:
|
||||
xs:
|
||||
- d: 1
|
||||
add: abc
|
||||
adds:
|
||||
x: abc
|
||||
xs:
|
||||
- def
|
||||
- d: 0
|
||||
add: ghi
|
||||
x: ghi
|
||||
|
||||
6
src/testdata/standalone.yaml
vendored
6
src/testdata/standalone.yaml
vendored
@@ -1,7 +1,7 @@
|
||||
adds:
|
||||
xs:
|
||||
- t: 1
|
||||
add: def
|
||||
x: def
|
||||
tag: abc
|
||||
- t: 2
|
||||
add: ghi
|
||||
x: ghi
|
||||
tag: ""
|
||||
|
||||
Reference in New Issue
Block a user