From ba49aa806a48839b61fb261f7ccd007a507d8d50 Mon Sep 17 00:00:00 2001 From: 53hornet Date: Sat, 22 Jan 2022 08:45:24 -0500 Subject: feat: experimental cgi fork --- src/repo/constants.rs | 2 - src/repo/mod.rs | 107 -------------------------------------------------- 2 files changed, 109 deletions(-) delete mode 100644 src/repo/constants.rs delete mode 100644 src/repo/mod.rs (limited to 'src/repo') diff --git a/src/repo/constants.rs b/src/repo/constants.rs deleted file mode 100644 index 6da641f..0000000 --- a/src/repo/constants.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub const PARTS_TREE: &str = "parts"; -pub const CARS_TREE: &str = "cars"; diff --git a/src/repo/mod.rs b/src/repo/mod.rs deleted file mode 100644 index 4c55696..0000000 --- a/src/repo/mod.rs +++ /dev/null @@ -1,107 +0,0 @@ -use crate::config; -use crate::error::TwinHError; -use crate::models::Car; -use crate::models::Part; -use bincode::deserialize; -use constants::*; -use sled::{Config, Db}; - -mod constants; - -lazy_static! { - static ref REPO_INSTANCE: Db = match Config::default().path(&config::INSTANCE.data_dir).open() { - Err(e) => panic!("failed to open database: {}", e), - Ok(db) => db, - }; -} - -pub fn create_demo_db() -> Result<(), TwinHError> { - let db = sled::Config::default() - .path(&config::INSTANCE.data_dir) - .create_new(true) - .open()?; - let cars_tree = db.open_tree(CARS_TREE)?; - Ok(()) -} - -pub fn create_new_db() -> Result<(), TwinHError> { - sled::Config::default() - .path(&config::INSTANCE.data_dir) - .create_new(true) - .open()?; - Ok(()) -} - -pub fn get_all_cars() -> Result, TwinHError> { - let cars = REPO_INSTANCE - .open_tree(CARS_TREE)? - .iter() - .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) -} - -pub fn insert_part(part: Part) -> Result { - todo!() -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::models::{Car, Entity, Part}; - use bincode::serialize; - use std::error::Error; - - #[test] - fn test_insert_part() -> Result<(), Box> { - let db = sled::Config::default() - .mode(sled::Mode::HighThroughput) - .temporary(true) - .open()?; - - let car = Car { - make: "Hudson".into(), - model: "Hornet".into(), - trim: "Sedan".into(), - doors: 4, - year: 1953, - }; - - let entity = Entity(1, &car); - - let tree = db.open_tree(CARS_TREE)?; - let key = entity.0.to_be_bytes(); - let val = serialize(&car)?; - tree.insert(key, val)?; - - let part = Part { - key: 2, - number: "ABC123".into(), - name: "Rear Wheel Bearing".into(), - fits_cars: vec![entity.0], - categories: Vec::new(), - sources: Vec::new(), - }; - - let tree = db.open_tree(PARTS_TREE)?; - let (key, val) = (serialize(&part.key)?, serialize(&part)?); - tree.insert(key.clone(), val)?; - - let part_out = tree.get(key)?; - let part_out = deserialize::(&part_out.unwrap())?; - assert_eq!(part.key, part_out.key); - - Ok(()) - } -} -- cgit v1.2.3