From 504b3597245f8338e74c355acb653fe6c6ce8620 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 3 Nov 2018 10:42:48 +0100 Subject: [PATCH] another way to initialize the api tree --- Cargo.toml | 1 + src/api3.rs | 60 +++++++++++++++++++++++++++++++++++++++++ src/api_info.rs | 41 +++++++++++----------------- src/lib.rs | 7 +++++ src/main.rs | 72 ++++++------------------------------------------- 5 files changed, 91 insertions(+), 90 deletions(-) create mode 100644 src/api3.rs diff --git a/Cargo.toml b/Cargo.toml index f408fe5e..50d67172 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,4 @@ serde_json = "1.0.32" serde_derive = "1.0.80" url = "1.7.1" hyper = "0.12.13" +lazy_static = "1.1.0" \ No newline at end of file diff --git a/src/api3.rs b/src/api3.rs new file mode 100644 index 00000000..5723fd83 --- /dev/null +++ b/src/api3.rs @@ -0,0 +1,60 @@ +use failure::*; +use std::collections::HashMap; + + +use crate::json_schema::*; +use crate::api_info::*; +use serde_json::{json, Value}; + + +static GET: ApiMethod = ApiMethod { + handler: test_api_handler, + description: "This is a simple test.", + parameters: &Object!{ + properties => &propertymap!{ + force => &Boolean!{ + optional => Some(true), + description => "Test for boolean options." + } + } + }, + returns: &Jss::Null, +}; + +fn test_api_handler(param: Value) -> Result { + println!("This is a test {}", param); + + // let force: Option = Some(false); + + //if let Some(force) = param.force { + //} + + let _force = param["force"].as_bool() + .ok_or_else(|| format_err!("missing parameter 'force'"))?; + + if let Some(_force) = param["force"].as_bool() { + } + + + Ok(json!(null)) +} + +pub fn get_api_definition() -> MethodInfo { + + let subdir1 = methodinfo!{ + get => &GET + }; + + let info = methodinfo!{ + get => &GET, + subdirs => { + let mut map = HashMap::new(); + + map.insert(String::from("subdir"), subdir1); + + map + } + }; + + info +} diff --git a/src/api_info.rs b/src/api_info.rs index baf950a4..521f6f4e 100644 --- a/src/api_info.rs +++ b/src/api_info.rs @@ -3,46 +3,35 @@ use failure::*; use crate::json_schema::*; use serde_json::{Value}; +use std::collections::HashMap; + #[derive(Debug)] pub struct ApiMethod<'a> { pub description: &'a str, - pub properties: &'a PropertyMap<'a>, + pub parameters: &'a Jss<'a>, pub returns: &'a Jss<'a>, pub handler: fn(Value) -> Result, } -pub type SubdirMap<'a> = crate::static_map::StaticMap<'a, &'a str, &'a MethodInfo<'a>>; - -#[macro_export] -macro_rules! subdirmap { - ($($name:ident => $e:expr),*) => {{ - SubdirMap { - entries: &[ - $( ( stringify!($name), $e), )* - ] - } - }} -} - #[derive(Debug)] -pub struct MethodInfo<'a> { - pub get: Option<&'a ApiMethod<'a>>, - pub put: Option<&'a ApiMethod<'a>>, - pub post: Option<&'a ApiMethod<'a>>, - pub delete: Option<&'a ApiMethod<'a>>, - pub subdirs: Option<&'a SubdirMap<'a>>, +pub struct MethodInfo { + pub get: Option<&'static ApiMethod<'static>>, + pub put: Option<&'static ApiMethod<'static>>, + pub post: Option<&'static ApiMethod<'static>>, + pub delete: Option<&'static ApiMethod<'static>>, + pub subdirs: Option>, } -impl<'a> MethodInfo<'a> { +impl MethodInfo { - pub fn find_method(&'a self, components: &[&str]) -> Option<&'a MethodInfo<'a>> { + pub fn find_method<'a>(&'a self, components: &[&str]) -> Option<&'a MethodInfo> { if components.len() == 0 { return Some(self); }; let (dir, rest) = (components[0], &components[1..]); if let Some(ref dirmap) = self.subdirs { - if let Some(info) = dirmap.get(&dir) { + if let Some(ref info) = dirmap.get(dir) { return info.find_method(rest); } } @@ -51,7 +40,7 @@ impl<'a> MethodInfo<'a> { } } -pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo { +pub const METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo { get: None, put: None, post: None, @@ -61,8 +50,8 @@ pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo { #[macro_export] macro_rules! methodinfo { - ($name:ident, $($option:ident => $e:expr),*) => { - static $name: MethodInfo = MethodInfo { + ($($option:ident => $e:expr),*) => { + MethodInfo { $( $option: Some($e), )* ..METHOD_INFO_DEFAULTS }; diff --git a/src/lib.rs b/src/lib.rs index f82a7a17..0391a950 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,10 @@ pub mod static_map; + +#[macro_use] pub mod json_schema; + +#[macro_use] pub mod api_info; + +pub mod api3; + diff --git a/src/main.rs b/src/main.rs index 8949003e..7bf5f4e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,14 @@ -#[macro_use] extern crate apitest; -use failure::*; +//use failure::*; use std::collections::HashMap; +use lazy_static::lazy_static; - -use apitest::json_schema::*; +//use apitest::json_schema::*; use apitest::api_info::*; -use serde_derive::{Serialize, Deserialize}; +//use serde_derive::{Serialize, Deserialize}; use serde_json::{json, Value}; use url::form_urlencoded; @@ -18,65 +17,6 @@ use hyper::{Method, Body, Request, Response, Server, StatusCode}; use hyper::rt::Future; use hyper::service::service_fn_ok; - - - -#[derive(Serialize, Deserialize)] -struct Myparam { - test: bool, -} - -fn test_api_handler(param: Value) -> Result { - println!("This is a test {}", param); - - // let force: Option = Some(false); - - //if let Some(force) = param.force { - //} - - let _force = param["force"].as_bool() - .ok_or_else(|| format_err!("missing parameter 'force'"))?; - - if let Some(_force) = param["force"].as_bool() { - } - - let _tmp: Myparam = serde_json::from_value(param)?; - - - Ok(json!(null)) -} - -static TEST_API_METHOD: ApiMethod = ApiMethod { - description: "This is a simple test.", - properties: &propertymap!{ - force => &Boolean!{ - optional => Some(true), - description => "Test for boolean options." - } - }, - returns: &Jss::Null, - handler: test_api_handler, -}; - - -methodinfo!{ - API3_TEST, -} - -methodinfo!{ - API3_NODES, - get => &TEST_API_METHOD -} - -methodinfo!{ - API_ROOT, - get => &TEST_API_METHOD, - subdirs => &subdirmap!{ - test => &API3_TEST, - nodes => &API3_NODES - } -} - macro_rules! http_error { ($status:ident, $msg:expr) => {{ let mut resp = Response::new(Body::from($msg)); @@ -160,6 +100,10 @@ fn handle_request(req: Request) -> Response { Response::new(Body::from("RETURN WEB GUI\n")) } +lazy_static!{ + static ref API_ROOT: MethodInfo = apitest::api3::get_api_definition(); +} + fn main() { println!("Fast Static Type Definitions 1");