use serde_json as json;
use svd_parser as svd;
use json::Value;
use std::env::args;
use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;
fn main() {
let mut args = args();
let svd_fn = if let (Some(_), Some(arg1), None) = (args.next(), args.next(), args.next()) {
PathBuf::from(arg1)
} else {
println!("Usage: (svd2json) file.svd");
return;
};
let mut svd_xml = String::new();
File::open(&svd_fn)
.expect("Failed to open SVD input file")
.read_to_string(&mut svd_xml)
.expect("Failed to read SVD input file to a String");
let device = svd::parse(&mut svd_xml).expect("Failed to parse the SVD file into Rust structs");
let v: Value =
json::to_value(device).expect("Failed to serialize Rust structs into JSON format");
let mut json_fn = svd_fn.to_path_buf();
json_fn.set_extension("json");
File::create(json_fn)
.expect("Failed to open JSON output file")
.write_all(json::to_string_pretty(&v).unwrap().as_bytes())
.expect("Failed to write to JSON output file");
}