Fix clippy issues; change imports

This commit is contained in:
Paul van Tilburg 2022-10-17 19:34:24 +02:00
parent da5179d05a
commit d6dd43c7f2
Signed by: paul
GPG Key ID: C6DE073EDA9EEC4D
4 changed files with 42 additions and 46 deletions

View File

@ -1,10 +1,9 @@
use std::collections::HashMap; use std::collections::HashMap;
use rocket::serde::Serialize; use rocket::{get, serde::Serialize, State};
use rocket::{get, State};
use rocket_dyn_templates::Template; use rocket_dyn_templates::Template;
use super::super::{Config, NoteStore}; use crate::{Config, NoteStore};
#[derive(Serialize)] #[derive(Serialize)]
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
@ -34,9 +33,10 @@ pub(crate) async fn index(notes: &State<NoteStore>, config: &State<Config>) -> T
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use rocket; use rocket::{
use rocket::http::{Accept, Status}; http::{Accept, Status},
use rocket::local::blocking::Client; local::blocking::Client,
};
#[test] #[test]
fn index() { fn index() {

View File

@ -1,8 +1,6 @@
use rocket::serde::json::Json; use rocket::{get, put, serde::json::Json, State};
use rocket::{get, put, State};
use super::super::models::note::Note; use crate::{models::note::Note, NoteStore};
use super::super::NoteStore;
#[get("/", format = "application/json")] #[get("/", format = "application/json")]
pub(crate) async fn index(notes: &State<NoteStore>) -> Option<Json<Vec<Note>>> { pub(crate) async fn index(notes: &State<NoteStore>) -> Option<Json<Vec<Note>>> {
@ -38,9 +36,11 @@ pub(crate) async fn update(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use rocket; use rocket::{
use rocket::http::{Accept, ContentType, Status}; self,
use rocket::local::blocking::Client; http::{Accept, ContentType, Status},
local::blocking::Client,
};
use serde_json::{self, json}; use serde_json::{self, json};
use super::*; use super::*;

View File

@ -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 handlers;
mod models; 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>>; type NoteStore = RwLock<Vec<models::note::Note>>;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -18,9 +20,7 @@ pub struct Config {
} }
fn build_rocket(notes_path: Option<&str>) -> Rocket<Build> { fn build_rocket(notes_path: Option<&str>) -> Rocket<Build> {
use std::fs::File; use std::{fs::File, io::prelude::*, path::Path};
use std::io::prelude::*;
use std::path::Path;
let mut config_data = String::new(); let mut config_data = String::new();
let config_file_name = Path::new(env!("CARGO_MANIFEST_DIR")).join("config.toml"); let config_file_name = Path::new(env!("CARGO_MANIFEST_DIR")).join("config.toml");

View File

@ -1,7 +1,4 @@
use std::fs::File; use std::{fs::File, io::prelude::*, path::PathBuf, time::SystemTime};
use std::io::prelude::*;
use std::path::PathBuf;
use std::time::SystemTime;
use comrak; use comrak;
use glob::glob; use glob::glob;
@ -38,14 +35,11 @@ impl Note {
} }
pub fn update_data(&mut self, data: &String) { pub fn update_data(&mut self, data: &String) {
let mut file = File::create(&self.path).expect(&format!( let path = &self.path;
"Cannot open note file: {}", let mut file = File::create(path)
self.path.to_str().unwrap() .unwrap_or_else(|_| panic!("Cannot open note file: {}", path.display()));
)); file.write_all(data.as_bytes())
file.write_all(data.as_bytes()).expect(&format!( .unwrap_or_else(|_| panic!("Cannot write note file: {}", path.display()));
"Cannot write note file: {}",
self.path.to_str().unwrap()
));
self.data = data.clone(); self.data = data.clone();
let metadata = file.metadata().unwrap(); let metadata = file.metadata().unwrap();
@ -54,36 +48,38 @@ impl Note {
pub fn load_all(note_path: Option<&str>) -> Vec<Self> { pub fn load_all(note_path: Option<&str>) -> Vec<Self> {
let mut notes: Vec<Note> = vec![]; let mut notes: Vec<Note> = vec![];
let mut index = 0;
let path_glob = match note_path { let path_glob = match note_path {
Some(dir) => format!("{}/notes/*.note", dir), 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 file_name = entry.file_name().unwrap().to_str().unwrap();
let name = match file_name.find('.') { let name = match file_name.find('.') {
Some(index) => &file_name[0..index], Some(index) => &file_name[0..index],
None => "unknown", None => "unknown",
}; };
let mut data = String::new(); let mut data = String::new();
let mut file = let mut file = File::open(&entry)
File::open(&entry).expect(&format!("Cannot open note file: {}", file_name)); .unwrap_or_else(|e| panic!("Cannot open note file {file_name}: {e}"));
file.read_to_string(&mut data) 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 let metadata = file
.metadata() .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 { let note = Note {
id: String::from(name), id: String::from(name),
index: index, index,
data: data, data,
mtime: metadata.modified().unwrap(), mtime: metadata.modified().unwrap(),
name: String::from(name).to_title_case(), name: String::from(name).to_title_case(),
path: entry.clone(), path: entry.clone(),
}; };
notes.push(note); notes.push(note);
index += 1;
} }
notes notes
} }