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

65 lines
1.8 KiB
Rust
Raw Normal View History

//! Command-line Rust code generator for Preserves Schema. See the documentation at
//! <https://preserves.dev/doc/preserves-schema-rs.html>.
2021-06-28 20:25:41 +00:00
use std::io::Error;
use std::io::ErrorKind;
2021-06-28 20:25:41 +00:00
use std::path::PathBuf;
2021-06-28 14:35:45 +00:00
use structopt::StructOpt;
2023-11-18 15:13:56 +00:00
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;
2021-06-28 14:35:45 +00:00
#[derive(Clone, StructOpt, Debug)]
struct CommandLine {
#[structopt(short, long)]
output_dir: PathBuf,
#[structopt(short, long)]
2021-06-29 20:32:35 +00:00
prefix: String,
2021-06-28 14:35:45 +00:00
2021-06-29 20:32:35 +00:00
#[structopt(long)]
support_crate: Option<String>,
2021-06-28 14:35:45 +00:00
#[structopt(long)]
module: Vec<String>,
#[structopt(long)]
xref: Vec<String>,
2023-07-21 15:57:15 +00:00
#[structopt(long)]
rustfmt_skip: bool,
2021-06-29 20:32:35 +00:00
input_glob: Vec<String>,
2021-06-28 14:35:45 +00:00
}
2021-06-28 20:25:41 +00:00
fn main() -> Result<(), Error> {
2021-06-28 14:35:45 +00:00
let args = CommandLine::from_args();
2023-11-18 15:13:56 +00:00
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 {
2023-07-21 16:15:30 +00:00
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));
}
2021-06-29 20:32:35 +00:00
if let Some(c) = args.support_crate {
config.support_crate = c;
2021-06-28 14:35:45 +00:00
}
2023-07-21 15:57:15 +00:00
config.rustfmt_skip = args.rustfmt_skip;
config.load_schemas_and_bundles(
&expand_inputs(&args.input_glob)?,
2023-07-21 16:15:30 +00:00
&expand_inputs(&args.xref)?,
)?;
2023-11-18 15:13:56 +00:00
compile(&config, &mut CodeCollector::files(args.output_dir))
2021-06-28 14:35:45 +00:00
}