diff --git a/src/main.rs b/src/main.rs index 921e7e6..e27869e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ extern crate rocket; #[macro_use] extern crate serde; -use serde_json::{json, Value, Number}; +use serde_json::{json, Number, Value}; //use rocket::request::FromSegments; //use rocket::http::uri::Segments; @@ -14,17 +14,17 @@ use rocket_contrib::templates::Template; mod store; +use crate::store::form::{render_card_fields, render_empty_fields, RenderedCard, RenderedField}; +use crate::store::model::FieldKind; use crate::store::Store; -use rocket::{State, Data}; +use indexmap::map::IndexMap; use parking_lot::RwLock; -use rocket::response::Redirect; use rocket::http::Status; -use rocket::request::{Form, LenientForm, FromForm, FormItems}; +use rocket::request::{Form, FormItems, FromForm, LenientForm}; +use rocket::response::Redirect; +use rocket::{Data, State}; use std::env; -use crate::store::form::{RenderedField, render_empty_fields, RenderedCard, render_card_fields}; -use crate::store::model::FieldKind; use std::ops::Deref; -use indexmap::map::IndexMap; #[derive(Serialize, Debug)] pub struct ListContext<'a> { @@ -36,8 +36,14 @@ pub struct ListContext<'a> { const per_page: usize = 20; -fn find_page_with_card(store : &Store, card_id : usize) -> Option { - if let Some((n, _)) = store.data.cards.iter().enumerate().find(|(_n, (id, _card))| **id == card_id) { +fn find_page_with_card(store: &Store, card_id: usize) -> Option { + if let Some((n, _)) = store + .data + .cards + .iter() + .enumerate() + .find(|(_n, (id, _card))| **id == card_id) + { Some(n / per_page) } else { None @@ -48,7 +54,6 @@ fn find_page_with_card(store : &Store, card_id : usize) -> Option { fn route_index(store: State>, page: Option, card: Option) -> Template { let rg = store.read(); - let mut page = page.unwrap_or_default(); let n_pages = (rg.data.cards.len() as f64 / per_page as f64).ceil() as usize; @@ -64,7 +69,10 @@ fn route_index(store: State>, page: Option, card: Option>, page: Option, card: Option>, page: Option, card: Option + pub data: IndexMap, } impl<'a> FromForm<'a> for MapFromForm { @@ -100,7 +109,7 @@ impl<'a> FromForm<'a> for MapFromForm { } } -fn collect_card_form(store: &Store, mut form: MapFromForm) -> IndexMap:: { +fn collect_card_form(store: &Store, mut form: MapFromForm) -> IndexMap { let mut card = IndexMap::new(); for (k, field) in &store.model.fields { @@ -110,9 +119,7 @@ fn collect_card_form(store: &Store, mut form: MapFromForm) -> IndexMap:: { Value::String(input) } - FieldKind::Bool { .. } => { - serde_json::to_value(true).unwrap() - } + FieldKind::Bool { .. } => serde_json::to_value(true).unwrap(), FieldKind::Int { min, max, default } => { let mut val: i64 = if input.is_empty() { *default @@ -151,20 +158,19 @@ fn collect_card_form(store: &Store, mut form: MapFromForm) -> IndexMap:: { - let tags: Vec = input.split(' ') + let tags: Vec = input + .split(' ') .map(ToOwned::to_owned) .filter_map(|tag| { if options.contains(&tag) { @@ -172,12 +178,14 @@ fn collect_card_form(store: &Store, mut form: MapFromForm) -> IndexMap:: { - let tags: Vec = input.split(' ') + let tags: Vec = input + .split(' ') .map(str::trim) .filter(|s| !s.is_empty()) .map(ToOwned::to_owned) @@ -216,7 +224,7 @@ fn route_add(store: State>) -> Template { let rg = store.read(); let context = AddCardContext { - fields: render_empty_fields(&rg) + fields: render_empty_fields(&rg), }; Template::render("add", context) @@ -249,7 +257,11 @@ fn route_edit(id: usize, store: State>) -> Option