summaryrefslogtreecommitdiffstats
path: root/examples/main.rs
blob: 53a328dc1b770d8e76b42ac23a4258f6bfce9018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
extern crate kairos;

use kairos::timetype::TimeType as TT;

fn pretty_print(tt: TT) {
    match tt {
        TT::Seconds(e) => println!("{} Seconds", e),
        TT::Minutes(e) => println!("{} Minutes", e),
        TT::Hours(e) => println!("{} Hours", e),
        TT::Days(e) => println!("{} Days", e),
        TT::Months(e) => println!("{} Months", e),
        TT::Years(e) => println!("{} Years", e),
        TT::Moment(ndt) => println!("{} ", ndt),
        other => println!("Cannot pretty-print: '{:?}'", other),
    }
}

fn main() {
    // not sure whether this is actually fast or something, but we don't care here, do we?
    let s = std::env::args()
        .skip(1)
        .fold(String::new(), |acc, obj| format!("{} {}", acc, obj));
    let s = s.trim(); // because kairos is not yet whitespace tolerant

    match kairos::parser::parse(s) {
        Err(e) => println!("Error -> {:?}", e),
        Ok(kairos::parser::Parsed::TimeType(tt)) => match tt.calculate() {
            Ok(r) => pretty_print(r),
            Err(e) => println!("Error calculating: {:?}", e),
        },
        Ok(kairos::parser::Parsed::Iterator(Ok(ui))) => {
            for elem in ui {
                match elem {
                    Ok(r) => pretty_print(r),
                    Err(e) => {
                        println!("Error calculating: {:?}", e);
                        std::process::exit(1)
                    },
                }
            }
        },
        Ok(kairos::parser::Parsed::Iterator(Err(e))) => {
            println!("Failed building iterator: {:?}", e);
        },
    }
}