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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use crate::{types::advisor_review::ReviewRanking, utils::serde::deserialize_truthy_falsy};
use serde::{Deserialize, Serialize};

/// (Proposal Id, Assessor Id), an assessor cannot assess the same proposal more than once
pub type AdvisorReviewId = (String, String);
pub type VeteranAdvisorId = String;

#[derive(Deserialize)]
pub struct AdvisorReviewRow {
    pub proposal_id: String,
    #[serde(alias = "Idea URL")]
    pub idea_url: String,
    #[serde(alias = "Assessor")]
    pub assessor: String,
    #[serde(alias = "Impact / Alignment Note")]
    pub impact_alignment_note: String,
    #[serde(alias = "Impact / Alignment Rating")]
    pub impact_alignment_rating: u8,
    #[serde(alias = "Feasibility Note")]
    pub feasibility_note: String,
    #[serde(alias = "Feasibility Rating")]
    pub feasibility_rating: u8,
    #[serde(alias = "Auditability Note")]
    pub auditability_note: String,
    #[serde(alias = "Auditability Rating")]
    pub auditability_rating: u8,
    #[serde(alias = "Excellent", deserialize_with = "deserialize_truthy_falsy")]
    excellent: bool,
    #[serde(alias = "Good", deserialize_with = "deserialize_truthy_falsy")]
    good: bool,
    #[serde(
        default,
        alias = "Filtered Out",
        deserialize_with = "deserialize_truthy_falsy"
    )]
    filtered_out: bool,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct VeteranRankingRow {
    pub proposal_id: String,
    #[serde(alias = "Assessor")]
    pub assessor: String,
    #[serde(alias = "Excellent", deserialize_with = "deserialize_truthy_falsy")]
    excellent: bool,
    #[serde(alias = "Good", deserialize_with = "deserialize_truthy_falsy")]
    good: bool,
    #[serde(
        default,
        alias = "Filtered Out",
        deserialize_with = "deserialize_truthy_falsy"
    )]
    filtered_out: bool,
    pub vca: VeteranAdvisorId,
}

impl VeteranRankingRow {
    pub fn new(
        proposal_id: String,
        assessor: String,
        vca: VeteranAdvisorId,
        ranking: ReviewRanking,
    ) -> Self {
        let excellent = ranking == ReviewRanking::Excellent;
        let good = ranking == ReviewRanking::Good;
        let filtered_out = ranking == ReviewRanking::FilteredOut;

        Self {
            proposal_id,
            assessor,
            vca,
            excellent,
            good,
            filtered_out,
        }
    }

    pub fn score(&self) -> ReviewRanking {
        ranking_mux(self.excellent, self.good, self.filtered_out)
    }

    pub fn review_id(&self) -> AdvisorReviewId {
        (self.proposal_id.clone(), self.assessor.clone())
    }
}

impl AdvisorReviewRow {
    pub fn score(&self) -> ReviewRanking {
        ranking_mux(self.excellent, self.good, self.filtered_out)
    }
}

fn ranking_mux(excellent: bool, good: bool, filtered_out: bool) -> ReviewRanking {
    match (excellent, good, filtered_out) {
        (true, false, false) => ReviewRanking::Excellent,
        (false, true, false) => ReviewRanking::Good,
        (false, false, true) => ReviewRanking::FilteredOut,
        (false, false, false) => ReviewRanking::NotReviewedByVCA,
        _ => {
            // This should never happen, from the source of information a review could be either
            // Excellent or Good or not assessed. It cannot be both and it is considered
            // a malformed information input.
            panic!(
                "Invalid combination of scores {} {} {}",
                excellent, good, filtered_out
            )
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{ReviewRanking, VeteranRankingRow};
    use crate::community_advisors::models::AdvisorReviewRow;
    use crate::utils::csv as csv_utils;
    use rand::{distributions::Alphanumeric, Rng};
    use std::path::PathBuf;

    #[test]
    fn test_deserialize() {
        let file_path = PathBuf::from("resources/testing/valid_assessments.csv");
        let data: Vec<AdvisorReviewRow> =
            csv_utils::load_data_from_csv::<_, b','>(&file_path).unwrap();
        assert_eq!(data.len(), 1);
    }

    fn ranking_demux(ranking: ReviewRanking) -> (bool, bool, bool) {
        match ranking {
            ReviewRanking::Good => (false, true, false),
            ReviewRanking::Excellent => (true, false, false),
            ReviewRanking::FilteredOut => (false, false, true),
            ReviewRanking::NotReviewedByVCA => (false, false, false),
        }
    }

    impl VeteranRankingRow {
        pub fn dummy(score: ReviewRanking, assessor: String, vca: String) -> Self {
            let (excellent, good, filtered_out) = ranking_demux(score);
            Self {
                proposal_id: String::new(),
                assessor,
                excellent,
                good,
                filtered_out,
                vca,
            }
        }
    }

    impl AdvisorReviewRow {
        pub fn dummy(score: ReviewRanking) -> Self {
            let (excellent, good, filtered_out) = ranking_demux(score);
            AdvisorReviewRow {
                proposal_id: String::new(),
                idea_url: String::new(),
                assessor: (0..10)
                    .map(|_| rand::thread_rng().sample(Alphanumeric) as char)
                    .collect(),
                impact_alignment_note: String::new(),
                impact_alignment_rating: 0,
                feasibility_note: String::new(),
                feasibility_rating: 0,
                auditability_note: String::new(),
                auditability_rating: 0,
                excellent,
                good,
                filtered_out,
            }
        }
    }
}