5
0
mirror of git://git.proxmox.com/git/proxmox-backup.git synced 2025-02-01 05:47:22 +03:00

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] [dependencies]
failure = "0.1.3" failure = "0.1.3"
phf = "0.7.23"
phf_macros = "0.7.23"
derive-new = "0.5.5"
serde = "1.0.80" serde = "1.0.80"
serde_json = "1.0.32" serde_json = "1.0.32"
serde_derive = "1.0.80" serde_derive = "1.0.80"

View File

@ -1,7 +1,7 @@
use failure::*; use failure::*;
use json_schema::*; use json_schema::*;
use serde_json::{json, Value}; use serde_json::{Value};
pub struct ApiMethod { pub struct ApiMethod {
pub description: &'static str, pub description: &'static str,
@ -10,7 +10,7 @@ pub struct ApiMethod {
pub handler: fn(Value) -> Result<Value, Error>, 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 struct MethodInfo {
pub get: Option<&'static ApiMethod>, pub get: Option<&'static ApiMethod>,
@ -28,14 +28,14 @@ pub static METHOD_INFO_DEFAULTS: MethodInfo = MethodInfo {
subdirs: None, 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); }; if components.len() == 0 { return Some(root); };
let (dir, rest) = (components[0], &components[1..]); let (dir, rest) = (components[0], &components[1..]);
if let Some(dirmap) = root.subdirs { if let Some(ref dirmap) = root.subdirs {
if let Some(info) = dirmap.get(dir) { if let Some(info) = dirmap.get(&dir) {
return find_method_info(info, rest); 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)] #[derive(Debug)]
pub struct JssBoolean { 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 { pub static DEFAULTOBJECT: JssObject = JssObject {
description: "", description: "",

View File

@ -1,16 +1,8 @@
#![feature(plugin)]
#![plugin(phf_macros)]
extern crate failure; extern crate failure;
extern crate phf;
extern crate serde_json; extern crate serde_json;
// Jss => JavaScript Schema
//use failure::Error;
pub mod static_map;
pub mod json_schema; pub mod json_schema;
pub mod api_info; pub mod api_info;

View File

@ -1,10 +1,8 @@
#![feature(plugin)]
#![plugin(phf_macros)]
extern crate phf;
extern crate failure; extern crate failure;
use failure::*; use failure::*;
use apitest::static_map::StaticMap;
use std::collections::HashMap; use std::collections::HashMap;
#[macro_use] #[macro_use]
@ -30,40 +28,44 @@ use hyper::{Method, Body, Request, Response, Server, StatusCode};
use hyper::rt::Future; use hyper::rt::Future;
use hyper::service::service_fn_ok; use hyper::service::service_fn_ok;
static PARAMETERS1: StaticPropertyMap = phf_map! { static PARAMETERS1: StaticPropertyMap = StaticPropertyMap {
"force" => Boolean!{ entries: &[
description => "Test for boolean options." ("force", Boolean!{
}, description => "Test for boolean options."
"text1" => ApiString!{ }),
description => "A simple text string.", ("text1", ApiString!{
min_length => Some(10), description => "A simple text string.",
max_length => Some(30) min_length => Some(10),
}, max_length => Some(30)
"count" => Integer!{ }),
description => "A counter for everything.", ("count", Integer!{
minimum => Some(0), description => "A counter for everything.",
maximum => Some(10) minimum => Some(0),
}, maximum => Some(10)
"myarray1" => Array!{ }),
description => "Test Array of simple integers.", ("myarray1", Array!{
items => &PVE_VMID description => "Test Array of simple integers.",
}, items => &PVE_VMID
"myarray2" => Jss::Array(JssArray { }),
description: "Test Array of simple integers.", ("myarray2", Jss::Array(JssArray {
optional: Some(false), description: "Test Array of simple integers.",
items: &Object!{description => "Empty Object."}, optional: Some(false),
}), items: &Object!{description => "Empty Object."},
"myobject" => Object!{ })),
description => "TEST Object.", ("myobject", Object!{
properties => &phf_map!{ description => "TEST Object.",
"vmid" => Jss::Reference { reference: &PVE_VMID}, properties => &StaticPropertyMap {
"loop" => Integer!{ entries: &[
description => "Totally useless thing.", ("vmid", Jss::Reference { reference: &PVE_VMID}),
optional => Some(false) ("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 { static TEST_API_METHOD: ApiMethod = ApiMethod {
description: "This is a simple test.", description: "This is a simple test.",
properties: phf_map! { properties: StaticPropertyMap {
"force" => Boolean!{ entries: &[
optional => Some(true), ("force", Boolean!{
description => "Test for boolean options." optional => Some(true),
} description => "Test for boolean options."
})
]
}, },
returns: Jss::Null, returns: Jss::Null,
handler: test_api_handler, handler: test_api_handler,
@ -113,7 +117,11 @@ static API3_NODES: MethodInfo = MethodInfo {
static API_ROOT: MethodInfo = MethodInfo { static API_ROOT: MethodInfo = MethodInfo {
get: Some(&TEST_API_METHOD), get: Some(&TEST_API_METHOD),
subdirs: Some(&phf_map!{"nodes" => &API3_NODES}), subdirs: Some(&StaticSubdirMap {
entries: &[
("nodes", &API3_NODES),
]
}),
..METHOD_INFO_DEFAULTS ..METHOD_INFO_DEFAULTS
}; };
@ -193,7 +201,7 @@ fn handle_request(req: Request<Body>) -> Response<Body> {
fn main() { fn main() {
println!("Fast Static Type Definitions 1"); println!("Fast Static Type Definitions 1");
for (k, v) in PARAMETERS1.entries() { for (k, v) in PARAMETERS1.entries {
println!("Parameter: {} Value: {:?}", k, v); 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
}
}