From d7472320a00fa0bfd6b9be904e0730461f093f61 Mon Sep 17 00:00:00 2001 From: 53hornet Date: Sat, 1 Jan 2022 12:27:41 -0500 Subject: chore: pre-ripping-apart --- Cargo.toml | 2 +- src/models/mod.rs | 20 ++++++++++++++++++-- src/repo/mod.rs | 25 +++++++++++++++++-------- src/routes/mod.rs | 9 +++++++++ src/templates/base.hbs | 10 ++++++---- src/templates/favicon.svg | 5 +++++ src/templates/index.hbs | 5 ++++- src/templates/mod.rs | 2 ++ 8 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 src/templates/favicon.svg diff --git a/Cargo.toml b/Cargo.toml index 57eb34d..7deeff7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "twinh" version = "0.1.0" authors = ["Adam T. Carpenter "] -description = "Twin H-Power: A classic car parts catalog and cross-reference tool" +description = "Twin H-Power: A dead simple classic car parts catalog and cross-reference tool" edition = "2018" [dependencies] diff --git a/src/models/mod.rs b/src/models/mod.rs index a252f51..ddef71f 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,4 +1,21 @@ -use serde::{Deserialize, Serialize}; +use crate::error::TwinHError; +use bincode::deserialize; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; +use sled::IVec; + +pub struct Entity { + pub umrn: u64, + pub model: M, +} + +impl Entity { + pub fn new(t: (IVec, IVec)) -> Result { + let (umrn, model) = t; + let umrn = deserialize(&umrn)?; + let model = deserialize(&model)?; + Ok(Entity { umrn, model }) + } +} #[derive(Serialize, Deserialize)] pub struct Part { @@ -12,7 +29,6 @@ pub struct Part { #[derive(Serialize, Deserialize)] pub struct Car { - pub key: u64, pub doors: u8, //pub engine: Engine, pub make: String, diff --git a/src/repo/mod.rs b/src/repo/mod.rs index 80b2814..4c55696 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -35,12 +35,20 @@ pub fn create_new_db() -> Result<(), TwinHError> { pub fn get_all_cars() -> Result, TwinHError> { let cars = REPO_INSTANCE .open_tree(CARS_TREE)? - .into_iter() - .values() - .collect::, _>>()? .iter() - .map(|c| deserialize::(&c)) + .collect::, _>>()? + .into_iter() + .map(|e| crate::models::Entity::new(e)) + .map(|e| e.map(|e| e.model)) .collect::, _>>()?; + // let cars = REPO_INSTANCE + // .open_tree(CARS_TREE)? + // .into_iter() + // .values() + // .collect::, _>>()? + // .iter() + // .map(|c| deserialize::(&c)) + // .collect::, _>>()?; Ok(cars) } @@ -51,7 +59,7 @@ pub fn insert_part(part: Part) -> Result { #[cfg(test)] mod tests { use super::*; - use crate::models::{Car, Part}; + use crate::models::{Car, Entity, Part}; use bincode::serialize; use std::error::Error; @@ -63,7 +71,6 @@ mod tests { .open()?; let car = Car { - key: 1, make: "Hudson".into(), model: "Hornet".into(), trim: "Sedan".into(), @@ -71,8 +78,10 @@ mod tests { year: 1953, }; + let entity = Entity(1, &car); + let tree = db.open_tree(CARS_TREE)?; - let key = car.key.to_be_bytes(); + let key = entity.0.to_be_bytes(); let val = serialize(&car)?; tree.insert(key, val)?; @@ -80,7 +89,7 @@ mod tests { key: 2, number: "ABC123".into(), name: "Rear Wheel Bearing".into(), - fits_cars: vec![car.key], + fits_cars: vec![entity.0], categories: Vec::new(), sources: Vec::new(), }; diff --git a/src/routes/mod.rs b/src/routes/mod.rs index be07aa0..1948657 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize}; pub async fn router(req: Request) -> Result, TwinHError> { match (req.method(), req.uri().path()) { + (&Method::GET, "/favicon.ico") | (&Method::GET, "/favicon.svg") => favicon(), (&Method::GET, "/") => index(req).await, (&Method::GET, "/cars") => cars(req).await, (&Method::GET, "/login") => login().await, @@ -128,6 +129,14 @@ async fn cars(req: Request) -> Result, TwinHError> { todo!() } +fn favicon() -> Result, TwinHError> { + let resp = Response::builder() + .header(hyper::header::CONTENT_TYPE, "image/svg+xml") + .body(templates::FAVICON.into()) + .unwrap(); + Ok(resp) +} + async fn index(req: Request) -> Result, TwinHError> { let query = req.uri().query().unwrap_or_default(); let filter = serde_urlencoded::de::from_str::(query).unwrap(); diff --git a/src/templates/base.hbs b/src/templates/base.hbs index 410a5b2..34f435b 100644 --- a/src/templates/base.hbs +++ b/src/templates/base.hbs @@ -42,10 +42,12 @@ flex-direction: column; background-color: var(--balboa); padding: 0.5em; + color: white; } aside button { font-size: 1em; + padding: 0.5em 0.5em; display: block; background: none; border: 0 none; @@ -61,11 +63,11 @@ padding: 0.5em; } - #hidden:target aside { + #hidemenu:target aside { display: none; } - #hidden:target a.openMenu { + #hidemenu:target a.openMenu { display: block; } @@ -73,7 +75,7 @@ display: none; } - #hidden:target a.closeMenu { + #hidemenu:target a.closeMenu { display: none; } @@ -148,7 +150,7 @@ - +