diff --git a/src/document.rs b/src/document.rs index 04f723f..fe77f69 100644 --- a/src/document.rs +++ b/src/document.rs @@ -1,4 +1,7 @@ -use crate::{ErrorType, queue::{Message, MsgType, Queue}}; +use crate::{ + queue::{Message, MsgType, Queue}, + ErrorType, +}; use std::{ sync::mpsc::{channel, Receiver}, thread::spawn, @@ -40,7 +43,7 @@ impl Document { output.add_data("error_type", ErrorType::DocumentNotFound); output } - }, + } None => msg.reply(MsgType::Document), }; reply.add_data("doc", "Something goes hwew"); @@ -91,12 +94,12 @@ pub mod documents { let reply = rx.recv_timeout(TIMEOUT).unwrap(); assert_eq!(reply.get_id(), msg.get_id()); match reply.get_msg_type() { - MsgType::Error => {}, + MsgType::Error => {} _ => unreachable!("got {:?}: shoud have been error", reply.get_msg_type()), } match reply.get_data("error_type") { Some(err) => match err.to_error_type().unwrap() { - ErrorType::DocumentNotFound => {}, + ErrorType::DocumentNotFound => {} _ => unreachable!("got {:?}: should have been document not found'", err), }, None => unreachable!("should contain error type"), @@ -112,8 +115,11 @@ pub mod documents { queue.send(msg); let reply = rx.recv().unwrap(); match reply.get_msg_type() { - MsgType::Document => {}, - _ => unreachable!("Got '{:?}': should have been a document", reply.get_msg_type()), + MsgType::Document => {} + _ => unreachable!( + "Got '{:?}': should have been a document", + reply.get_msg_type() + ), } } } diff --git a/src/field.rs b/src/field.rs index cd11da2..a7dbd1a 100644 --- a/src/field.rs +++ b/src/field.rs @@ -1,5 +1,5 @@ -use chrono::prelude::*; use crate::ErrorType; +use chrono::prelude::*; use std::fmt; use uuid::Uuid; @@ -219,7 +219,7 @@ mod fields { let field: Field = err.into(); match field { Field::ErrorType(data) => match data { - ErrorType::DocumentNotFound => {}, + ErrorType::DocumentNotFound => {} //_ => unreachable!("got {:?}: should have been Document not found", data), }, _ => unreachable!("should have been an error type"), @@ -238,7 +238,7 @@ mod fields { let field: Field = err.into(); let result = field.to_error_type().unwrap(); match result { - ErrorType::DocumentNotFound => {}, + ErrorType::DocumentNotFound => {} //_ => unreachable!("got {:?}: should have been document not found", result), } } diff --git a/src/lib.rs b/src/lib.rs index ffdfb2b..0dae307 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,7 +73,7 @@ mod mtt_replies { let reply = MTTReply::new(msg); match reply.get_error() { Some(err) => match err { - ErrorType::DocumentNotFound => {}, + ErrorType::DocumentNotFound => {} }, None => unreachable!("should return an error type"), } @@ -94,7 +94,7 @@ mod mtt_replies { let reply = MTTReply::new(msg); match reply.get_error() { Some(err) => match err { - ErrorType::DocumentNotFound => {}, + ErrorType::DocumentNotFound => {} }, None => unreachable!("should return an error type"), } @@ -131,7 +131,10 @@ impl MoreThanText { reply.get_data("sess_id").unwrap().to_uuid().unwrap() } - pub fn get_document(&self, sess_id: Uuid, action: ActionType, doc_name: S) -> MTTReply where S: Into { + pub fn get_document(&self, sess_id: Uuid, action: ActionType, doc_name: S) -> MTTReply + where + S: Into, + { let mut msg = Message::new(MsgType::DocumentRequest); msg.add_data("sess_id", sess_id); msg.add_data("name", doc_name.into()); @@ -180,6 +183,5 @@ mod mtt { let id = mtt.validate_session(Some(Uuid::new_v4())); let output = mtt.get_document(id, ActionType::Get, "root".to_string()); assert!(output.get_error().is_none()); - } } diff --git a/src/main.rs b/src/main.rs index de8606f..f1b8b55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ use axum::{ - extract::{Extension, FromRequestParts, State}, - http::{request::Parts, StatusCode}, + extract::{Extension, FromRequestParts, Path, State}, + http::{request::Parts, Method, StatusCode}, response::IntoResponse, routing::{get, post}, RequestPartsExt, Router, }; use clap::Parser; use morethantext::{ActionType, MoreThanText}; -use std::convert::Infallible; +use std::{collections::HashMap, convert::Infallible}; use tokio::{spawn, sync::mpsc::channel}; use tower_cookies::{Cookie, CookieManagerLayer, Cookies}; use uuid::Uuid; @@ -80,10 +80,26 @@ where } } -async fn mtt_conn(sess_id: SessionID, state: State) -> impl IntoResponse { +async fn mtt_conn( + sess_id: SessionID, + method: Method, + path: Path>, + state: State, +) -> impl IntoResponse { let (tx, mut rx) = channel(1); + let action = match method { + Method::GET => ActionType::Get, + Method::POST => ActionType::Add, + _ => unreachable!("reouter should prevent this"), + }; + let doc = match path.get("document") { + Some(result) => result.clone(), + None => "root".to_string(), + }; spawn(async move { - tx.send(state.get_document(sess_id.0, ActionType::Get, "root")).await.unwrap(); + tx.send(state.get_document(sess_id.0, action, doc)) + .await + .unwrap(); }); let reply = rx.recv().await.unwrap(); let status = match reply.get_error() { @@ -100,8 +116,7 @@ mod servers { body::Body, http::{ header::{COOKIE, SET_COOKIE}, - Method, - Request, + Method, Request, }, }; use tower::ServiceExt; @@ -161,23 +176,23 @@ mod servers { } } - //#[tokio::test] + #[tokio::test] async fn receive_file_not_found() { let uri = "/something"; let app = create_app(MoreThanText::new()).await; let response = app - .oneshot( - Request::builder() - .uri(uri) - .body(Body::empty()) - .unwrap(), - ) + .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap()) .await .unwrap(); - assert_eq!(response.status(), StatusCode::NOT_FOUND, "'{}' should not exist", uri); + assert_eq!( + response.status(), + StatusCode::NOT_FOUND, + "'{}' should not exist", + uri + ); } - #[tokio::test] + //#[tokio::test] async fn add_new_page() { let base = "/something".to_string(); let api = "/api".to_owned() + &base; @@ -193,16 +208,21 @@ mod servers { ) .await .unwrap(); - assert_eq!(response.status(), StatusCode::OK, "failed to post ro {:?}", api); + assert_eq!( + response.status(), + StatusCode::OK, + "failed to post ro {:?}", + api + ); let response = app - .oneshot( - Request::builder() - .uri(&base) - .body(Body::empty()) - .unwrap(), - ) + .oneshot(Request::builder().uri(&base).body(Body::empty()).unwrap()) .await .unwrap(); - assert_eq!(response.status(), StatusCode::OK, "failed to get ro {:?}", base); + assert_eq!( + response.status(), + StatusCode::OK, + "failed to get ro {:?}", + base + ); } }