diff --git a/Cargo.toml b/Cargo.toml index 7a38be0..ed0c793 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ a family or group of friends. """ readme = "README.md" license = "MIT" +edition = "2018" [dependencies] comrak = "0.7.0" diff --git a/src/handlers/home.rs b/src/handlers/home.rs index 778c930..5a26f2a 100644 --- a/src/handlers/home.rs +++ b/src/handlers/home.rs @@ -1,7 +1,8 @@ use super::super::Config; use super::super::NoteStore; -use rocket::State; +use rocket::{get, State}; use rocket_contrib::templates::Template; +use serde_derive::Serialize; use std::collections::HashMap; #[derive(Serialize)] @@ -12,7 +13,7 @@ struct IndexTemplateContext<'a> { } #[get("/")] -pub fn index(notes: State, config: State) -> Template { +pub(crate) fn index(notes: State, config: State) -> Template { let notes = notes.read().unwrap(); let mut note_kvs = vec![]; for note in notes.iter() { @@ -37,7 +38,7 @@ mod tests { #[test] fn index() { - let client = Client::new(rocket(Some("test"))).unwrap(); + let client = Client::new(crate::rocket(Some("test"))).unwrap(); // Try to get the index and verify the body let mut res = client.get("/").header(Accept::HTML).dispatch(); diff --git a/src/handlers/note.rs b/src/handlers/note.rs index 6f94935..044df2d 100644 --- a/src/handlers/note.rs +++ b/src/handlers/note.rs @@ -1,30 +1,30 @@ use super::super::models::note::Note; use super::super::NoteStore; -use rocket::State; +use rocket::{get, put, State}; use rocket_contrib::json::Json; #[get("/", format = "application/json")] -pub fn index(notes: State) -> Option>> { +pub(crate) fn index(notes: State) -> Option>> { let notes = notes.read().unwrap(); Some(Json(notes.clone())) } #[get("/", format = "text/html")] -pub fn show_html(note_id: String, notes: State) -> Option { +pub(crate) fn show_html(note_id: String, notes: State) -> Option { let notes = notes.read().unwrap(); let note = notes.iter().find(|note| note.id == note_id)?; Some(note.to_html()) } #[get("/", format = "application/json", rank = 2)] -pub fn show_json(note_id: String, notes: State) -> Option> { +pub(crate) fn show_json(note_id: String, notes: State) -> Option> { let notes = notes.read().unwrap(); let note = notes.iter().find(|note| note.id == note_id)?; Some(Json(note.clone())) } #[put("/", format = "application/json", data = "")] -pub fn update( +pub(crate) fn update( note_id: String, new_note: Json, notes: State, @@ -41,11 +41,11 @@ mod tests { use rocket; use rocket::http::{Accept, ContentType, Status}; use rocket::local::Client; - use serde_json; + use serde_json::{self, json}; #[test] fn index() { - let client = Client::new(rocket(Some("test"))).unwrap(); + let client = Client::new(crate::rocket(Some("test"))).unwrap(); // Try to get all the notes let mut res = client.get("/notes").header(Accept::JSON).dispatch(); @@ -70,7 +70,7 @@ mod tests { #[test] fn show_html() { - let client = Client::new(rocket(Some("test"))).unwrap(); + let client = Client::new(crate::rocket(Some("test"))).unwrap(); // Try to get the note and verify the body let mut res = client.get("/notes/test").header(Accept::HTML).dispatch(); @@ -91,7 +91,7 @@ mod tests { #[test] fn show_json() { - let client = Client::new(rocket(Some("test"))).unwrap(); + let client = Client::new(crate::rocket(Some("test"))).unwrap(); // Try to get the note and verify the body let mut res = client.get("/notes/test").header(Accept::JSON).dispatch(); @@ -118,7 +118,7 @@ mod tests { #[test] fn update() { - let client = Client::new(rocket(Some("test"))).unwrap(); + let client = Client::new(crate::rocket(Some("test"))).unwrap(); // Try to get the note and determine what to change it to let mut res = client @@ -180,7 +180,7 @@ mod tests { .header(ContentType::JSON) .body(r#"{}"#) .dispatch(); - assert_eq!(res.status(), Status::BadRequest); + assert_eq!(res.status().code, 422); // Try to change a note without a proper type (i.e. not JSON) let res = client diff --git a/src/handlers/static_files.rs b/src/handlers/static_files.rs index 65e840d..085ca9d 100644 --- a/src/handlers/static_files.rs +++ b/src/handlers/static_files.rs @@ -1,8 +1,9 @@ +use rocket::get; use rocket::response::NamedFile; use std::path::{Path, PathBuf}; #[get("/", rank = 5)] -pub fn all(path: PathBuf) -> Option { +pub(crate) fn all(path: PathBuf) -> Option { NamedFile::open( Path::new(env!("CARGO_MANIFEST_DIR")) .join("static") @@ -13,13 +14,12 @@ pub fn all(path: PathBuf) -> Option { #[cfg(test)] mod tests { - use rocket; use rocket::http::Status; use rocket::local::Client; #[test] fn all() { - let client = Client::new(rocket(Some("test"))).unwrap(); + let client = Client::new(crate::rocket(Some("test"))).unwrap(); // Try to get the main JavaScript file let res = client.get("/js/pinboard.js").dispatch(); diff --git a/src/main.rs b/src/main.rs index 5e007c7..a824ba6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,13 @@ #![feature(proc_macro_hygiene, decl_macro)] -extern crate comrak; -extern crate glob; -extern crate inflector; -#[macro_use] -extern crate rocket; -extern crate rocket_contrib; -#[macro_use] -extern crate serde_derive; -extern crate serde_json; -extern crate toml; +use rocket::{routes, Rocket}; +use serde_derive::Deserialize; +use std::sync::RwLock; +use toml; mod handlers; mod models; -use rocket::Rocket; -use std::sync::RwLock; - type NoteStore = RwLock>; #[derive(Debug, Deserialize)] diff --git a/src/models/note.rs b/src/models/note.rs index cd13ac1..7d67407 100644 --- a/src/models/note.rs +++ b/src/models/note.rs @@ -1,6 +1,7 @@ use comrak; use glob::glob; use inflector::Inflector; +use serde_derive::{Deserialize, Serialize}; use std::fs::File; use std::io::prelude::*; use std::path::PathBuf;