forked from fcoury/razermacos-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
157 lines (141 loc) · 4.42 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use serde::{de::Error, Deserialize, Deserializer, Serialize};
use std::{env, fs, path::PathBuf};
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
enum RazerDeviceType {
#[serde(rename = "mouse")]
Mouse,
#[serde(rename = "mousedock")]
MouseDock,
#[serde(rename = "mousemat")]
MouseMat,
#[serde(rename = "keyboard")]
Keyboard,
#[serde(rename = "accessory")]
Accessory,
#[serde(rename = "headphone")]
Headphone,
#[serde(rename = "egpu")]
EGpu,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct RazerDevice {
name: String,
#[serde(deserialize_with = "from_hex")]
product_id: u16,
main_type: RazerDeviceType,
image: String,
features: Option<Vec<String>>,
features_missing: Option<Vec<String>>,
features_config: Option<serde_json::Value>,
}
fn from_hex<'de, D>(deserializer: D) -> Result<u16, D::Error>
where
D: Deserializer<'de>,
{
let s: &str = Deserialize::deserialize(deserializer)?;
// do better hex decoding than this
u16::from_str_radix(&s[2..], 16).map_err(D::Error::custom)
}
fn main() {
let razer_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("razer_devices.rs");
let files = fs::read_dir("src/devices").unwrap();
let mut contents = format!(
r#"
use serde::{{Deserialize, Serialize}};
use lazy_static::lazy_static;
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
pub enum RazerDeviceType {{
#[serde(rename = "mouse")]
Mouse,
#[serde(rename = "mousedock")]
MouseDock,
#[serde(rename = "mousemat")]
MouseMat,
#[serde(rename = "keyboard")]
Keyboard,
#[serde(rename = "accessory")]
Accessory,
#[serde(rename = "headphone")]
Headphone,
#[serde(rename = "egpu")]
EGpu,
}}
pub struct RazerDeviceDescriptor {{
pub name: String,
pub product_id: u16,
pub main_type: RazerDeviceType,
pub image: String,
pub features: Option<Vec<String>>,
pub features_missing: Option<Vec<String>>,
pub features_config: Option<serde_json::Value>,
}}
lazy_static! {{
pub static ref RAZER_DEVICES: [RazerDeviceDescriptor; {}] = [
"#,
files.count(),
);
let files = fs::read_dir("src/devices").unwrap();
for file in files {
let file = file.unwrap();
let path = file.path();
let data = fs::read_to_string(path).unwrap();
let res = serde_json::from_str::<RazerDevice>(&data).unwrap();
let features = match res.features {
Some(features) => format!(
"Some(vec![{}])",
features
.iter()
.map(|feature| format!(r#""{}".to_string()"#, feature))
.collect::<Vec<_>>()
.join(", ")
),
None => "None".to_string(),
};
let features_missing = match res.features_missing {
Some(features_missing) => format!(
"Some(vec![{}])",
features_missing
.iter()
.map(|feature| format!(r#""{}".to_string()"#, feature))
.collect::<Vec<_>>()
.join(", ")
),
None => "None".to_string(),
};
let features_config = match res.features_config {
Some(features_config) => format!("Some(serde_json::json!({}))", features_config),
None => "None".to_string(),
};
contents.push_str(&format!(
r#"
RazerDeviceDescriptor {{
name: "{}".to_string(),
product_id: {},
main_type: RazerDeviceType::Mouse,
image: "{}".to_string(),
features: {},
features_missing: {},
features_config: {},
}},
"#,
res.name, res.product_id, res.image, features, features_missing, features_config,
));
}
contents.push_str(
r#"
];
}
pub fn find_descriptor<'a>(product_id: u16) -> Option<&'a RazerDeviceDescriptor> {
if let Some(device) = RAZER_DEVICES
.iter()
.find(|device| device.product_id == product_id)
{
return Some(device.clone());
}
None
}
"#,
);
fs::write(razer_path, contents).unwrap();
}