fn read_raw_data(&self,electorate:&str) -> anyhow::Result<ElectionData> {
        let prohibit_electronic = false;
        let mut metadata = self.read_raw_metadata(electorate)?;
        let filename = electorate.to_string()+"Total.txt";
        let preferences_text_file = self.find_raw_data_file(&filename)?;
        println!("Parsing {}",&preferences_text_file.to_string_lossy());
        metadata.source[0].files.push(filename);
        let candidate_of_pcode_and_ccode : HashMap<(String,usize),CandidateIndex> = metadata.candidates.iter().enumerate().map(|(i,c)|((metadata.parties[c.party.unwrap().0].column_id.clone(),c.position.unwrap()),CandidateIndex(i))).collect();
        #[derive(Deserialize)]
        struct VoteRecord {
            batch : String,
            pindex : String, // possibly not a unique id, but unique for a batch.
            pref:usize,
            pcode:String,
            ccode:usize,
            // rcand:usize,
        }
        let mut rdr = csv::Reader::from_path(&preferences_text_file)?;
        let mut last_paper : Option<String> = None;
        let mut btl = UniqueBTLBuilder::default();
        let mut prefs : Vec<CandidateIndex> = vec![];
        for result in rdr.deserialize() {
            let record: VoteRecord = result?;
            if prohibit_electronic && record.batch.ends_with("000") {} else {
                if let Some(&candidate) = candidate_of_pcode_and_ccode.get(&(record.pcode,record.ccode)) {
                    let paper = record.batch+"_"+&record.pindex;
                    if last_paper==None || last_paper.as_ref().unwrap()!=&paper {
                        if !prefs.is_empty() { btl.add(prefs.clone()); }
                        prefs.clear();
                        last_paper=Some(paper);
                    }
                    prefs.push(candidate);
                    if prefs.len()!=record.pref { return Err(anyhow!("Preferences not in order. No reason they should be other than they seem to be, but it saves work if we assume they are and this is a safety check"))}
                } else { return Err(anyhow!("Bad candidate"))}
            }
        }
        if !prefs.is_empty() { btl.add(prefs.clone()); }
        Ok(ElectionData{ metadata, atl:vec![], atl_types: vec![], btl:btl.to_btls(), btl_types: vec![], informal:0 })
    }
// This below should be made more general and most of it factored out into a separate function.
This below should be made more general and most of it factored out into a separate function.
This below should be made more general and most of it factored out into a separate function .
fn read_raw_data(&self,electorate:&str) -> anyhow::Result<ElectionData> {
        let prohibit_electronic = false;
        let mut metadata = self.read_raw_metadata(electorate)?;
        let filename = electorate.to_string()+"Total.txt";
        let preferences_text_file = self.find_raw_data_file(&filename)?;
        println!("Parsing {}",&preferences_text_file.to_string_lossy());
        metadata.source[0].files.push(filename);
        let candidate_of_pcode_and_ccode : HashMap<(String,usize),CandidateIndex> = metadata.candidates.iter().enumerate().map(|(i,c)|((metadata.parties[c.party.unwrap().0].column_id.clone(),c.position.unwrap()),CandidateIndex(i))).collect();
        #[derive(Deserialize)]
        struct VoteRecord {
            batch : String,
            pindex : String, // possibly not a unique id, but unique for a batch.
            pref:usize,
            pcode:String,
            ccode:usize,
            // rcand:usize,
        }
        let mut rdr = csv::Reader::from_path(&preferences_text_file)?;
        let mut last_paper : Option<String> = None;
        let mut btl = UniqueBTLBuilder::default();
        let mut prefs : Vec<CandidateIndex> = vec![];
        for result in rdr.deserialize() {
            let record: VoteRecord = result?;
            if prohibit_electronic && record.batch.ends_with("000") {} else {
                if let Some(&candidate) = candidate_of_pcode_and_ccode.get(&(record.pcode,record.ccode)) {
                    let paper = record.batch+"_"+&record.pindex;
                    if last_paper==None || last_paper.as_ref().unwrap()!=&paper {
                        if !prefs.is_empty() { btl.add(prefs.clone()); }
                        prefs.clear();
                        last_paper=Some(paper);
                    }
                    prefs.push(candidate);
                    if prefs.len()!=record.pref { return Err(anyhow!("Preferences not in order. No reason they should be other than they seem to be, but it saves work if we assume they are and this is a safety check"))}
                } else { return Err(anyhow!("Bad candidate"))}
            }
        }
        if !prefs.is_empty() { btl.add(prefs.clone()); }
        Ok(ElectionData{ metadata, atl:vec![], atl_types: vec![], btl:btl.to_btls(), btl_types: vec![], informal:0 })
    }
fn read_raw_data ( & self , electorate : & str ) -> anyhow :: Result < ElectionData > { let prohibit_electronic = false ; let mut metadata = self . read_raw_metadata ( electorate ) ? ; let filename = electorate . to_string ( ) + " " ; let preferences_text_file = self . find_raw_data_file ( & filename ) ? ; println ! ( " " preferences_text_file to_string_lossy ( ) ) ; metadata . source [ 0 ] . files . push ( filename ) ; let candidate_of_pcode_and_ccode : HashMap < ( String , usize ) , CandidateIndex > = metadata . candidates . iter ( ) . enumerate ( ) . map ( | ( i , c ) | ( ( metadata . parties [ c . party . unwrap ( ) . 0 ] . column_id . clone ( ) , c . position . unwrap ( ) ) , CandidateIndex ( i ) ) ) . collect ( ) ; # [ derive ( Deserialize ) ] struct VoteRecord { batch : String , pindex : String , // possibly not a unique id, but unique for a batch. pref : usize , pcode : String , ccode : usize , // rcand:usize, } let mut rdr = csv :: Reader :: from_path ( & preferences_text_file ) ? ; let mut last_paper : Option < String > = None ; let mut btl = UniqueBTLBuilder :: default ( ) ; let mut prefs : Vec < CandidateIndex > = vec ! [ ] ; for result in rdr . deserialize ( ) { let record : VoteRecord = result ? ; if prohibit_electronic && record . batch . ends_with ( " " ) { } else { if let Some ( & candidate ) = candidate_of_pcode_and_ccode . get ( & ( record . pcode , record . ccode ) ) { let paper = record . batch + " " + & record . pindex ; if last_paper == None || last_paper . as_ref ( ) . unwrap ( ) != & paper { if ! prefs . is_empty ( ) { btl . add ( prefs . clone ( ) ) ; } prefs . clear ( ) ; last_paper = Some ( paper ) ; } prefs . push ( candidate ) ; if prefs . len ( ) != record . pref { return Err ( anyhow ! ( " " ) ) } } else { return Err ( anyhow ! ( " " ) ) } } } if ! prefs . is_empty ( ) { btl . add ( prefs . clone ( ) ) ; } Ok ( ElectionData { metadata , atl : vec ! [ ] , atl_types : vec ! [ ] , btl : btl . to_btls ( ) , btl_types : vec ! [ ] , informal : 0 } ) }
This below should be made more general and most of it factored out into a separate function.
This below should be made more general and most of it factored out into a separate function .
&self
electorate
&str
&self
electorate
&str