Buttplug sex toy control library
1use std::collections::BTreeMap;
2
3use buttplug_core::util::json::JSONValidator;
4use serde::{Deserialize, Serialize};
5use serde_json::{self, Value};
6
7const VERSION_FILE: &str = "./device-config-v4/version.yaml";
8const OUTPUT_FILE: &str = "./build-config/buttplug-device-config-v4.json";
9const PROTOCOL_DIR: &str = "./device-config-v4/protocols/";
10const SCHEMA_FILE: &str = "./device-config-v4/buttplug-device-config-schema-v4.json";
11
12#[derive(Serialize, Deserialize, Eq, PartialEq)]
13struct VersionFile {
14 version: BuildVersion,
15}
16
17#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Copy)]
18struct BuildVersion {
19 pub major: u32,
20 pub minor: u32,
21}
22
23#[derive(Deserialize, Serialize, Eq, PartialEq)]
24struct JsonOutputFile {
25 version: BuildVersion,
26 protocols: BTreeMap<String, Value>,
27}
28
29fn main() {
30 println!("cargo:rerun-if-changed={}", PROTOCOL_DIR);
31
32 // Open version file
33 let mut version: VersionFile =
34 serde_yaml::from_str(&std::fs::read_to_string(VERSION_FILE).unwrap()).unwrap();
35 // Bump minor version
36 version.version.minor += 1;
37
38 // Compile device config file
39 let mut output = JsonOutputFile {
40 // lol
41 version: version.version,
42 protocols: BTreeMap::new(),
43 };
44
45 for protocol_file in std::fs::read_dir(PROTOCOL_DIR).unwrap() {
46 let f = protocol_file.unwrap();
47 output.protocols.insert(
48 f.file_name()
49 .into_string()
50 .unwrap()
51 .split(".")
52 .next()
53 .unwrap()
54 .to_owned(),
55 serde_yaml::from_str(&std::fs::read_to_string(f.path()).unwrap()).unwrap(),
56 );
57 }
58
59 let json = serde_json::to_string_pretty(&output).unwrap();
60
61 // Validate
62 let validator = JSONValidator::new(&std::fs::read_to_string(SCHEMA_FILE).unwrap());
63 validator.validate(&json).unwrap();
64
65 // See if it's actually different than our last output file
66 if let Ok(true) = std::fs::exists(OUTPUT_FILE) {
67 let old_output: JsonOutputFile =
68 serde_json::from_str(&std::fs::read_to_string(OUTPUT_FILE).unwrap()).unwrap();
69 if old_output.protocols == output.protocols {
70 // No actual changes, break out early, don't save
71 return;
72 }
73 }
74
75 // Save it to the build_config directory
76 std::fs::write(
77 VERSION_FILE,
78 serde_yaml::to_string(&version).unwrap().as_bytes(),
79 )
80 .unwrap();
81 std::fs::write(OUTPUT_FILE, json.as_bytes()).unwrap();
82}