summaryrefslogtreecommitdiffstats
path: root/src/frontend.rs
blob: 1c1075e6e7c5cb3ef270949e1046e7ec8e7638b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use actix_web::Result;
use actix_web::get;
use actix_web::post;
use actix_web::web;

#[get("/")]
pub async fn index() -> Result<maud::Markup> {
    Ok(maud::html! {
        html {
            body {
                h1 { "Hello World!" }

                form action="/make_landscape" method="post" {
                    label { "Elements" }
                    input type="number" name="elements" checked;

                    input type="submit";
                }
            }
        }
    })
}


#[derive(serde::Deserialize)]
pub struct LandscapeSize {
    elements: usize,
}

#[post("/make_landscape")]
pub async fn make_landscape(form: web::Form<LandscapeSize>) -> Result<maud::Markup> {
    if form.elements > 100 {
        return Ok(maud::html! {
            html {
                body {
                    p { "For runtime reasons, this application only supports landscapes up to 100 elements" }
                }
            }
        })
    }

    Ok(maud::html! {
        html {
            body {
                h1 { "Hello World!" }

                p { "Please fill in the landscape values" }
                form action="/calculate" method="post" {
                    @for _n in 0..form.elements {
                        p {
                            input type="number" name="levels[]" checked;
                        }
                    }

                    input type="number" name="hours" checked;
                    label for="hours" { "Hours of Rain" }

                    input type="submit";
                }
            }
        }
    })
}

#[derive(serde::Deserialize)]
pub struct Landscape {
    levels: Vec<usize>,
    hours: usize,
}

#[post("/calculate")]
pub async fn calculate(form: web::Form<Landscape>) -> Result<maud::Markup> {
    Ok(maud::html! {
        html {
            body {
                h1 { "Landscape!" }

                p { "Filling in " (form.hours) " hours" }

                @for value in &form.levels {
                    p { (value) }
                }
            }
        }
    })
}