let cookie = request
.headers()
.get(cgi::http::header::COOKIE)
- .map(|c| c.as_bytes())
+ .map(cgi::http::HeaderValue::as_bytes)
.and_then(|c| c.strip_prefix(COOKIE_NAME))
.and_then(|c| c.strip_prefix(b"="))
.ok_or_else(|| cgi::text_response(400, "Invalid cookie"))?;
fn make_random_id() -> [u8; COOKIE_LENGTH] {
let mut id = [0; COOKIE_LENGTH];
- for c in id.iter_mut() {
- while !valid_id_char(c)
- {
+ for c in &mut id {
+ while !valid_id_char(c) {
*c = random()
}
}
fn supports(tally: &HashMap<String, HashSet<String>>, me: &str, candidate: &str) -> bool {
tally
.get(candidate)
- .map(|supporters| supporters.contains(me))
- .unwrap_or(false)
+ .map_or(false, |supporters| supporters.contains(me))
}
fn prompt_for_vote(dir: PathBuf, request: cgi::Request) -> Result<cgi::Response, cgi::Response> {
std::iter::once(Ok(HTML_HEADER.to_owned()))
.chain(std::io::BufReader::new(cfile).lines().map(|rc| {
rc.map(|c| {
- let count = tally
- .get(&c)
- .map(|supporters| supporters.len())
- .unwrap_or(0);
- let checked = if me.map(|me| supports(&tally, me, &c)).unwrap_or(false) {
+ let count = tally.get(&c).map_or(0, std::collections::HashSet::len);
+ let checked = if me.map_or(false, |me| supports(&tally, me, &c)) {
"checked"
} else {
""