use bulk_examples_generator::compile_grammar;
use bulk_examples_generator::config::GeneratorConfig;
use bulk_examples_generator::parallel_generate_examples;
use bulk_examples_generator::parallel_generate_save_examples;
use std::fs::File;
use std::io::prelude::*;
use std::io::Error;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "bulk-examples-generator")]
pub struct Opt {
#[structopt(short, long, parse(from_os_str))]
pub grammar: PathBuf,
#[structopt(short, long)]
pub quantity: u32,
#[structopt(short, long)]
pub start_rule: String,
#[structopt(short, long, verbatim_doc_comment)]
pub out_type: String,
#[structopt(long)]
pub print_progress: bool,
#[structopt(long, required_if("out_type", "file"), parse(from_os_str))]
pub output_folder: Option<PathBuf>,
#[structopt(short, long, default_value = "example-{}.txt")]
pub template_name: String,
#[structopt(short, long, parse(from_os_str))]
pub config_file: Option<PathBuf>,
}
fn main() -> Result<(), Error> {
let opt = Opt::from_args();
let mut config: GeneratorConfig = Default::default();
if let Some(config_file) = &opt.config_file {
config = GeneratorConfig::new(config_file.to_str().unwrap()).unwrap();
}
let mut grammar_string = String::new();
let mut f = File::open(&opt.grammar)?;
f.read_to_string(&mut grammar_string)?;
if opt.out_type == "debug" {
println!("{:?}", &opt);
let g = compile_grammar(grammar_string.clone());
println!("{:?}", g);
let results = parallel_generate_examples(
grammar_string,
opt.quantity,
opt.start_rule,
&config,
true,
false,
);
println!("{:?}", results);
} else if opt.out_type == "stdout" {
parallel_generate_examples(
grammar_string,
opt.quantity,
opt.start_rule,
&config,
opt.print_progress,
true,
);
} else {
parallel_generate_save_examples(
grammar_string,
opt.quantity,
opt.start_rule,
opt.output_folder.unwrap(),
opt.template_name,
&config,
);
}
Ok(())
}