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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use util;
use storage::{self, Storable};
use super::Project;
use super::spec::*;
custom_derive! {
#[derive(Debug,
IterVariants(ComputedFields), IterVariantNames(ComputedFieldNames),
EnumFromStr
)]
pub enum ComputedField{
Responsible,
OfferNumber,
InvoiceNumber,
InvoiceNumberLong,
Name,
Final,
Age,
OurBad,
TheirBad,
Year,
Employees,
ClientFullName,
Wages,
Deserializes,
SortIndex,
Date,
Invalid,
Format,
Dir
}
}
impl<'a> From<&'a str> for ComputedField {
fn from(s: &'a str) -> ComputedField {
s.parse::<ComputedField>().unwrap_or(ComputedField::Invalid)
}
}
impl ComputedField {
pub fn get(&self, project: &Project) -> Option<String> {
let storage = storage::get_storage_path();
match *self {
ComputedField::Responsible => project.responsible().map(|s| s.to_owned()),
ComputedField::OfferNumber => project.offer().number(),
ComputedField::InvoiceNumber => project.invoice().number_str(),
ComputedField::InvoiceNumberLong => project.invoice().number_long_str(),
ComputedField::Name => Some(project.name().map(ToString::to_string).unwrap_or_else(|| project.file_name())),
ComputedField::Final => project.sum_sold().map(|c| util::currency_to_string(&c)).ok(),
ComputedField::Age => project.age().map(|a| lformat!("{} days", a)),
ComputedField::OurBad => project.our_bad() .map(|a| lformat!("{} weeks", a.num_weeks().abs())),
ComputedField::TheirBad => project.their_bad().map(|a| lformat!("{} weeks", a.num_weeks().abs())),
ComputedField::Year => project.year().map(|i|i.to_string()),
ComputedField::Date => project.modified_date().map(|d| d.format("%Y.%m.%d").to_string()),
ComputedField::SortIndex => project.index(),
ComputedField::Employees => project.hours().employees_string(),
ComputedField::ClientFullName => project.client().full_name(),
ComputedField::Deserializes => Some(format!("{:?}", project.parse_yaml().is_ok())),
ComputedField::Wages => project.hours().gross_wages().map(|c| util::currency_to_string(&c)),
ComputedField::Invalid => None,
ComputedField::Format => project.format().map(|f|f.to_string()),
ComputedField::Dir => project.dir()
.parent()
.and_then(|d| d.strip_prefix(&storage).ok())
.map(|d| d.display().to_string())
}
}
}