From f17db0abfd0bd95718cea119a8af7296c43c13e9 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 15 Nov 2018 17:07:10 +0100 Subject: [PATCH] cleanup module names --- src/{api_config.rs => api/config.rs} | 3 +- src/{json_schema.rs => api/schema.rs} | 0 src/{api_server.rs => api/server.rs} | 6 +- src/api3.rs | 4 +- src/api_info.rs | 91 --------------------------- src/lib.rs | 13 ++-- src/main.rs | 7 +-- 7 files changed, 17 insertions(+), 107 deletions(-) rename src/{api_config.rs => api/config.rs} (96%) rename src/{json_schema.rs => api/schema.rs} (100%) rename src/{api_server.rs => api/server.rs} (99%) delete mode 100644 src/api_info.rs diff --git a/src/api_config.rs b/src/api/config.rs similarity index 96% rename from src/api_config.rs rename to src/api/config.rs index db9cec92..dd7dccf2 100644 --- a/src/api_config.rs +++ b/src/api/config.rs @@ -1,5 +1,4 @@ -use crate::api_info::*; -//use crate::json_schema::*; +use crate::api::router::*; use std::collections::HashMap; use std::path::{PathBuf}; diff --git a/src/json_schema.rs b/src/api/schema.rs similarity index 100% rename from src/json_schema.rs rename to src/api/schema.rs diff --git a/src/api_server.rs b/src/api/server.rs similarity index 99% rename from src/api_server.rs rename to src/api/server.rs index 6a89d960..889c3a92 100644 --- a/src/api_server.rs +++ b/src/api/server.rs @@ -1,6 +1,6 @@ -use crate::json_schema::*; -use crate::api_info::*; -use crate::api_config::*; +use crate::api::schema::*; +use crate::api::router::*; +use crate::api::config::*; use std::fmt; use std::path::{PathBuf}; diff --git a/src/api3.rs b/src/api3.rs index a3ef5e5c..752793a2 100644 --- a/src/api3.rs +++ b/src/api3.rs @@ -2,8 +2,8 @@ use failure::*; use std::collections::HashMap; -use crate::json_schema::*; -use crate::api_info::*; +use crate::api::schema::*; +use crate::api::router::*; use serde_json::{json, Value}; fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result { diff --git a/src/api_info.rs b/src/api_info.rs deleted file mode 100644 index 7d339e30..00000000 --- a/src/api_info.rs +++ /dev/null @@ -1,91 +0,0 @@ -use failure::*; - -use crate::json_schema::*; -use serde_json::{Value}; -use std::collections::HashMap; - -pub struct ApiMethod { - pub description: &'static str, - pub parameters: Schema, - pub returns: Schema, - pub handler: fn(Value, &ApiMethod) -> Result, -} - -pub enum SubRoute { - None, - Hash(HashMap), - MatchAll { router: Box, param_name: String }, -} - -pub struct Router { - pub get: Option, - pub put: Option, - pub post: Option, - pub delete: Option, - pub subroute: SubRoute, -} - -impl Router { - - pub fn new() -> Self { - Self { - get: None, - put: None, - post: None, - delete: None, - subroute: SubRoute::None - } - } - - pub fn subdirs(mut self, map: HashMap) -> Self { - self.subroute = SubRoute::Hash(map); - self - } - - pub fn match_all(mut self, router: Router) -> Self { - self.subroute = SubRoute::MatchAll { router: Box::new(router), param_name: "test".into() }; - self - } - - pub fn get(mut self, m: ApiMethod) -> Self { - self.get = Some(m); - self - } - - pub fn find_route(&self, components: &[&str]) -> Option<&Router> { - - if components.len() == 0 { return Some(self); }; - - let (dir, rest) = (components[0], &components[1..]); - - match self.subroute { - SubRoute::None => {}, - SubRoute::Hash(ref dirmap) => { - if let Some(ref router) = dirmap.get(dir) { - println!("FOUND SUBDIR {}", dir); - return router.find_route(rest); - } - } - SubRoute::MatchAll { ref router, ref param_name } => { - println!("URI PARAM {} = {}", param_name, dir); // fixme: store somewhere - return router.find_route(rest); - }, - } - - None - } -} - -// fixme: remove - not required? -#[macro_export] -macro_rules! methodinfo { - ($($option:ident => $e:expr),*) => {{ - let info = Router::new(); - - $( - info.$option = Some($e); - )* - - info - }} -} diff --git a/src/lib.rs b/src/lib.rs index 61f3e893..b739a89a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,17 @@ pub mod static_map; #[macro_use] -pub mod json_schema; +pub mod api { -#[macro_use] -pub mod api_info; + #[macro_use] + pub mod schema; + #[macro_use] + pub mod router; + pub mod config; + pub mod server; -pub mod api_config; +} -pub mod api_server; pub mod api3; diff --git a/src/main.rs b/src/main.rs index 07287dcc..140bb2ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ -//use apitest::json_schema::*; -use apitest::api_info::*; -use apitest::api_config::*; -use apitest::api_server::*; +use apitest::api::router::*; +use apitest::api::config::*; +use apitest::api::server::*; //use failure::*; use lazy_static::lazy_static;