Port to the 2018 edition
This commit is contained in:
parent
22821fedbc
commit
0c78996f66
|
@ -8,6 +8,7 @@ a family or group of friends.
|
|||
"""
|
||||
readme = "README.md"
|
||||
license = "MIT"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
comrak = "0.7.0"
|
||||
|
|
|
@ -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<NoteStore>, config: State<Config>) -> Template {
|
||||
pub(crate) fn index(notes: State<NoteStore>, config: State<Config>) -> 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();
|
||||
|
|
|
@ -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<NoteStore>) -> Option<Json<Vec<Note>>> {
|
||||
pub(crate) fn index(notes: State<NoteStore>) -> Option<Json<Vec<Note>>> {
|
||||
let notes = notes.read().unwrap();
|
||||
Some(Json(notes.clone()))
|
||||
}
|
||||
|
||||
#[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 note = notes.iter().find(|note| note.id == note_id)?;
|
||||
Some(note.to_html())
|
||||
}
|
||||
|
||||
#[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 note = notes.iter().find(|note| note.id == note_id)?;
|
||||
Some(Json(note.clone()))
|
||||
}
|
||||
|
||||
#[put("/<note_id>", format = "application/json", data = "<new_note>")]
|
||||
pub fn update(
|
||||
pub(crate) fn update(
|
||||
note_id: String,
|
||||
new_note: Json<Note>,
|
||||
notes: State<NoteStore>,
|
||||
|
@ -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
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use rocket::get;
|
||||
use rocket::response::NamedFile;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[get("/<path..>", rank = 5)]
|
||||
pub fn all(path: PathBuf) -> Option<NamedFile> {
|
||||
pub(crate) fn all(path: PathBuf) -> Option<NamedFile> {
|
||||
NamedFile::open(
|
||||
Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("static")
|
||||
|
@ -13,13 +14,12 @@ pub fn all(path: PathBuf) -> Option<NamedFile> {
|
|||
|
||||
#[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();
|
||||
|
|
17
src/main.rs
17
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<Vec<models::note::Note>>;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue