X-Git-Url: http://git.scottworley.com/voter/blobdiff_plain/971d76c762faaa77ac019efaba5f68478bd82e30..5ee4072bf723ad5b091bcf8e5869382fed9f00ba:/src/main.rs?ds=inline diff --git a/src/main.rs b/src/main.rs index fcb16c7..92937e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,32 +53,44 @@ fn get_voter(request: &cgi::Request) -> Result<&[u8], cgi::Response> { fn tally_votes(dir: &Path) -> std::io::Result>> { let mut tally: HashMap> = HashMap::new(); - if let Ok(vfile) = std::fs::File::open(dir.to_owned().join("votes")) { - for liner in std::io::BufReader::new(vfile).lines() { - let line = liner?; - if let Some((voter, datum)) = line.split_once(' ') { - if voter.len() == COOKIE_LENGTH { - if let Some((vote, candidate)) = datum.split_once(' ') { - if vote == "0" { - if let Some(entry) = tally.get_mut(candidate) { - entry.remove(voter); + match std::fs::File::open(dir.to_owned().join("votes")) { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(tally), + Err(e) => Err(e), + Ok(vfile) => { + for liner in std::io::BufReader::new(vfile).lines() { + let line = liner?; + if let Some((voter, datum)) = line.split_once(' ') { + if voter.len() == COOKIE_LENGTH { + if let Some((vote, candidate)) = datum.split_once(' ') { + if vote == "0" { + if let Some(entry) = tally.get_mut(candidate) { + entry.remove(voter); + } + } else if vote == "1" { + tally + .entry(candidate.to_owned()) + .or_default() + .insert(voter.to_owned()); } - } else if vote == "1" { - tally - .entry(candidate.to_owned()) - .or_default() - .insert(voter.to_owned()); } } } } + Ok(tally) } } - Ok(tally) +} + +fn read_elim_list(dir: &Path) -> std::io::Result> { + match std::fs::File::open(dir.join("eliminated")) { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(HashSet::new()), + Err(e) => Err(e), + Ok(elimfile) => std::io::BufReader::new(elimfile).lines().collect(), + } } fn valid_id_char(c: u8) -> bool { - (b'A'..=b'Z').contains(&c) || (b'a'..=b'z').contains(&c) || (b'0'..=b'9').contains(&c) + c.is_ascii_alphanumeric() } fn make_random_id() -> [u8; COOKIE_LENGTH] { @@ -117,6 +129,7 @@ const HTML_HEADER: &str = "