versa 0.17.0

Versa types and utilities for developing Versa client applications in Rust
use std::{fs, path::Path};

use typify::{TypeSpace, TypeSpaceSettings};

const SCHEMA_URL: &'static str = "https://raw.githubusercontent.com/versa-protocol/schema";
const SCHEMA_PATH: &'static str = "data/receipt.schema.json";

fn main() {
  let schema_version = std::env::var("VERSA_SCHEMA_VERSION").expect("A VERSA_SCHEMA_VERSION must be specified in Cargo.toml");
  let schema_url = format!("{}/{}/{}", SCHEMA_URL, schema_version, SCHEMA_PATH);
  // let content = std::fs::read_to_string("../example.json").unwrap();
  // get content from url using reqwest as text
  let content = match reqwest::blocking::get(&schema_url) {
    Ok(res) => match res.text() {
      Ok(text) => text,
      Err(e) => {
        println!("Error fetching schema version {}: {:?}", schema_version, e);
        return;
      }
    },
    Err(e) => {
      println!("Error fetching schema version {}: {:?}", schema_version, e);
      return;
    }
  };

  let schema = serde_json::from_str::<schemars::schema::RootSchema>(&content).unwrap();

  let mut type_space = TypeSpace::new(TypeSpaceSettings::default().with_struct_builder(true));
  type_space.add_root_schema(schema).unwrap();

  let contents = prettyplease::unparse(&syn::parse2::<syn::File>(type_space.to_stream()).unwrap())
    .replace("chrono::naive::NaiveDate", "String");

  let mut out_file = Path::new("src").to_path_buf();
  out_file.push("receipt.rs");
  fs::write(out_file, contents).unwrap();
}