do not use phf

This commit is contained in:
Dietmar Maurer 2018-11-01 13:05:45 +01:00
parent 28e47cea55
commit d11f14f77d
6 changed files with 87 additions and 63 deletions

View File

@ -13,9 +13,6 @@ authors = ["Dietmar Maurer <dietmar@proxmox.com>"]
[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"

View File

@ -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<Value, Error>,
}
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);
}
}

View File

@ -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: "",

View File

@ -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;

View File

@ -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!{
static PARAMETERS1: StaticPropertyMap = StaticPropertyMap {
entries: &[
("force", Boolean!{
description => "Test for boolean options."
},
"text1" => ApiString!{
}),
("text1", ApiString!{
description => "A simple text string.",
min_length => Some(10),
max_length => Some(30)
},
"count" => Integer!{
}),
("count", Integer!{
description => "A counter for everything.",
minimum => Some(0),
maximum => Some(10)
},
"myarray1" => Array!{
}),
("myarray1", Array!{
description => "Test Array of simple integers.",
items => &PVE_VMID
},
"myarray2" => Jss::Array(JssArray {
}),
("myarray2", Jss::Array(JssArray {
description: "Test Array of simple integers.",
optional: Some(false),
items: &Object!{description => "Empty Object."},
}),
"myobject" => Object!{
})),
("myobject", Object!{
description => "TEST Object.",
properties => &phf_map!{
"vmid" => Jss::Reference { reference: &PVE_VMID},
"loop" => Integer!{
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<Value, Error> {
static TEST_API_METHOD: ApiMethod = ApiMethod {
description: "This is a simple test.",
properties: phf_map! {
"force" => Boolean!{
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<Body>) -> Response<Body> {
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);
}

25
src/static_map.rs Normal file
View File

@ -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<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q> + std::cmp::PartialEq<Q>,
Q: Eq {
for (ref k, ref v) in self.entries {
if k == key { return Some(v) }
}
None
}
}