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

67 lines
1.9 KiB
Rust
Raw Normal View History

2021-06-28 14:35:45 +00:00
use std::path::PathBuf;
use std::fs::File;
use preserves::value::packed::PackedReader;
use preserves::value::{NestedValue, Reader};
use structopt::StructOpt;
use glob::glob;
use preserves_schema::compiler::{CompilerConfig, compile};
#[derive(Clone, StructOpt, Debug)]
struct CommandLine {
#[structopt(short, long)]
output_dir: PathBuf,
#[structopt(long)]
prefix: Option<String>,
input_glob: Vec<String>,
}
fn inputs(globs: &Vec<String>) -> Vec<PathBuf> {
let mut result = Vec::new();
for g in globs.iter() {
match glob(g) {
Ok(paths) =>
for p in paths {
match p {
Ok(s) => result.push(s),
Err(e) => println!("warning: {:?}", e),
}
}
Err(e) => println!("warning: {:?}", e),
}
}
result
}
fn main() -> Result<(), std::io::Error> {
let args = CommandLine::from_args();
let prefix = match &args.prefix {
Some(s) => s.split(".").map(str::to_string).collect(),
None => vec![],
};
let mut config = CompilerConfig::new(args.output_dir);
for i in inputs(&args.input_glob) {
let mut f = File::open(&i)?;
let mut reader = PackedReader::decode_read(&mut f);
let schema = reader.demand_next(false)?;
if let Some(s) = schema.value().as_simple_record("schema", Some(1)) {
config.bundle.insert(prefix.clone(), s[0].clone());
} else if let Some(b) = schema.value().as_simple_record("bundle", Some(1)) {
for (k, v) in b[0].value().to_dictionary()? {
let mut name = prefix.clone();
for p in k.value().to_sequence()? {
name.push(p.value().to_string()?.to_owned());
}
config.bundle.insert(name, v.clone());
}
}
}
compile(&config)
}