forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

scheme.rs31 lines, 860 bytesHistoryRawDownload
1use topcoat::{
2 Result,
3 context::Cx,
4 cookie::{Cookies, cookies},
5 router::{
6 content::Form,
7 error::{SeeOther, bad_request, see_other},
8 route,
9 },
10};
11
12use super::lang::safe_back;
13use crate::scheme::{Scheme, scheme_cookie};
14
15#[derive(serde::Deserialize)]
16struct SchemeForm {
17 scheme: String,
18 /// Where to return to. Only a relative path on this site is accepted.
19 #[serde(default)]
20 back: String,
21}
22
23/// POST /scheme: stores the chosen color scheme in a cookie and returns to the page it
24/// was chosen on.
25#[route(POST)]
26async fn set_scheme(cx: &Cx, Form(form): Form<SchemeForm>) -> Result<SeeOther> {
27 let scheme = Scheme::from_id(form.scheme.trim())
28 .ok_or_else(|| bad_request("unsupported color scheme"))?;
29 cookies(cx).add(scheme_cookie(scheme));
30 Ok(see_other(safe_back(&form.back)))
31}