preserves/implementations/rust/preserves-schema/src/bin/preserves-schema-rs.rs

65 lines
1.8 KiB
Rust

//! Command-line Rust code generator for Preserves Schema. See the documentation at
//! <https://preserves.dev/doc/preserves-schema-rs.html>.
use std::io::Error;
use std::io::ErrorKind;
use std::path::PathBuf;
use structopt::StructOpt;
use preserves_schema::compiler::CodeCollector;
use preserves_schema::compiler::CompilerConfig;
use preserves_schema::compiler::ExternalModule;
use preserves_schema::compiler::compile;
use preserves_schema::compiler::expand_inputs;
#[derive(Clone, StructOpt, Debug)]
struct CommandLine {
#[structopt(short, long)]
output_dir: PathBuf,
#[structopt(short, long)]
prefix: String,
#[structopt(long)]
support_crate: Option<String>,
#[structopt(long)]
module: Vec<String>,
#[structopt(long)]
xref: Vec<String>,
#[structopt(long)]
rustfmt_skip: bool,
input_glob: Vec<String>,
}
fn main() -> Result<(), Error> {
let args = CommandLine::from_args();
let mut config = CompilerConfig::new(args.prefix);
for alias in args.module {
let (modulepath_str, target) = {
let pieces: Vec<&str> = alias.split('=').collect();
if pieces.len() != 2 {
return Err(Error::new(
ErrorKind::InvalidData,
format!("Invalid module alias: {:?}", alias),
));
}
(pieces[0], pieces[1])
};
let modulepath: Vec<String> = modulepath_str.split('.').map(str::to_owned).collect();
config.add_external_module(ExternalModule::new(modulepath, target));
}
if let Some(c) = args.support_crate {
config.support_crate = c;
}
config.rustfmt_skip = args.rustfmt_skip;
config.load_schemas_and_bundles(
&expand_inputs(&args.input_glob)?,
&expand_inputs(&args.xref)?,
)?;
compile(&config, &mut CodeCollector::files(args.output_dir))
}