1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
use backend::environment::Environment; use backend::models as m; use backend; use clap::{Arg, App, SubCommand, ArgMatches}; use errors::*; use naming; use options::Options; use std::path::Path; fn parse_id_converter(input: &str) -> Result<Box<naming::Naming>> { let mut parts = input.split(":"); if let Some(first) = parts.next() { if let Some(second) = parts.next() { let naming: Box<naming::FromNaming> = match first { "camel" => Box::new(naming::CamelCase::new()), "snake" => Box::new(naming::SnakeCase::new()), _ => return Err(format!("Not a valid source: {}", first).into()), }; let naming = match second { "lower_camel" => naming.to_lower_camel(), "upper_camel" => naming.to_upper_camel(), "lower_snake" => naming.to_lower_snake(), "upper_snake" => naming.to_upper_snake(), _ => return Err(format!("Not a valid target: {}", second).into()), }; return Ok(naming); } } return Err(format!("Invalid --id-conversion argument: {}", input).into()); } pub struct CompileOptions { } pub fn compile_options<'a, 'b>(name: &str) -> App<'a, 'b> { SubCommand::with_name(name) .arg(Arg::with_name("backend") .long("backend") .short("b") .help("Backend to used to emit code") .takes_value(true) .required(true)) .arg(Arg::with_name("module") .long("module") .short("m") .takes_value(true) .multiple(true) .number_of_values(1) .help("Modules to load for a given backend")) .arg(Arg::with_name("path") .long("path") .short("p") .takes_value(true) .multiple(true) .number_of_values(1) .help("Paths to look for definitions.")) .arg(Arg::with_name("out") .long("out") .short("o") .takes_value(true) .help("Output directory.")) .arg(Arg::with_name("id-converter") .long("id-converter") .takes_value(true) .help("Convert arguments")) .arg(Arg::with_name("package-prefix") .long("package-prefix") .takes_value(true) .help("Package prefix to use when generating classes")) .arg(Arg::with_name("file") .long("file") .help("File to compile") .takes_value(true) .multiple(true) .number_of_values(1)) .arg(Arg::with_name("package") .long("package") .help("Packages to compile") .takes_value(true) .multiple(true) .number_of_values(1)) } fn setup_compiler<'a> (matches: &'a ArgMatches) -> Result<(Vec<&'a Path>, Vec<m::Package>, Environment, Options, &'a str)> { let paths: Vec<::std::path::PathBuf> = matches.values_of("path") .into_iter() .flat_map(|it| it) .map(Path::new) .map(ToOwned::to_owned) .collect(); let backend = matches.value_of("backend").ok_or("--backend <backend> is required")?; let out_path = matches.value_of("out").ok_or("--out <dir> is required")?; let out_path = Path::new(&out_path); let package_prefix = matches.value_of("package-prefix").map(ToOwned::to_owned); let modules = matches.values_of("module").into_iter().flat_map(|it| it).map(ToOwned::to_owned).collect(); let id_converter = if let Some(id_converter) = matches.value_of("id-converter") { Some(parse_id_converter(&id_converter)?) } else { None }; let options = Options { out_path: out_path.to_path_buf(), package_prefix: package_prefix, id_converter: id_converter, modules: modules, }; let env = Environment::new(paths); let files: Vec<&Path> = matches.values_of("file") .into_iter() .flat_map(|it| it) .map(Path::new) .collect(); let packages: Vec<m::Package> = matches.values_of("package") .into_iter() .flat_map(|it| it) .map(|s| m::Package::new(s.split(".").map(ToOwned::to_owned).collect())) .collect(); Ok((files, packages, env, options, backend)) } fn do_compile(matches: &ArgMatches) -> Result<Box<backend::Backend>> { let (files, packages, mut env, options, backend) = setup_compiler(matches)?; let mut failed: Vec<backend::errors::Error> = Vec::new(); for file in files { if let Err(e) = env.import_file(file, None) { failed.push(e); } } for package in packages { if let Err(e) = env.import(&package) { failed.push(e); } } let backend = backend::resolve(&backend, options, env); match backend { Err(e) => { failed.push(e); Err(failed.into()) } Ok(backend) => { if failed.is_empty() { Ok(backend) } else { Err(failed.into()) } } } } pub fn compile(matches: &ArgMatches) -> Result<()> { let backend = do_compile(matches)?; backend.process()?; Ok(()) } pub fn verify(matches: &ArgMatches) -> Result<()> { let backend = do_compile(matches)?; let errors = backend.verify()?; if errors.is_empty() { return Ok(()); } Err(ErrorKind::BackendErrors(errors).into()) } pub fn commands<'a, 'b>() -> Vec<App<'a, 'b>> { let mut commands = Vec::new(); commands.push(compile_options("compile").about("Compile .reproto declarations")); commands.push(compile_options("verify").about("Verify .reproto declarations")); commands }