forked from shimunn/ctap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
44 lines (42 loc) · 1.26 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
use csv::{Reader, StringRecord};
use serde_derive::Deserialize;
use std::env;
use std::fs::File;
use std::io::{Result, Write};
use std::iter::FromIterator;
use std::string::String;
fn main() {
parse_error_codes().expect("Failed to parse error codes")
}
fn parse_error_codes() -> Result<()> {
println!("cargo:rerun-if-changed=ctap_error_codes.csv");
let mut out_file = File::create(format!(
"{}/ctap_error_codes.rs",
env::var("OUT_DIR").unwrap()
))?;
out_file.write_all(b"static CTAP_ERROR_CODES: &[(usize, &str, &str)] = &[")?;
let mut rdr = Reader::from_path("ctap_error_codes.csv")?;
rdr.set_headers(StringRecord::from_iter(&["code", "name", "desc"]));
#[derive(Debug, Deserialize)]
struct ErrorCode {
code: String,
name: String,
desc: String,
}
for result in rdr.deserialize() {
let record: ErrorCode = result.unwrap();
if &record.code == "Code" {
continue;
}
out_file.write_all(
format!(
"({}, \"{}\", \"{}\"),\n",
i64::from_str_radix(&record.code[2..], 16).unwrap(),
record.name,
record.desc
)
.as_bytes(),
)?;
}
out_file.write_all(b"];")
}