Port to the 2018 edition

This commit is contained in:
Paul van Tilburg 2020-02-03 21:38:20 +01:00
parent 22821fedbc
commit 0c78996f66
Signed by: paul
GPG Key ID: C6DE073EDA9EEC4D
6 changed files with 24 additions and 30 deletions

View File

@ -8,6 +8,7 @@ a family or group of friends.
""" """
readme = "README.md" readme = "README.md"
license = "MIT" license = "MIT"
edition = "2018"
[dependencies] [dependencies]
comrak = "0.7.0" comrak = "0.7.0"

View File

@ -1,7 +1,8 @@
use super::super::Config; use super::super::Config;
use super::super::NoteStore; use super::super::NoteStore;
use rocket::State; use rocket::{get, State};
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
use serde_derive::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Serialize)] #[derive(Serialize)]
@ -12,7 +13,7 @@ struct IndexTemplateContext<'a> {
} }
#[get("/")] #[get("/")]
pub fn index(notes: State<NoteStore>, config: State<Config>) -> Template { pub(crate) fn index(notes: State<NoteStore>, config: State<Config>) -> Template {
let notes = notes.read().unwrap(); let notes = notes.read().unwrap();
let mut note_kvs = vec![]; let mut note_kvs = vec![];
for note in notes.iter() { for note in notes.iter() {
@ -37,7 +38,7 @@ mod tests {
#[test] #[test]
fn index() { 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 // Try to get the index and verify the body
let mut res = client.get("/").header(Accept::HTML).dispatch(); let mut res = client.get("/").header(Accept::HTML).dispatch();

View File

@ -1,30 +1,30 @@
use super::super::models::note::Note; use super::super::models::note::Note;
use super::super::NoteStore; use super::super::NoteStore;
use rocket::State; use rocket::{get, put, State};
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
#[get("/", format = "application/json")] #[get("/", format = "application/json")]
pub fn index(notes: State<NoteStore>) -> Option<Json<Vec<Note>>> { pub(crate) fn index(notes: State<NoteStore>) -> Option<Json<Vec<Note>>> {
let notes = notes.read().unwrap(); let notes = notes.read().unwrap();
Some(Json(notes.clone())) Some(Json(notes.clone()))
} }
#[get("/<note_id>", format = "text/html")] #[get("/<note_id>", format = "text/html")]
pub fn show_html(note_id: String, notes: State<NoteStore>) -> Option<String> { pub(crate) fn show_html(note_id: String, notes: State<NoteStore>) -> Option<String> {
let notes = notes.read().unwrap(); let notes = notes.read().unwrap();
let note = notes.iter().find(|note| note.id == note_id)?; let note = notes.iter().find(|note| note.id == note_id)?;
Some(note.to_html()) Some(note.to_html())
} }
#[get("/<note_id>", format = "application/json", rank = 2)] #[get("/<note_id>", format = "application/json", rank = 2)]
pub fn show_json(note_id: String, notes: State<NoteStore>) -> Option<Json<Note>> { pub(crate) fn show_json(note_id: String, notes: State<NoteStore>) -> Option<Json<Note>> {
let notes = notes.read().unwrap(); let notes = notes.read().unwrap();
let note = notes.iter().find(|note| note.id == note_id)?; let note = notes.iter().find(|note| note.id == note_id)?;
Some(Json(note.clone())) Some(Json(note.clone()))
} }
#[put("/<note_id>", format = "application/json", data = "<new_note>")] #[put("/<note_id>", format = "application/json", data = "<new_note>")]
pub fn update( pub(crate) fn update(
note_id: String, note_id: String,
new_note: Json<Note>, new_note: Json<Note>,
notes: State<NoteStore>, notes: State<NoteStore>,
@ -41,11 +41,11 @@ mod tests {
use rocket; use rocket;
use rocket::http::{Accept, ContentType, Status}; use rocket::http::{Accept, ContentType, Status};
use rocket::local::Client; use rocket::local::Client;
use serde_json; use serde_json::{self, json};
#[test] #[test]
fn index() { 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 // Try to get all the notes
let mut res = client.get("/notes").header(Accept::JSON).dispatch(); let mut res = client.get("/notes").header(Accept::JSON).dispatch();
@ -70,7 +70,7 @@ mod tests {
#[test] #[test]
fn show_html() { 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 // Try to get the note and verify the body
let mut res = client.get("/notes/test").header(Accept::HTML).dispatch(); let mut res = client.get("/notes/test").header(Accept::HTML).dispatch();
@ -91,7 +91,7 @@ mod tests {
#[test] #[test]
fn show_json() { 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 // Try to get the note and verify the body
let mut res = client.get("/notes/test").header(Accept::JSON).dispatch(); let mut res = client.get("/notes/test").header(Accept::JSON).dispatch();
@ -118,7 +118,7 @@ mod tests {
#[test] #[test]
fn update() { 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 // Try to get the note and determine what to change it to
let mut res = client let mut res = client
@ -180,7 +180,7 @@ mod tests {
.header(ContentType::JSON) .header(ContentType::JSON)
.body(r#"{}"#) .body(r#"{}"#)
.dispatch(); .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) // Try to change a note without a proper type (i.e. not JSON)
let res = client let res = client

View File

@ -1,8 +1,9 @@
use rocket::get;
use rocket::response::NamedFile; use rocket::response::NamedFile;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
#[get("/<path..>", rank = 5)] #[get("/<path..>", rank = 5)]
pub fn all(path: PathBuf) -> Option<NamedFile> { pub(crate) fn all(path: PathBuf) -> Option<NamedFile> {
NamedFile::open( NamedFile::open(
Path::new(env!("CARGO_MANIFEST_DIR")) Path::new(env!("CARGO_MANIFEST_DIR"))
.join("static") .join("static")
@ -13,13 +14,12 @@ pub fn all(path: PathBuf) -> Option<NamedFile> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use rocket;
use rocket::http::Status; use rocket::http::Status;
use rocket::local::Client; use rocket::local::Client;
#[test] #[test]
fn all() { 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 // Try to get the main JavaScript file
let res = client.get("/js/pinboard.js").dispatch(); let res = client.get("/js/pinboard.js").dispatch();

View File

@ -1,22 +1,13 @@
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
extern crate comrak; use rocket::{routes, Rocket};
extern crate glob; use serde_derive::Deserialize;
extern crate inflector; use std::sync::RwLock;
#[macro_use] use toml;
extern crate rocket;
extern crate rocket_contrib;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate toml;
mod handlers; mod handlers;
mod models; mod models;
use rocket::Rocket;
use std::sync::RwLock;
type NoteStore = RwLock<Vec<models::note::Note>>; type NoteStore = RwLock<Vec<models::note::Note>>;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]

View File

@ -1,6 +1,7 @@
use comrak; use comrak;
use glob::glob; use glob::glob;
use inflector::Inflector; use inflector::Inflector;
use serde_derive::{Deserialize, Serialize};
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::PathBuf; use std::path::PathBuf;