use rocket::State; use rocket_contrib::Template; use std::collections::HashMap; use super::super::Config; use super::super::NoteStore; #[derive(Serialize)] struct IndexTemplateContext<'a> { app_version: &'a str, notes: Vec>, title: String, } #[get("/")] fn index(notes: State, config: State) -> Template { let notes = notes.read().unwrap(); let mut note_kvs = vec![]; for note in notes.iter() { let mut note_kv = HashMap::new(); note_kv.insert("id", note.id.as_ref()); note_kv.insert("name", note.name.as_ref()); note_kvs.push(note_kv); } let context = IndexTemplateContext { app_version: env!("CARGO_PKG_VERSION"), notes: note_kvs, title: config.title.clone(), }; Template::render("index", &context) } #[cfg(test)] mod tests { use rocket; use rocket::http::{Accept, Status}; use rocket::local::Client; #[test] fn index() { let client = Client::new(rocket(Some("test"))).unwrap(); // Try to get the index and verify the body let mut res = client.get("/").header(Accept::HTML).dispatch(); assert_eq!(res.status(), Status::Ok); let body = res.body_string().unwrap(); println!("body: {}", body); assert!(body.contains("Test")); } }