]>
Commit | Line | Data |
---|---|---|
d8d978eb DM |
1 | #![feature(plugin)] |
2 | #![plugin(phf_macros)] | |
d8d978eb DM |
3 | extern crate phf; |
4 | ||
d6a4ba71 | 5 | extern crate failure; |
22f0adf2 | 6 | use failure::*; |
d8d978eb | 7 | |
d6a4ba71 DM |
8 | extern crate json_schema; |
9 | use json_schema::*; | |
d8d978eb | 10 | |
22f0adf2 DM |
11 | extern crate serde_json; |
12 | #[macro_use] | |
13 | extern crate serde_derive; | |
14 | ||
15 | use serde_json::{json, Value}; | |
16 | ||
d8d978eb DM |
17 | |
18 | static PARAMETERS1: StaticPropertyMap = phf_map! { | |
19 | "force" => Boolean!{ | |
20 | description => "Test for boolean options." | |
21 | }, | |
22 | "text1" => ApiString!{ | |
23 | description => "A simple text string.", | |
24 | min_length => Some(10), | |
25 | max_length => Some(30) | |
26 | }, | |
27 | "count" => Integer!{ | |
28 | description => "A counter for everything.", | |
29 | minimum => Some(0), | |
30 | maximum => Some(10) | |
31 | }, | |
32 | "myarray1" => Array!{ | |
33 | description => "Test Array of simple integers.", | |
34 | items => &PVE_VMID | |
35 | }, | |
d6a4ba71 | 36 | "myarray2" => Jss::Array(JssArray { |
d8d978eb DM |
37 | description: "Test Array of simple integers.", |
38 | optional: Some(false), | |
39 | items: &Object!{description => "Empty Object."}, | |
40 | }), | |
41 | "myobject" => Object!{ | |
42 | description => "TEST Object.", | |
43 | properties => &phf_map!{ | |
d6a4ba71 | 44 | "vmid" => Jss::Reference { reference: &PVE_VMID}, |
d8d978eb DM |
45 | "loop" => Integer!{ |
46 | description => "Totally useless thing.", | |
47 | optional => Some(false) | |
48 | } | |
49 | } | |
50 | }, | |
51 | "emptyobject" => Object!{description => "Empty Object."}, | |
52 | }; | |
53 | ||
54 | ||
22f0adf2 DM |
55 | struct ApiMethod { |
56 | description: &'static str, | |
57 | properties: StaticPropertyMap, | |
58 | returns: Jss, | |
59 | handler: fn(Value) -> Result<Value, Error>, | |
60 | } | |
61 | ||
62 | #[derive(Serialize, Deserialize)] | |
63 | struct Myparam { | |
64 | test: bool, | |
65 | } | |
66 | ||
67 | fn test_api_handler(param: Value) -> Result<Value, Error> { | |
68 | println!("This is a test {}", param); | |
69 | ||
70 | // let force: Option<bool> = Some(false); | |
71 | ||
72 | //if let Some(force) = param.force { | |
73 | //} | |
74 | ||
75 | let force = param["force"].as_bool() | |
76 | .ok_or_else(|| format_err!("meine fehlermeldung"))?; | |
77 | ||
78 | if let Some(force) = param["force"].as_bool() { | |
79 | } | |
80 | ||
81 | let tmp: Myparam = serde_json::from_value(param)?; | |
82 | ||
83 | ||
84 | Ok(json!(null)) | |
85 | } | |
86 | ||
87 | static TEST_API_METHOD: ApiMethod = ApiMethod { | |
88 | description: "This is a simple test.", | |
89 | properties: phf_map! { | |
90 | "force" => Boolean!{ | |
91 | description => "Test for boolean options." | |
92 | } | |
93 | }, | |
94 | returns: Jss::Null, | |
95 | handler: test_api_handler, | |
96 | }; | |
97 | ||
98 | type StaticSubdirMap = phf::Map<&'static str, &'static MethodInfo>; | |
99 | ||
100 | struct MethodInfo { | |
101 | path: &'static str, | |
102 | get: Option<&'static ApiMethod>, | |
103 | subdirs: Option<&'static StaticSubdirMap>, | |
104 | } | |
105 | ||
106 | static API3_NODES: MethodInfo = MethodInfo { | |
107 | path: "", | |
108 | get: Some(&TEST_API_METHOD), | |
109 | subdirs: None, | |
110 | }; | |
111 | ||
112 | static API3: MethodInfo = MethodInfo { | |
113 | path: "", | |
114 | get: Some(&TEST_API_METHOD), | |
115 | subdirs: Some(&phf_map!{"nodes" => &API3_NODES}), | |
116 | }; | |
117 | ||
d8d978eb DM |
118 | fn main() { |
119 | println!("Fast Static Type Definitions 1"); | |
120 | ||
121 | for (k, v) in PARAMETERS1.entries() { | |
122 | println!("Parameter: {} Value: {:?}", k, v); | |
123 | } | |
22f0adf2 | 124 | |
d8d978eb | 125 | } |