work_tock_lib/
gob.rs

1use crate::clockin::{ClockAction, LineClockAction};
2use crate::s_time::STime;
3use gobble::*;
4
5parser! {
6    (Date->(usize,usize,Option<isize>))
7    (common::UInt,last(ws__('/'),common::UInt),maybe(last(ws__('/'),common::Int)))
8}
9
10parser! {
11    (StrVal -> String)
12    or(common::Quoted,common::Ident)
13}
14
15parser! {
16    (Comment ->())
17    ('#',Any.except("\n\r,").istar()).ig()
18}
19
20pub fn next_<P: Parser>(p: P) -> impl Parser<Out = P::Out> {
21    sep_star(", \t\n\r".istar(), Comment).ig_then(p)
22}
23
24parser! {
25    (ToEnd->()),
26    (" ,\t\n\r".istar(),eoi).ig()
27}
28
29parser! {
30    (STIME -> STime),
31    (common::Int, ":", common::Int).map(|(a, _, b)| STime::new(a, b))
32}
33
34pub fn line_clock_actions() -> impl Parser<Out = Vec<LineClockAction>> {
35    star_until_ig(
36        next_((line_col, ClockACTION)).map(|((line, col), action)| LineClockAction {
37            line,
38            col,
39            action,
40        }),
41        ToEnd,
42    )
43}
44
45parser! {
46    (Group->ClockAction)
47    (
48        '$',
49        StrVal,
50        ws__('['),
51        star_until_ig(next_(StrVal), next_("]")),
52    )
53        .map(|(_, k, _, v)| ClockAction::DefGroup(k, v))
54}
55
56parser! {
57    (ClockACTION -> ClockAction)
58    or!(
59        //handle tags
60        ('_', StrVal).map(|(_, s)| ClockAction::AddTag(s)),
61        ("__", maybe(StrVal)).map(|(_, os)| ClockAction::ClearTags(os)),
62        //handle time
63        ('-', STIME).map(|(_, t)| ClockAction::Out(t)),
64        Date.map(|(d, m, yop)| ClockAction::SetDate(d, m, yop)),
65        (STIME, maybe(('-', STIME))).map(|(i, op)| match op {
66            Some((_, out)) => ClockAction::InOut(i, out),
67            None => ClockAction::In(i),
68        }),
69        ('=', StrVal, ws__(':'), common::Int).map(|(_, k, _, v)| ClockAction::SetNum(k, v)),
70        Group,
71        (StrVal, maybe((ws__('='), common::Int))).map(|(k, set)| match set {
72            Some((_, v)) => ClockAction::SetNum(k, v),
73            None => ClockAction::SetJob(k),
74        }),
75    )
76}
77
78#[cfg(test)]
79pub mod test {
80    use super::*;
81    #[test]
82    pub fn str_val_parses_dashes() {
83        assert_eq!(StrVal.parse_s("hello "), Ok("hello".to_string()));
84        assert_eq!(StrVal.parse_s("hel_p_me@52"), Ok("hel_p_me".to_string()));
85        assert_eq!(
86            StrVal.parse_s(r#""hello\tworld"poo "#),
87            Ok("hello\tworld".to_string())
88        );
89        assert!(StrVal.parse_s("_hello").is_err());
90    }
91}