]>
Commit | Line | Data |
---|---|---|
d8d978eb DM |
1 | #![feature(plugin)] |
2 | #![plugin(phf_macros)] | |
3 | ||
4 | extern crate failure; | |
5 | ||
6 | extern crate phf; | |
7 | ||
8 | ||
9 | use failure::Error; | |
10 | ||
11 | type StaticPropertyMap = phf::Map<&'static str, ApiTypeDef>; | |
12 | ||
13 | #[derive(Debug)] | |
14 | struct ApiTypeDefBoolean { | |
15 | description: &'static str, | |
16 | optional: Option<bool>, | |
17 | default: Option<bool>, | |
18 | } | |
19 | ||
20 | #[derive(Debug)] | |
21 | struct ApiTypeDefInteger { | |
22 | description: &'static str, | |
23 | optional: Option<bool>, | |
24 | minimum: Option<usize>, | |
25 | maximum: Option<usize>, | |
26 | default: Option<usize>, | |
27 | } | |
28 | ||
29 | #[derive(Debug)] | |
30 | struct ApiTypeDefString { | |
31 | description: &'static str, | |
32 | optional: Option<bool>, | |
33 | default: Option<&'static str>, | |
34 | min_length: Option<usize>, | |
35 | max_length: Option<usize>, | |
36 | } | |
37 | ||
38 | #[derive(Debug)] | |
39 | struct ApiTypeDefArray { | |
40 | description: &'static str, | |
41 | optional: Option<bool>, | |
42 | items: &'static ApiTypeDef, | |
43 | } | |
44 | ||
45 | #[derive(Debug)] | |
46 | struct ApiTypeDefObject { | |
47 | description: &'static str, | |
48 | optional: Option<bool>, | |
49 | additional_properties: Option<bool>, | |
50 | properties: &'static StaticPropertyMap, | |
51 | } | |
52 | ||
53 | #[derive(Debug)] | |
54 | enum ApiTypeDef { | |
55 | Null, | |
56 | Boolean(ApiTypeDefBoolean), | |
57 | Integer(ApiTypeDefInteger), | |
58 | String(ApiTypeDefString), | |
59 | Object(ApiTypeDefObject), | |
60 | Array(ApiTypeDefArray), | |
61 | Reference { reference: &'static ApiTypeDef }, | |
62 | } | |
63 | ||
64 | static DEFAULTBOOL: ApiTypeDefBoolean = ApiTypeDefBoolean { | |
65 | description: "", | |
66 | optional: None, | |
67 | default: None, | |
68 | }; | |
69 | ||
70 | macro_rules! Boolean { | |
71 | ($($name:ident => $e:expr),*) => {{ | |
72 | ApiTypeDef::Boolean(ApiTypeDefBoolean { $($name: $e, )* ..DEFAULTBOOL}) | |
73 | }} | |
74 | } | |
75 | ||
76 | static DEFAULTINTEGER: ApiTypeDefInteger = ApiTypeDefInteger { | |
77 | description: "", | |
78 | optional: None, | |
79 | default: None, | |
80 | minimum: None, | |
81 | maximum: None, | |
82 | }; | |
83 | ||
84 | macro_rules! Integer { | |
85 | ($($name:ident => $e:expr),*) => {{ | |
86 | ApiTypeDef::Integer(ApiTypeDefInteger { $($name: $e, )* ..DEFAULTINTEGER}) | |
87 | }} | |
88 | } | |
89 | ||
90 | static DEFAULTSTRING: ApiTypeDefString = ApiTypeDefString { | |
91 | description: "", | |
92 | optional: None, | |
93 | default: None, | |
94 | min_length: None, | |
95 | max_length: None, | |
96 | }; | |
97 | ||
98 | macro_rules! ApiString { | |
99 | ($($name:ident => $e:expr),*) => {{ | |
100 | ApiTypeDef::String(ApiTypeDefString { $($name: $e, )* ..DEFAULTSTRING}) | |
101 | }} | |
102 | } | |
103 | ||
104 | static DEFAULTARRAY: ApiTypeDefArray = ApiTypeDefArray { | |
105 | description: "", | |
106 | optional: None, | |
107 | items: &ApiTypeDef::Null, // is this a reasonable default?? | |
108 | }; | |
109 | ||
110 | macro_rules! Array { | |
111 | ($($name:ident => $e:expr),*) => {{ | |
112 | ApiTypeDef::Array(ApiTypeDefArray { $($name: $e, )* ..DEFAULTARRAY}) | |
113 | }} | |
114 | } | |
115 | ||
116 | static EMPTYOBJECT: StaticPropertyMap = phf_map!{}; | |
117 | ||
118 | static DEFAULTOBJECT: ApiTypeDefObject = ApiTypeDefObject { | |
119 | description: "", | |
120 | optional: None, | |
121 | additional_properties: None, | |
122 | properties: &EMPTYOBJECT, // is this a reasonable default?? | |
123 | }; | |
124 | ||
125 | macro_rules! Object { | |
126 | ($($name:ident => $e:expr),*) => {{ | |
127 | ApiTypeDef::Object(ApiTypeDefObject { $($name: $e, )* ..DEFAULTOBJECT}) | |
128 | }} | |
129 | } | |
130 | ||
131 | ||
132 | // Standard Option Definitions | |
133 | static PVE_VMID: ApiTypeDef = Integer!{ | |
134 | description => "The (unique) ID of the VM.", | |
135 | minimum => Some(1) | |
136 | }; | |
137 | ||
138 | ||
139 | static PARAMETERS1: StaticPropertyMap = phf_map! { | |
140 | "force" => Boolean!{ | |
141 | description => "Test for boolean options." | |
142 | }, | |
143 | "text1" => ApiString!{ | |
144 | description => "A simple text string.", | |
145 | min_length => Some(10), | |
146 | max_length => Some(30) | |
147 | }, | |
148 | "count" => Integer!{ | |
149 | description => "A counter for everything.", | |
150 | minimum => Some(0), | |
151 | maximum => Some(10) | |
152 | }, | |
153 | "myarray1" => Array!{ | |
154 | description => "Test Array of simple integers.", | |
155 | items => &PVE_VMID | |
156 | }, | |
157 | "myarray2" => ApiTypeDef::Array(ApiTypeDefArray { | |
158 | description: "Test Array of simple integers.", | |
159 | optional: Some(false), | |
160 | items: &Object!{description => "Empty Object."}, | |
161 | }), | |
162 | "myobject" => Object!{ | |
163 | description => "TEST Object.", | |
164 | properties => &phf_map!{ | |
165 | "vmid" => ApiTypeDef::Reference { reference: &PVE_VMID}, | |
166 | "loop" => Integer!{ | |
167 | description => "Totally useless thing.", | |
168 | optional => Some(false) | |
169 | } | |
170 | } | |
171 | }, | |
172 | "emptyobject" => Object!{description => "Empty Object."}, | |
173 | }; | |
174 | ||
175 | ||
176 | fn main() { | |
177 | println!("Fast Static Type Definitions 1"); | |
178 | ||
179 | for (k, v) in PARAMETERS1.entries() { | |
180 | println!("Parameter: {} Value: {:?}", k, v); | |
181 | } | |
182 | ||
183 | } |