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::*;

/// Fields that are accessible but are not directly found in the file format.
/// This is used to get fields that are computed through an ordinary `field("responsible")`
custom_derive! {
    #[derive(Debug,
             IterVariants(ComputedFields), IterVariantNames(ComputedFieldNames),
             EnumFromStr
             )]
    /// `Project::field()` allows accessing fields within the raw `yaml` data structure.
    /// Computed fields are fields that are not present in the document but computed.
    ///
    /// `ComputedFields` is an automatically generated type that allows iterating of the variants of
    /// this Enum.
    pub enum ComputedField{
        /// Usually `storage`, or in legacy part of `signature`
        Responsible,
        OfferNumber,
        /// Pretty version of `invoice/number`: "`R042`"
        InvoiceNumber,
        /// Pretty version of `invoice/number` including year: "`R2016-042`"
        InvoiceNumberLong,
        ///Overall Cost Project, including taxes
        Name,
        Final,
        Age,
        OurBad,
        TheirBad,
        Year,
        Employees,
        ClientFullName,
        Wages,
        Deserializes,

        /// Sorting index
        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())), // TODO remove name() from `Storable`, storables only need a slug()
            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())

            // _ => None
        }
    }
}