crustacean-executable 0.3.1

A Rust bindings generator executable.
// Copyright 2016 Kyle Mayes
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]
#![cfg_attr(feature="clippy", warn(clippy))]

extern crate clap;
extern crate crustacean;

use std::io;

use clap::{App, AppSettings, Arg};

use crustacean::{Filter, Generator};

fn main() {
    let matches = App::new("crustacean")
        .version(env!("CARGO_PKG_VERSION"))
        .author("Kyle Mayes <kyle@mayeses.com>")
        .about("Generates Rust bindings.")
        .usage("crustacean [FLAGS] [OPTIONS] <headers>... [-- [CLANG]]")
        .setting(AppSettings::TrailingVarArg)
        .arg(Arg::with_name("headers")
            .help("The headers to be parsed")
            .required(true)
            .multiple(true))
        .arg(Arg::with_name("allow-unsupported-types")
            .help("Sets whether unsupported types will be ignored and treated as `void`")
            .short("a")
            .long("allow-unsupported-types"))
        .arg(Arg::with_name("derive-enum")
            .help("Sets the traits that will be derived by generated enums")
            .short("e")
            .long("derive-enum")
            .takes_value(true))
        .arg(Arg::with_name("derive-struct")
            .help("Sets the traits that will be derived by generated structs")
            .short("s")
            .long("derive-struct")
            .takes_value(true))
        .arg(Arg::with_name("filter")
            .help("Sets which kinds of declarations will be filtered out")
            .short("f")
            .long("filter")
            .takes_value(true)
            .possible_values(&["none", "system", "non-main"]))
        .arg(Arg::with_name("nested-mod")
            .help("Sets the name of the `mod` that will contain nested enums, structs, and unions")
            .short("n")
            .long("nested-mod")
            .takes_value(true))
        .arg(Arg::with_name("primitive")
            .help("Sets the prefix for primitive types")
            .short("p")
            .long("primitive")
            .takes_value(true))
        .get_matches();

    let mut generator = Generator::new();

    let trailing = matches.values_of("headers").unwrap().collect::<Vec<_>>();
    let (headers, arguments) = if let Some(index) = trailing.iter().position(|v| *v == "--") {
        (&trailing[..index], &trailing[index + 1..])
    } else {
        (&trailing[..], &[] as &[&str])
    };

    for header in headers {
        generator.header(header, arguments);
    }
    generator.allow_unsupported_types(matches.is_present("allow-unsupported-types"));
    if let Some(derive_enum) = matches.values_of("derive-enum") {
        generator.derive_enum(&derive_enum.collect::<Vec<_>>()[..]);
    }
    if let Some(derive_struct) = matches.values_of("derive-struct") {
        generator.derive_struct(&derive_struct.collect::<Vec<_>>()[..]);
    }
    generator.display_diagnostics(true);
    if let Some(filter) = matches.value_of("filter") {
        let filter = match filter {
            "none" => Filter::None,
            "system" => Filter::System,
            "non-main" => Filter::NonMain,
            _ => unreachable!(),
        };
        generator.filter(filter);
    }
    if let Some(primitive) = matches.values_of("primitive") {
        generator.primitive(&primitive.collect::<Vec<_>>()[..]);
    }
    if let Some(nested_mod) = matches.value_of("nested-mod") {
        generator.nested_mod(nested_mod);
    }

    generator.write(&mut io::stdout()).unwrap();
}