Skip to content

Commit 08c0923

Browse files
committed
Three Changes introduced:
1. Added new method `command_line_flags` to `Builder` to generate list of command line arguments supplied.[file: src/lib.rs] 2. Added new method `get_set` and `get_items` method to `RegexSet` to return immutable reference to it's fields.[file: src/regex_set.rs] 3. Added simple test case for `command_line_flags` method.[file: src/lib.rs]
1 parent 3837dd9 commit 08c0923

File tree

2 files changed

+278
-0
lines changed

2 files changed

+278
-0
lines changed

src/lib.rs

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,240 @@ pub fn builder() -> Builder {
175175
}
176176

177177
impl Builder {
178+
///Generates the command line flags use for creating `builder`
179+
pub fn command_line_flags(&self) -> Vec<String> {
180+
let mut output_vector: Vec<String> = Vec::new();
181+
182+
if let Some(ref header) = self.options.input_header {
183+
//Positional argument 'header'
184+
output_vector.push(header.clone().into());
185+
}
186+
187+
self.options
188+
.bitfield_enums
189+
.get_items()
190+
.iter()
191+
.map(|item| {
192+
output_vector.push("--bitfield-enum".into());
193+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
194+
})
195+
.count();
196+
197+
self.options
198+
.constified_enums
199+
.get_items()
200+
.iter()
201+
.map(|item| {
202+
output_vector.push("--constified-enum".into());
203+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
204+
})
205+
.count();
206+
207+
self.options
208+
.hidden_types
209+
.get_items()
210+
.iter()
211+
.map(|item| {
212+
output_vector.push("--blacklist-type".into());
213+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
214+
})
215+
.count();
216+
217+
if self.options.derive_debug == false {
218+
output_vector.push("--no-derive-debug".into());
219+
}
220+
221+
if self.options.derive_default == false {
222+
output_vector.push("--no-derive-default".into());
223+
} else {
224+
output_vector.push("--with-derive-default".into());
225+
}
226+
227+
if self.options.generate_comments == false {
228+
output_vector.push("--no-doc-comments".into());
229+
}
230+
231+
if self.options.whitelist_recursively == false {
232+
output_vector.push("--no-recursive-whitelist".into());
233+
}
234+
235+
if self.options.objc_extern_crate == true {
236+
output_vector.push("--objc-extern-crate".into());
237+
}
238+
239+
if self.options.builtins == true {
240+
output_vector.push("--builtins".into());
241+
}
242+
243+
if let Some(ref prefix) = self.options.ctypes_prefix {
244+
output_vector.push("--ctypes-prefix".into());
245+
output_vector.push(prefix.clone());
246+
}
247+
248+
self.options
249+
.clang_args
250+
.iter()
251+
.map(|item| {
252+
output_vector.push("--clang-args".into());
253+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
254+
})
255+
.count();
256+
257+
if let Some(ref dummy) = self.options.dummy_uses {
258+
output_vector.push("--dummy-uses".into());
259+
output_vector.push(dummy.clone());
260+
}
261+
262+
if self.options.emit_ast == true {
263+
output_vector.push("--emit-clang-ast".into());
264+
}
265+
266+
if self.options.emit_ir == true {
267+
output_vector.push("--emit-ir".into());
268+
}
269+
if let Some(ref graph) = self.options.emit_ir_graphviz {
270+
output_vector.push("--emit-ir-graphviz".into());
271+
output_vector.push(graph.clone())
272+
}
273+
if self.options.enable_cxx_namespaces == true {
274+
output_vector.push("--enable-cxx-namespaces".into());
275+
}
276+
if self.options.disable_name_namespacing == true {
277+
output_vector.push("--disable-name-namespacing".into());
278+
}
279+
280+
self.options
281+
.links
282+
.iter()
283+
.map(|&(ref item, _)| {
284+
output_vector.push("--framework".into());
285+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
286+
})
287+
.count();
288+
289+
if self.options.codegen_config.functions == false {
290+
output_vector.push("--ignore-functions".into());
291+
}
292+
293+
output_vector.push("--generate".into());
294+
295+
//Temporary placeholder for below 4 options
296+
let mut options:Vec<String> = Vec::new();
297+
if self.options.codegen_config.functions == true {
298+
options.push("function".into());
299+
}
300+
if self.options.codegen_config.types == true {
301+
options.push("types".into());
302+
}
303+
if self.options.codegen_config.vars == true {
304+
options.push("vars".into());
305+
}
306+
if self.options.codegen_config.methods == true {
307+
options.push("methods".into());
308+
}
309+
if self.options.codegen_config.constructors == true {
310+
options.push("constructors".into());
311+
}
312+
if self.options.codegen_config.destructors == true {
313+
options.push("destructors".into());
314+
}
315+
316+
output_vector.push(options.join(","));
317+
318+
if self.options.codegen_config.methods == false{
319+
output_vector.push("--ignore-methods".into());
320+
}
321+
322+
self.options
323+
.links
324+
.iter()
325+
.map(|&(ref item, _)| {
326+
output_vector.push("--clang-args".into());
327+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
328+
})
329+
.count();
330+
331+
if self.options.convert_floats == false {
332+
output_vector.push("--no-convert-floats".into());
333+
}
334+
335+
if self.options.prepend_enum_name == false {
336+
output_vector.push("--no-prepend-enum-name".into());
337+
}
338+
339+
if self.options.unstable_rust == false {
340+
output_vector.push("--no-unstable-rust".into());
341+
}
342+
343+
self.options
344+
.opaque_types
345+
.get_items()
346+
.iter()
347+
.map(|item| {
348+
output_vector.push("--opaque-type".into());
349+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
350+
})
351+
.count();
352+
353+
self.options
354+
.raw_lines
355+
.iter()
356+
.map(|item| {
357+
output_vector.push("--raw-line".into());
358+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
359+
})
360+
.count();
361+
362+
self.options
363+
.links
364+
.iter()
365+
.map(|&(ref item, _)| {
366+
output_vector.push("--static".into());
367+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
368+
})
369+
.count();
370+
371+
if self.options.use_core == true {
372+
output_vector.push("--use-core".into());
373+
}
374+
375+
if self.options.conservative_inline_namespaces == true {
376+
output_vector.push("--conservative-inline-namespaces".into());
377+
}
378+
379+
self.options
380+
.whitelisted_functions
381+
.get_items()
382+
.iter()
383+
.map(|item| {
384+
output_vector.push("--whitelist-function".into());
385+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
386+
})
387+
.count();
388+
389+
self.options
390+
.whitelisted_types
391+
.get_items()
392+
.iter()
393+
.map(|item| {
394+
output_vector.push("--whitelist-type".into());
395+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
396+
})
397+
.count();
398+
399+
self.options
400+
.whitelisted_vars
401+
.get_items()
402+
.iter()
403+
.map(|item| {
404+
output_vector.push("--whitelist-var".into());
405+
output_vector.push(item.trim_left_matches("^").trim_right_matches("$").into());
406+
})
407+
.count();
408+
409+
output_vector
410+
}
411+
178412
/// Set the input C/C++ header.
179413
pub fn header<T: Into<String>>(mut self, header: T) -> Builder {
180414
let header = header.into();
@@ -969,3 +1203,38 @@ pub fn clang_version() -> ClangVersion {
9691203
full: raw_v.clone(),
9701204
}
9711205
}
1206+
1207+
/// Test command_line_flag function.
1208+
#[test]
1209+
fn commandline_flag_unit_test_function() {
1210+
//Test 1
1211+
let bindings = ::builder();
1212+
let command_line_flags = bindings.command_line_flags();
1213+
1214+
let test_cases = vec!["--no-derive-default",
1215+
"--generate", "function,types,vars,methods,constructors,destructors"]
1216+
.iter()
1217+
.map(|&x| x.into())
1218+
.collect::<Vec<String>>();
1219+
1220+
assert!(test_cases.iter().all(|ref x| command_line_flags.contains(x)) );
1221+
1222+
//Test 2
1223+
let bindings = ::builder().header("input_header")
1224+
.whitelisted_type("Distinct_Type")
1225+
.whitelisted_function("safe_function");
1226+
1227+
let command_line_flags = bindings.command_line_flags();
1228+
let test_cases = vec!["input_header",
1229+
"--no-derive-default",
1230+
"--generate", "function,types,vars,methods,constructors,destructors",
1231+
"--whitelist-type", "Distinct_Type",
1232+
"--whitelist-function", "safe_function"]
1233+
.iter()
1234+
.map(|&x| x.into())
1235+
.collect::<Vec<String>>();
1236+
println!("{:?}", command_line_flags);
1237+
1238+
assert!(test_cases.iter().all(|ref x| command_line_flags.contains(x)) );
1239+
1240+
}

src/regex_set.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ impl RegexSet {
3535
self.items.push(format!("^{}$", string.as_ref()));
3636
self.set = None;
3737
}
38+
39+
/// Returns slice of String from its field 'items'
40+
pub fn get_items(&self) -> &[String] {
41+
&self.items[..]
42+
}
43+
/// Returns reference of its field 'set'
44+
pub fn get_set(&self) -> Option<&RxSet> {
45+
self.set.as_ref()
46+
}
3847

3948
/// Construct a RegexSet from the set of entries we've accumulated.
4049
///

0 commit comments

Comments
 (0)