summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml2
-rw-r--r--artifacts/makes-models5
-rw-r--r--artifacts/template.json22
-rw-r--r--src/error.rs25
-rw-r--r--src/handlers/mod.rs1
-rw-r--r--src/main.rs7
-rw-r--r--src/models/mod.rs31
-rw-r--r--src/repo/constants.rs11
-rw-r--r--src/repo/mod.rs80
-rw-r--r--src/templates/login.html6
10 files changed, 145 insertions, 45 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 72dff12..465754c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,8 +4,6 @@ version = "0.1.0"
authors = ["Adam T. Carpenter <atc@53hor.net>"]
edition = "2018"
-# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
-
[dependencies]
bincode = "1.3"
env_logger = "0.8"
diff --git a/artifacts/makes-models b/artifacts/makes-models
deleted file mode 100644
index a18b407..0000000
--- a/artifacts/makes-models
+++ /dev/null
@@ -1,5 +0,0 @@
-Hudson
- Hornet
- Wasp
- Pacemaker
-
diff --git a/artifacts/template.json b/artifacts/template.json
deleted file mode 100644
index ddacc2a..0000000
--- a/artifacts/template.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "type": "part",
- "shortName": "Wheel Bearing",
- "fitsCars": [
- {
- "type": "car",
- "year": 1953,
- "make": "Hudson",
- "model": "Hornet",
- "engine": {
- "type": "engine",
- "cylinders": 6,
- "displacement": 308,
- "layout": "I"
- },
- "doors": 4,
- "transmission": "Dual-Range Hydramatic"
- }
- ],
- "sources": ["url://", "url://"],
- "categories": ["Wheel & Tire", "Drivetrain"]
-}
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..06d7d1e
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,25 @@
+use bincode::Error as bincode_e;
+use sled::Error as sled_e;
+
+#[derive(Debug)]
+pub struct TwinHError;
+
+impl std::error::Error for TwinHError {}
+
+impl std::fmt::Display for TwinHError {
+ fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
+ todo!()
+ }
+}
+
+impl From<sled_e> for TwinHError {
+ fn from(_: sled_e) -> Self {
+ todo!()
+ }
+}
+
+impl From<bincode_e> for TwinHError {
+ fn from(_: bincode_e) -> Self {
+ todo!()
+ }
+}
diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs
index 1aba908..3ad7f49 100644
--- a/src/handlers/mod.rs
+++ b/src/handlers/mod.rs
@@ -5,6 +5,7 @@ use hyper::Method;
use hyper::StatusCode;
use hyper::{Body, Request, Response};
use serde::{Deserialize, Serialize};
+use sled::Db;
use std::convert::Infallible; // TODO:
pub async fn router(req: Request<Body>) -> Result<Response<Body>, Infallible> {
diff --git a/src/main.rs b/src/main.rs
index 168a5f5..1947ff8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,16 +5,19 @@ use hyper::{
use std::convert::Infallible; // TODO:
mod config;
+mod error;
mod handlers;
mod models;
+mod repo;
mod templates;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = config::INSTANCE.addr;
- let make_svc =
- make_service_fn(move |_conn| async { Ok::<_, Infallible>(service_fn(handlers::router)) });
+ let make_svc = make_service_fn(move |_conn| async {
+ Ok::<_, Infallible>(service_fn(|req| handlers::router(req)))
+ });
let server = Server::bind(&addr).serve(make_svc);
let graceful = server.with_graceful_shutdown(shutdown_signal());
diff --git a/src/models/mod.rs b/src/models/mod.rs
index 5830a1b..a252f51 100644
--- a/src/models/mod.rs
+++ b/src/models/mod.rs
@@ -1,27 +1,30 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
-pub struct Car {
- doors: u8,
- engine: Engine,
- make: String,
- model: String,
- transmission: Transmission,
- trim: String,
- year: u16, // I'll worry about this in 65534 AD.
+pub struct Part {
+ pub key: u64,
+ pub number: String,
+ pub name: String,
+ pub sources: Vec<u64>,
+ pub categories: Vec<u64>,
+ pub fits_cars: Vec<u64>,
}
#[derive(Serialize, Deserialize)]
-pub struct Part {
- name: String,
- compatible_cars: Vec<Car>,
- sources: Vec<Source>,
- categories: Vec<Category>,
+pub struct Car {
+ pub key: u64,
+ pub doors: u8,
+ //pub engine: Engine,
+ pub make: String,
+ pub model: String,
+ //pub transmission: Transmission,
+ pub trim: String,
+ pub year: u16,
}
#[derive(Serialize, Deserialize)]
pub enum Source {
- Uri(String),
+ Web(String),
}
#[derive(Serialize, Deserialize)]
diff --git a/src/repo/constants.rs b/src/repo/constants.rs
new file mode 100644
index 0000000..e168a77
--- /dev/null
+++ b/src/repo/constants.rs
@@ -0,0 +1,11 @@
+use once_cell::sync::Lazy;
+use sled::{Config, Db};
+
+pub const PARTS_TREE: &str = "parts";
+pub const CARS_TREE: &str = "cars";
+pub static REPO_INSTANCE: Lazy<Db> = Lazy::new(|| {
+ Config::default()
+ .path("/tmp/twinh")
+ .open()
+ .expect("Couldn't open DB!")
+});
diff --git a/src/repo/mod.rs b/src/repo/mod.rs
new file mode 100644
index 0000000..d4c5e44
--- /dev/null
+++ b/src/repo/mod.rs
@@ -0,0 +1,80 @@
+use crate::error::TwinHError;
+use crate::models::Car;
+use crate::models::Part;
+use bincode::deserialize;
+use constants::*;
+
+mod constants;
+
+pub fn create_new_db() -> Result<(), TwinHError> {
+ let config = sled::Config::default()
+ .path("/var/db/twinh")
+ .create_new(true);
+ let db = config.open()?;
+ Ok(())
+}
+
+pub fn get_all_cars() -> Result<Vec<Car>, TwinHError> {
+ let cars = REPO_INSTANCE
+ .open_tree(CARS_TREE)?
+ .into_iter()
+ .values()
+ .collect::<Result<Vec<_>, _>>()?
+ .iter()
+ .map(|c| deserialize::<Car>(&c))
+ .collect::<Result<Vec<_>, _>>()?;
+ Ok(cars)
+}
+
+pub fn insert_part(part: Part) -> Result<u64, TwinHError> {
+ todo!()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::models::{Car, Part};
+ use bincode::serialize;
+ use std::error::Error;
+
+ #[test]
+ fn test_insert_part() -> Result<(), Box<dyn Error>> {
+ let db = sled::Config::default()
+ .mode(sled::Mode::HighThroughput)
+ .temporary(true)
+ .open()?;
+
+ let car = Car {
+ key: 1,
+ make: "Hudson".into(),
+ model: "Hornet".into(),
+ trim: "Sedan".into(),
+ doors: 4,
+ year: 1953,
+ };
+
+ let tree = db.open_tree(CARS_TREE)?;
+ let key = car.key.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![car.key],
+ 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>(&part_out.unwrap())?;
+ assert_eq!(part.key, part_out.key);
+
+ Ok(())
+ }
+}
diff --git a/src/templates/login.html b/src/templates/login.html
new file mode 100644
index 0000000..9329a22
--- /dev/null
+++ b/src/templates/login.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<html>
+ <body>
+ <a href="/login/google">Login with Google</a>
+ </body>
+</html>