Yay templates

This commit is contained in:
Gender Shrapnel 2020-05-31 04:48:02 +02:00
parent df77f814f5
commit a888ec9a8a
5 changed files with 828 additions and 55 deletions

841
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,4 +10,10 @@ license = "AGPL-3.0-or-later"
[dependencies]
legion = "0.2.1"
rand = "0.7.3"
rocket = "0.4.4"
rocket = "0.4.4"
serde = { version = "1.0.111", features = ["derive"] }
[dependencies.rocket_contrib]
version = "0.4.5"
default-features = false
features = ["tera_templates"]

View File

@ -2,19 +2,21 @@
#[macro_use]
extern crate rocket;
extern crate serde;
mod simulation;
use std::fmt::Write as FmtWrite;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use rocket::State;
use rocket_contrib::templates::Template;
use serde::Serialize;
use simulation::{Object, Simulation};
#[derive(Clone, Serialize)]
struct SimState {
pub iteration: u64,
pub objects: Vec<Object>
@ -23,18 +25,11 @@ struct SimState {
type WrappedState = Arc<Mutex<SimState>>;
#[get("/")]
fn index(sim: State<WrappedState>) -> String {
fn index(sim: State<WrappedState>) -> Template {
let state = sim.lock().unwrap();
let mut result = String::from("Hello, world!\n");
write!(&mut result, "Iteration {}\n", (*state).iteration).unwrap();
for object in &(*state).objects {
write!(&mut result, "Object {} at {}, {}\n", object.name, object.x, object.y).unwrap();
}
result
Template::render("stuff", state.clone())
}
fn simulation(s: WrappedState) {
@ -61,6 +56,7 @@ fn main() {
simulation(Arc::clone(&state));
rocket::ignite()
.attach(Template::fairing())
.manage(Arc::clone(&state))
.mount("/", routes![index])
.launch();

View File

@ -1,5 +1,6 @@
use legion::prelude::*;
use rand::prelude::*;
use serde::Serialize;
#[derive(Clone, Debug, PartialEq)]
struct Name {
@ -22,6 +23,7 @@ pub struct Simulation {
world: World
}
#[derive(Clone, Serialize)]
pub struct Object {
pub name: String,
pub x: f64,

14
templates/stuff.tera Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Blab</title>
</head>
<body>
<h1>Hello, world!</h1>
<ul>
{% for object in objects %}
<li>{{ object.name }} @ {{ object.x }}, {{ object.y }}</li>
{% endfor %}
</ul>
</body>
</html>