use std::collections::BTreeMap;
use buttplug_core::util::json::JSONValidator;
use serde::{Deserialize, Serialize};
use serde_json::{self, Value};
const VERSION_FILE: &str = "./device-config/version.yaml";
const OUTPUT_FILE: &str = "./build-config/buttplug-device-config-v5.json";
const PROTOCOL_DIR: &str = "./device-config/protocols/";
const SCHEMA_FILE: &str = "./device-config/buttplug-device-config-schema-v5.json";
#[derive(Serialize, Deserialize, Eq, PartialEq)]
struct VersionFile {
version: BuildVersion,
}
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Copy)]
struct BuildVersion {
pub major: u32,
pub minor: u32,
}
#[derive(Deserialize, Serialize, Eq, PartialEq)]
struct JsonOutputFile {
version: BuildVersion,
protocols: BTreeMap<String, Value>,
}
fn main() {
println!("cargo:rerun-if-changed={}", PROTOCOL_DIR);
let mut version: VersionFile =
serde_yaml::from_str(&std::fs::read_to_string(VERSION_FILE).unwrap()).unwrap();
version.version.minor += 1;
let mut output = JsonOutputFile {
version: version.version,
protocols: BTreeMap::new(),
};
for protocol_file in std::fs::read_dir(PROTOCOL_DIR).unwrap() {
let f = protocol_file.unwrap();
output.protocols.insert(
f.file_name()
.into_string()
.unwrap()
.split(".")
.next()
.unwrap()
.to_owned(),
serde_yaml::from_str(&std::fs::read_to_string(f.path()).unwrap()).unwrap(),
);
}
let json = serde_json::to_string_pretty(&output).unwrap();
let validator = JSONValidator::new(&std::fs::read_to_string(SCHEMA_FILE).unwrap());
validator.validate(&json).unwrap();
if let Ok(true) = std::fs::exists(OUTPUT_FILE) {
let old_output: JsonOutputFile =
serde_json::from_str(&std::fs::read_to_string(OUTPUT_FILE).unwrap()).unwrap();
if old_output.protocols == output.protocols {
return;
}
}
std::fs::write(
VERSION_FILE,
serde_yaml::to_string(&version).unwrap().as_bytes(),
)
.unwrap();
std::fs::write(OUTPUT_FILE, json.as_bytes()).unwrap();
}