summaryrefslogtreecommitdiff
path: root/src/record.rs
blob: 841855e4fea37eec9b41d4c1b5c7ae6a5690c7d2 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#[derive(Debug)]
pub struct Record {
    account:        String,
    currency:       String,
    record_type:    String,
    main_type:      String,
    subcategory:    String,
    price:          f64,
    fee:            f64,
    bonus:          f64,
    name:           String,
    store:          Option<String>,
    date:           Date,
    time:           Option<String>,
    project:        Option<String>,
    note:           Option<String>,
    tags:           Option<String>,
    target:         Option<String>,
}

#[derive(Debug)]
struct Date {
    month: i32,
    day: i32,
    year: i32,
}

impl Record {
    pub fn new(r: csv::StringRecord) -> Self {
        Record {
            account: r[0].to_string(),
            currency: r[1].to_string(),
            record_type: r[2].to_string(),
            main_type: r[3].to_string(),
            subcategory: r[4].to_string(),
            price: r[5].parse().unwrap(),
            fee: r[6].parse().unwrap(),
            bonus: r[7].parse().unwrap(),
            name: r[8].to_string(),
            store: r.get(9).map(|s| s.to_string()),
            date: {
                let d = r[10].split('/').collect::<Vec<&str>>();
                Date {
                    month:  d[0].parse().unwrap(),
                    day:    d[1].parse().unwrap(),
                    year:   d[2].parse().unwrap(),
                }
            },
            time: r.get(11).map(|s| s.to_string()),
            project: r.get(12).map(|s| s.to_string()),
            note: r.get(13).map(|s| s.to_string()),
            tags: r.get(14).map(|s| s.to_string()),
            target: r.get(15).map(|s| s.to_string()),
        }
    }

    pub fn cal(&self) -> f64 {
        self.price + self.fee + self.bonus
    }

    pub fn record_type(&self) -> &str {
        self.record_type.as_str()
    }

    pub fn project(&self) -> Option<String> {
        self.project.clone()
    }

    pub fn currency(&self) -> String {
        self.currency.clone()
    }
}