Fix clippy issues; change imports
This commit is contained in:
parent
da5179d05a
commit
d6dd43c7f2
|
@ -1,10 +1,9 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use rocket::serde::Serialize;
|
||||
use rocket::{get, State};
|
||||
use rocket::{get, serde::Serialize, State};
|
||||
use rocket_dyn_templates::Template;
|
||||
|
||||
use super::super::{Config, NoteStore};
|
||||
use crate::{Config, NoteStore};
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
|
@ -34,9 +33,10 @@ pub(crate) async fn index(notes: &State<NoteStore>, config: &State<Config>) -> T
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rocket;
|
||||
use rocket::http::{Accept, Status};
|
||||
use rocket::local::blocking::Client;
|
||||
use rocket::{
|
||||
http::{Accept, Status},
|
||||
local::blocking::Client,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn index() {
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
use rocket::serde::json::Json;
|
||||
use rocket::{get, put, State};
|
||||
use rocket::{get, put, serde::json::Json, State};
|
||||
|
||||
use super::super::models::note::Note;
|
||||
use super::super::NoteStore;
|
||||
use crate::{models::note::Note, NoteStore};
|
||||
|
||||
#[get("/", format = "application/json")]
|
||||
pub(crate) async fn index(notes: &State<NoteStore>) -> Option<Json<Vec<Note>>> {
|
||||
|
@ -38,9 +36,11 @@ pub(crate) async fn update(
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rocket;
|
||||
use rocket::http::{Accept, ContentType, Status};
|
||||
use rocket::local::blocking::Client;
|
||||
use rocket::{
|
||||
self,
|
||||
http::{Accept, ContentType, Status},
|
||||
local::blocking::Client,
|
||||
};
|
||||
use serde_json::{self, json};
|
||||
|
||||
use super::*;
|
||||
|
|
22
src/main.rs
22
src/main.rs
|
@ -1,14 +1,16 @@
|
|||
use std::sync::RwLock;
|
||||
|
||||
use rocket::fs::{relative, FileServer};
|
||||
use rocket::serde::Deserialize;
|
||||
use rocket::{routes, Build, Rocket};
|
||||
use rocket_dyn_templates::Template;
|
||||
use toml;
|
||||
|
||||
mod handlers;
|
||||
mod models;
|
||||
|
||||
use std::sync::RwLock;
|
||||
|
||||
use rocket::{
|
||||
fs::{relative, FileServer},
|
||||
routes,
|
||||
serde::Deserialize,
|
||||
Build, Rocket,
|
||||
};
|
||||
use rocket_dyn_templates::Template;
|
||||
|
||||
type NoteStore = RwLock<Vec<models::note::Note>>;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
@ -18,9 +20,7 @@ pub struct Config {
|
|||
}
|
||||
|
||||
fn build_rocket(notes_path: Option<&str>) -> Rocket<Build> {
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::Path;
|
||||
use std::{fs::File, io::prelude::*, path::Path};
|
||||
|
||||
let mut config_data = String::new();
|
||||
let config_file_name = Path::new(env!("CARGO_MANIFEST_DIR")).join("config.toml");
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::PathBuf;
|
||||
use std::time::SystemTime;
|
||||
use std::{fs::File, io::prelude::*, path::PathBuf, time::SystemTime};
|
||||
|
||||
use comrak;
|
||||
use glob::glob;
|
||||
|
@ -38,14 +35,11 @@ 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 path = &self.path;
|
||||
let mut file = File::create(path)
|
||||
.unwrap_or_else(|_| panic!("Cannot open note file: {}", path.display()));
|
||||
file.write_all(data.as_bytes())
|
||||
.unwrap_or_else(|_| panic!("Cannot write note file: {}", path.display()));
|
||||
|
||||
self.data = data.clone();
|
||||
let metadata = file.metadata().unwrap();
|
||||
|
@ -54,36 +48,38 @@ impl Note {
|
|||
|
||||
pub fn load_all(note_path: Option<&str>) -> Vec<Self> {
|
||||
let mut notes: Vec<Note> = vec![];
|
||||
let mut index = 0;
|
||||
let path_glob = match note_path {
|
||||
Some(dir) => format!("{}/notes/*.note", dir),
|
||||
None => format!("notes/*.note"),
|
||||
None => String::from("notes/*.note"),
|
||||
};
|
||||
for entry in glob(path_glob.as_str()).unwrap().filter_map(Result::ok) {
|
||||
for (index, entry) in glob(path_glob.as_str())
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
.enumerate()
|
||||
{
|
||||
let file_name = entry.file_name().unwrap().to_str().unwrap();
|
||||
let name = match file_name.find('.') {
|
||||
Some(index) => &file_name[0..index],
|
||||
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)
|
||||
.unwrap_or_else(|e| panic!("Cannot open note file {file_name}: {e}"));
|
||||
file.read_to_string(&mut data)
|
||||
.expect(&format!("Cannot read note file: {}", file_name));
|
||||
.unwrap_or_else(|e| panic!("Cannot read note file {file_name}: {e}"));
|
||||
let metadata = file
|
||||
.metadata()
|
||||
.expect(&format!("Cannot get metadata of note file: {}", file_name));
|
||||
.unwrap_or_else(|e| panic!("Cannot get metadata of note file {file_name}: {e}"));
|
||||
|
||||
let note = Note {
|
||||
id: String::from(name),
|
||||
index: index,
|
||||
data: data,
|
||||
index,
|
||||
data,
|
||||
mtime: metadata.modified().unwrap(),
|
||||
name: String::from(name).to_title_case(),
|
||||
path: entry.clone(),
|
||||
};
|
||||
notes.push(note);
|
||||
index += 1;
|
||||
}
|
||||
notes
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue