+// voter: A simple CGI vote recorder, approval-voting-style
+//
+// This program is free software: you can redistribute it and/or modify it
+// under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, version 3.
+
use rand::prelude::*;
use std::collections::{HashMap, HashSet};
use std::io::prelude::*;
fn tally_votes(dir: PathBuf) -> std::io::Result<HashMap<String, HashSet<String>>> {
let mut tally: HashMap<String, HashSet<String>> = HashMap::new();
- let vfile = std::fs::File::open(dir.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);
+ if let Ok(vfile) = std::fs::File::open(dir.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);
+ }
+ } 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());
}
}
}
COOKIE_NAME,
b"=",
&make_random_id(),
- b"; Secure HttpOnly SameSite=Strict Max-Age=30000000 Path=",
+ b"; Secure; HttpOnly; SameSite=Strict; Max-Age=30000000; Path=",
path.as_bytes(),
]
.concat(),