Run rustfmt on all source files
This commit is contained in:
parent
67d55cadc4
commit
270cffa00e
|
@ -6,7 +6,7 @@ use super::super::NoteStore;
|
|||
#[derive(Serialize)]
|
||||
struct IndexTemplateContext<'a> {
|
||||
app_version: &'a str,
|
||||
notes: Vec<HashMap<&'a str, &'a str>>
|
||||
notes: Vec<HashMap<&'a str, &'a str>>,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
|
@ -21,7 +21,7 @@ fn index(notes: State<NoteStore>) -> Template {
|
|||
}
|
||||
let context = IndexTemplateContext {
|
||||
app_version: env!("CARGO_PKG_VERSION"),
|
||||
notes: note_kvs
|
||||
notes: note_kvs,
|
||||
};
|
||||
Template::render("index", &context)
|
||||
}
|
||||
|
|
|
@ -52,7 +52,10 @@ mod tests {
|
|||
assert_eq!(notes[0].id, "test");
|
||||
assert_eq!(notes[0].index, 0);
|
||||
assert_eq!(notes[0].name, "Test");
|
||||
assert_eq!(notes[0].data, "This is a test list\n\n* One\n* Two\n* Three\n");
|
||||
assert_eq!(
|
||||
notes[0].data,
|
||||
"This is a test list\n\n* One\n* Two\n* Three\n"
|
||||
);
|
||||
// The mtime field can vary, don't test for it
|
||||
// The path field is private, also don't test for it
|
||||
|
||||
|
@ -70,14 +73,16 @@ mod tests {
|
|||
assert_eq!(res.status(), Status::Ok);
|
||||
|
||||
let body = res.body_string().unwrap();
|
||||
assert_eq!(body,
|
||||
assert_eq!(
|
||||
body,
|
||||
r#"<p>This is a test list</p>
|
||||
<ul>
|
||||
<li>One</li>
|
||||
<li>Two</li>
|
||||
<li>Three</li>
|
||||
</ul>
|
||||
"#);
|
||||
"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -98,7 +103,10 @@ mod tests {
|
|||
// The path field is private, also don't test for it
|
||||
|
||||
// Try to get a note that doesn't exist
|
||||
let res = client.get("/notes/doesntexit").header(Accept::JSON).dispatch();
|
||||
let res = client
|
||||
.get("/notes/doesntexit")
|
||||
.header(Accept::JSON)
|
||||
.dispatch();
|
||||
assert_eq!(res.status(), Status::NotFound);
|
||||
|
||||
// FIXME: Test that there is some kind of error in the JSON
|
||||
|
@ -109,7 +117,10 @@ mod tests {
|
|||
let client = Client::new(rocket(Some("test"))).unwrap();
|
||||
|
||||
// Try to get the note and determine what to change it to
|
||||
let mut res = client.get("/notes/updatable").header(Accept::JSON).dispatch();
|
||||
let mut res = client
|
||||
.get("/notes/updatable")
|
||||
.header(Accept::JSON)
|
||||
.dispatch();
|
||||
let body = res.body_string().unwrap();
|
||||
let note = serde_json::from_str::<Note>(body.as_str()).unwrap();
|
||||
assert_eq!(note.data, "Some content");
|
||||
|
@ -127,13 +138,15 @@ mod tests {
|
|||
"name": "Updatable",
|
||||
"path": "test/notes/updatablenote"
|
||||
});
|
||||
let res = client.put("/notes/updatable")
|
||||
let res = client
|
||||
.put("/notes/updatable")
|
||||
.header(ContentType::JSON)
|
||||
.body(new_json.to_string())
|
||||
.dispatch();
|
||||
assert_eq!(res.status(), Status::Ok);
|
||||
|
||||
let mut res = client.get("/notes/updatable")
|
||||
let mut res = client
|
||||
.get("/notes/updatable")
|
||||
.header(Accept::JSON)
|
||||
.dispatch();
|
||||
let body = res.body_string().unwrap();
|
||||
|
@ -142,28 +155,32 @@ mod tests {
|
|||
|
||||
// ... and change it back
|
||||
*new_json.get_mut("data").unwrap() = json!("Some content");
|
||||
let res = client.put("/notes/updatable")
|
||||
let res = client
|
||||
.put("/notes/updatable")
|
||||
.header(ContentType::JSON)
|
||||
.body(new_json.to_string())
|
||||
.dispatch();
|
||||
assert_eq!(res.status(), Status::Ok);
|
||||
|
||||
// Try to change a note that doesn't exist
|
||||
let res = client.put("/notes/doesntexit")
|
||||
let res = client
|
||||
.put("/notes/doesntexit")
|
||||
.header(ContentType::JSON)
|
||||
.body(new_json.to_string())
|
||||
.dispatch();
|
||||
assert_eq!(res.status(), Status::NotFound);
|
||||
|
||||
// Try to change a note without a proper body
|
||||
let res = client.put("/notes/updatable")
|
||||
let res = client
|
||||
.put("/notes/updatable")
|
||||
.header(ContentType::JSON)
|
||||
.body(r#"{}"#)
|
||||
.dispatch();
|
||||
assert_eq!(res.status(), Status::BadRequest);
|
||||
|
||||
// Try to change a note without a proper type (i.e. not JSON)
|
||||
let res = client.put("/notes/updatable")
|
||||
let res = client
|
||||
.put("/notes/updatable")
|
||||
.header(ContentType::Plain)
|
||||
.body("foo bar baz")
|
||||
.dispatch();
|
||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -6,8 +6,10 @@ extern crate glob;
|
|||
extern crate inflector;
|
||||
extern crate rocket;
|
||||
extern crate rocket_contrib;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
#[macro_use] extern crate serde_json;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
#[macro_use]
|
||||
extern crate serde_json;
|
||||
|
||||
mod handlers;
|
||||
mod models;
|
||||
|
@ -21,11 +23,19 @@ fn rocket(notes_path: Option<&str>) -> Rocket {
|
|||
let notes = models::note::Note::load_all(notes_path);
|
||||
rocket::ignite()
|
||||
.manage(RwLock::new(notes))
|
||||
.mount("/", routes![handlers::home::index,
|
||||
handlers::static_files::all])
|
||||
.mount("/notes", routes![handlers::note::index,
|
||||
handlers::note::show_html, handlers::note::show_json,
|
||||
handlers::note::update])
|
||||
.mount(
|
||||
"/",
|
||||
routes![handlers::home::index, handlers::static_files::all],
|
||||
)
|
||||
.mount(
|
||||
"/notes",
|
||||
routes![
|
||||
handlers::note::index,
|
||||
handlers::note::show_html,
|
||||
handlers::note::show_json,
|
||||
handlers::note::update
|
||||
],
|
||||
)
|
||||
.attach(rocket_contrib::Template::fairing())
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ pub struct Note {
|
|||
/// The name of the note, i.e. the person it is for
|
||||
pub name: String,
|
||||
/// The path to the note file
|
||||
path: PathBuf
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
impl Note {
|
||||
|
@ -35,12 +35,14 @@ impl Note {
|
|||
}
|
||||
|
||||
pub fn update_data(&mut self, data: &String) {
|
||||
let mut file = File::create(&self.path)
|
||||
.expect(&format!("Cannot open note file {}",
|
||||
self.path.to_str().unwrap()));
|
||||
file.write_all(data.as_bytes())
|
||||
.expect(&format!("Cannot write note file {}",
|
||||
self.path.to_str().unwrap()));
|
||||
let mut file = File::create(&self.path).expect(&format!(
|
||||
"Cannot open note file {}",
|
||||
self.path.to_str().unwrap()
|
||||
));
|
||||
file.write_all(data.as_bytes()).expect(&format!(
|
||||
"Cannot write note file {}",
|
||||
self.path.to_str().unwrap()
|
||||
));
|
||||
|
||||
self.data = data.clone();
|
||||
let metadata = file.metadata().unwrap();
|
||||
|
@ -52,17 +54,17 @@ impl Note {
|
|||
let mut index = 0;
|
||||
let path_glob = match note_path {
|
||||
Some(dir) => format!("{}/notes/*.note", dir),
|
||||
None => format!("notes/*.note")
|
||||
None => format!("notes/*.note"),
|
||||
};
|
||||
for entry in glob(path_glob.as_str()).unwrap().filter_map(Result::ok) {
|
||||
let file_name = entry.file_name().unwrap().to_str().unwrap();
|
||||
let name = match file_name.find('.') {
|
||||
Some(index) => &file_name[0..index],
|
||||
None => "unknown"
|
||||
None => "unknown",
|
||||
};
|
||||
let mut data = String::new();
|
||||
let mut file = File::open(&entry)
|
||||
.expect(&format!("Cannot open note file {}", file_name));
|
||||
let mut file =
|
||||
File::open(&entry).expect(&format!("Cannot open note file {}", file_name));
|
||||
file.read_to_string(&mut data)
|
||||
.expect(&format!("Cannot read note file {}", file_name));
|
||||
let metadata = file.metadata()
|
||||
|
@ -74,7 +76,7 @@ impl Note {
|
|||
data: data,
|
||||
mtime: metadata.modified().unwrap(),
|
||||
name: String::from(name).to_title_case(),
|
||||
path: entry.clone()
|
||||
path: entry.clone(),
|
||||
};
|
||||
notes.push(note);
|
||||
index += 1;
|
||||
|
@ -90,30 +92,31 @@ mod tests {
|
|||
#[test]
|
||||
fn loads_all_notes() {
|
||||
let notes = Note::load_all(Some("test"));
|
||||
let note_ids: Vec<&str> = notes.iter()
|
||||
.map(|note| note.id.as_ref()).collect();
|
||||
let note_ids: Vec<&str> = notes.iter().map(|note| note.id.as_ref()).collect();
|
||||
assert_eq!(note_ids, vec!["test", "updatable"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn converts_to_html() {
|
||||
let notes = Note::load_all(Some("test"));
|
||||
let note = notes.iter()
|
||||
.find(|note| note.id == "test")
|
||||
.unwrap();
|
||||
assert_eq!(note.to_html(), r#"<p>This is a test list</p>
|
||||
let note = notes.iter().find(|note| note.id == "test").unwrap();
|
||||
assert_eq!(
|
||||
note.to_html(),
|
||||
r#"<p>This is a test list</p>
|
||||
<ul>
|
||||
<li>One</li>
|
||||
<li>Two</li>
|
||||
<li>Three</li>
|
||||
</ul>
|
||||
"#);
|
||||
"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn updates_data() {
|
||||
let mut notes = Note::load_all(Some("test"));
|
||||
let note = notes.iter_mut()
|
||||
let note = notes
|
||||
.iter_mut()
|
||||
.find(|note| note.id == "updatable")
|
||||
.unwrap();
|
||||
assert_eq!(note.data, "Some content");
|
||||
|
@ -126,7 +129,8 @@ mod tests {
|
|||
// Verify that the data is written to the file of the note by
|
||||
// loading them again
|
||||
let mut notes = Note::load_all(Some("test"));
|
||||
let note = notes.iter_mut()
|
||||
let note = notes
|
||||
.iter_mut()
|
||||
.find(|note| note.id == "updatable")
|
||||
.unwrap();
|
||||
assert_eq!(note.data, new_data);
|
||||
|
|
Loading…
Reference in New Issue