Skip to content

Instantly share code, notes, and snippets.

@skylerto
Created July 14, 2017 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skylerto/1a3f7cfb7bf57dcebd2fa3db1ff72cee to your computer and use it in GitHub Desktop.
Save skylerto/1a3f7cfb7bf57dcebd2fa3db1ff72cee to your computer and use it in GitHub Desktop.
extern crate serde_json;
extern crate handlebars;
#[macro_use]
extern crate maplit;
use std::path::Path;
use handlebars::Handlebars;
use serde_json::Value;
use std::env;
use std::fs::File;
use std::io::prelude::*;
fn main() {
let args: Vec<String> = env::args().collect();
let filename = &args[1];
println!("Loading JSON file {}", filename);
let mut f = File::open(filename).expect("file not found");
let mut contents = String::new();
f.read_to_string(&mut contents).expect("something went wrong reading the file");
let v: Value = serde_json::from_str(&contents).unwrap();
let mut handlebars = Handlebars::new();
handlebars
.register_template_file("inspecting", &Path::new("./src/partials/inspecting.hbs"))
.ok()
.unwrap();
let data0 =
btreemap! {
"data".to_string() => v.as_object(),
};
println!("Generating Inspecing HTML Page");
let data = handlebars.render("inspecting", &data0).unwrap_or_else(
|e| format!("Dammit {}", e),
);
let path = Path::new("inspecting.html");
let display = path.display();
// Open a file in write-only mode, returns `io::Result<File>`
let mut file = match File::create(&path) {
Err(why) => panic!("couldn't create {}: {}",
display,
why),
Ok(file) => file,
};
// Write the `LOREM_IPSUM` string to `file`, returns `io::Result<()>`
match file.write_all(
data.as_bytes()) {
Err(why) => {
panic!("couldn't write to {}: {}", display,
why)
},
Ok(_) => println!("successfully wrote to {}", display),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment