diff --git a/Cargo.toml b/Cargo.toml index 4a3a3966..87013eb7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,6 @@ authors = ["Dietmar Maurer "] [dependencies] failure = "0.1.3" -phf = "0.7.23" -phf_macros = "0.7.23" -derive-new = "0.5.5" serde = "1.0.80" serde_json = "1.0.32" serde_derive = "1.0.80" diff --git a/src/api_info.rs b/src/api_info.rs index 40fe3e71..eb451a99 100644 --- a/src/api_info.rs +++ b/src/api_info.rs @@ -1,7 +1,7 @@ use failure::*; use json_schema::*; -use serde_json::{json, Value}; +use serde_json::{Value}; pub struct ApiMethod { pub description: &'static str, @@ -10,7 +10,7 @@ pub struct ApiMethod { pub handler: fn(Value) -> Result, } -pub type StaticSubdirMap = phf::Map<&'static str, &'static MethodInfo>; +pub type StaticSubdirMap = crate::static_map::StaticMap<'static, &'static str, &'static MethodInfo>; pub struct MethodInfo { pub get: Option<&'static ApiMethod>, @@ -28,14 +28,14 @@ pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo { subdirs: None, }; -pub fn find_method_info<'a>(root: &'a MethodInfo, components: &[&str]) -> Option<&'a MethodInfo> { +pub fn find_method_info(root: &'static MethodInfo, components: &[&str]) -> Option<&'static MethodInfo> { if components.len() == 0 { return Some(root); }; let (dir, rest) = (components[0], &components[1..]); - if let Some(dirmap) = root.subdirs { - if let Some(info) = dirmap.get(dir) { + if let Some(ref dirmap) = root.subdirs { + if let Some(info) = dirmap.get(&dir) { return find_method_info(info, rest); } } diff --git a/src/json_schema.rs b/src/json_schema.rs index 1df78fa5..9212b01f 100644 --- a/src/json_schema.rs +++ b/src/json_schema.rs @@ -1,4 +1,6 @@ -pub type StaticPropertyMap = phf::Map<&'static str, Jss>; +use static_map::StaticMap; + +pub type StaticPropertyMap = StaticMap<'static, &'static str, Jss>; #[derive(Debug)] pub struct JssBoolean { @@ -107,7 +109,7 @@ macro_rules! Array { }} } -pub static EMPTYOBJECT: StaticPropertyMap = phf_map!{}; +pub static EMPTYOBJECT: StaticPropertyMap = StaticPropertyMap { entries: &[] }; pub static DEFAULTOBJECT: JssObject = JssObject { description: "", diff --git a/src/lib.rs b/src/lib.rs index 2b109ce1..835e6958 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,16 +1,8 @@ -#![feature(plugin)] -#![plugin(phf_macros)] - extern crate failure; -extern crate phf; - extern crate serde_json; -// Jss => JavaScript Schema - -//use failure::Error; - +pub mod static_map; pub mod json_schema; pub mod api_info; diff --git a/src/main.rs b/src/main.rs index 95d71254..cd5338f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,8 @@ -#![feature(plugin)] -#![plugin(phf_macros)] -extern crate phf; - extern crate failure; use failure::*; +use apitest::static_map::StaticMap; + use std::collections::HashMap; #[macro_use] @@ -30,40 +28,44 @@ use hyper::{Method, Body, Request, Response, Server, StatusCode}; use hyper::rt::Future; use hyper::service::service_fn_ok; -static PARAMETERS1: StaticPropertyMap = phf_map! { - "force" => Boolean!{ - description => "Test for boolean options." - }, - "text1" => ApiString!{ - description => "A simple text string.", - min_length => Some(10), - max_length => Some(30) - }, - "count" => Integer!{ - description => "A counter for everything.", - minimum => Some(0), - maximum => Some(10) - }, - "myarray1" => Array!{ - description => "Test Array of simple integers.", - items => &PVE_VMID - }, - "myarray2" => Jss::Array(JssArray { - description: "Test Array of simple integers.", - optional: Some(false), - items: &Object!{description => "Empty Object."}, - }), - "myobject" => Object!{ - description => "TEST Object.", - properties => &phf_map!{ - "vmid" => Jss::Reference { reference: &PVE_VMID}, - "loop" => Integer!{ - description => "Totally useless thing.", - optional => Some(false) +static PARAMETERS1: StaticPropertyMap = StaticPropertyMap { + entries: &[ + ("force", Boolean!{ + description => "Test for boolean options." + }), + ("text1", ApiString!{ + description => "A simple text string.", + min_length => Some(10), + max_length => Some(30) + }), + ("count", Integer!{ + description => "A counter for everything.", + minimum => Some(0), + maximum => Some(10) + }), + ("myarray1", Array!{ + description => "Test Array of simple integers.", + items => &PVE_VMID + }), + ("myarray2", Jss::Array(JssArray { + description: "Test Array of simple integers.", + optional: Some(false), + items: &Object!{description => "Empty Object."}, + })), + ("myobject", Object!{ + description => "TEST Object.", + properties => &StaticPropertyMap { + entries: &[ + ("vmid", Jss::Reference { reference: &PVE_VMID}), + ("loop", Integer!{ + description => "Totally useless thing.", + optional => Some(false) + }) + ] } - } - }, - "emptyobject" => Object!{description => "Empty Object."}, + }), + ("emptyobject", Object!{description => "Empty Object."}), + ] }; @@ -95,11 +97,13 @@ fn test_api_handler(param: Value) -> Result { static TEST_API_METHOD: ApiMethod = ApiMethod { description: "This is a simple test.", - properties: phf_map! { - "force" => Boolean!{ - optional => Some(true), - description => "Test for boolean options." - } + properties: StaticPropertyMap { + entries: &[ + ("force", Boolean!{ + optional => Some(true), + description => "Test for boolean options." + }) + ] }, returns: Jss::Null, handler: test_api_handler, @@ -113,7 +117,11 @@ static API3_NODES: MethodInfo = MethodInfo { static API_ROOT: MethodInfo = MethodInfo { get: Some(&TEST_API_METHOD), - subdirs: Some(&phf_map!{"nodes" => &API3_NODES}), + subdirs: Some(&StaticSubdirMap { + entries: &[ + ("nodes", &API3_NODES), + ] + }), ..METHOD_INFO_DEFAULTS }; @@ -193,7 +201,7 @@ fn handle_request(req: Request) -> Response { fn main() { println!("Fast Static Type Definitions 1"); - for (k, v) in PARAMETERS1.entries() { + for (k, v) in PARAMETERS1.entries { println!("Parameter: {} Value: {:?}", k, v); } diff --git a/src/static_map.rs b/src/static_map.rs new file mode 100644 index 00000000..06bcb298 --- /dev/null +++ b/src/static_map.rs @@ -0,0 +1,25 @@ +use std::borrow::Borrow; + +#[derive(Debug)] +pub struct StaticMap<'a, K, V> { + pub entries: &'a [(K,V)], +} + +impl<'a, K, V> StaticMap<'a, K, V> + where K: Eq { + + #[inline] + pub fn len(&self) -> usize { + self.entries.len() + } + + pub fn get(&self, key: &Q) -> Option<&V> + where K: Borrow + std::cmp::PartialEq, + Q: Eq { + for (ref k, ref v) in self.entries { + if k == key { return Some(v) } + } + + None + } +}