improve header.rs
This commit is contained in:
parent
41c4833a2b
commit
e403ab3b82
|
|
@ -12,36 +12,32 @@ pub struct Header {
|
|||
|
||||
impl Header {
|
||||
pub fn parse(row: quick_csv::Row, winners: usize) -> Result<Header, Box<dyn std::error::Error>> {
|
||||
let mut header = Header {
|
||||
parties: Vec::new(),
|
||||
candidates: Vec::new(),
|
||||
winners,
|
||||
};
|
||||
let mut parties = Vec::<Party>::new();
|
||||
let mut candidates = Vec::<Candidate>::new();
|
||||
let mut parties_lookup = HashMap::<&str, usize>::new();
|
||||
|
||||
for col in row.columns()?.skip(6) {
|
||||
let [party, name] = col.split(':').next_array().ok_or("Missing ':'")?;
|
||||
|
||||
if party == "UG" { // independents
|
||||
header.candidates.push(Candidate {
|
||||
candidates.push(Candidate {
|
||||
name: name.to_owned(),
|
||||
party_id: None,
|
||||
});
|
||||
}
|
||||
else if let Some(party_id) = parties_lookup.get(party).copied() {
|
||||
header.parties[party_id].member_ids.push(header.candidates.len());
|
||||
header.candidates.push(Candidate {
|
||||
parties[party_id].member_ids.push(candidates.len());
|
||||
candidates.push(Candidate {
|
||||
name: name.to_owned(),
|
||||
party_id: Some(party_id),
|
||||
});
|
||||
}
|
||||
else {
|
||||
parties_lookup.insert(party, header.parties.len());
|
||||
header.parties.push(Party::new(name.to_owned()));
|
||||
parties_lookup.insert(party, parties.len());
|
||||
parties.push(Party::new(name.to_owned()));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(header)
|
||||
return Ok(Header { parties, candidates, winners });
|
||||
}
|
||||
pub fn get_candidate_name(&self, index: usize) -> CandidateName {
|
||||
self.candidates[index].get_name(self)
|
||||
|
|
|
|||
Loading…
Reference in New Issue