]> git.scottworley.com Git - voter/commitdiff
Show the candidates
authorScott Worley <scottworley@scottworley.com>
Sat, 19 Nov 2022 06:49:50 +0000 (22:49 -0800)
committerScott Worley <scottworley@scottworley.com>
Sat, 19 Nov 2022 06:49:50 +0000 (22:49 -0800)
src/main.rs

index 9d8daa5f66ce9c0e6960b48b4346e99fc7dc8c42..994ffac9b049ab2c6a8d0795ea067a47e5e9e663 100644 (file)
@@ -75,7 +75,20 @@ fn set_cookie(mut response: cgi::Response, path: &str) -> Result<cgi::Response,
 
 fn prompt_for_vote(dir: PathBuf, request: cgi::Request) -> Result<cgi::Response, cgi::Response> {
     let voter = get_voter(&request);
-    let mut response = cgi::html_response(200, "<html><body>You should vote</body></html>");
+    let cfile = std::fs::File::open(dir.join("candidates"))
+        .map_err(|_| cgi::text_response(503, "No candidates"))?;
+    let mut response = cgi::html_response(
+        200,
+        std::iter::once(Ok("<html><body><table>".to_owned()))
+            .chain(
+                std::io::BufReader::new(cfile)
+                    .lines()
+                    .map(|rc| rc.map(|c| format!("<tr><td>{c}</td></tr>"))),
+            )
+            .chain(std::iter::once(Ok("</table></body></html>".to_owned())))
+            .collect::<std::io::Result<String>>()
+            .map_err(|_| cgi::text_response(503, "Missing candidates"))?,
+    );
     if voter.is_err() {
         response = set_cookie(response, request.uri().path())?
     }