| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use std::{env, fs, io::Write, path::Path}; |
| 5 |
|
| 6 |
struct AlertCsvEntry { |
| 7 |
value: u8, |
| 8 |
description: String, |
| 9 |
} |
| 10 |
|
| 11 |
impl AlertCsvEntry { |
| 12 |
fn from_csv_line(line: &str) -> Option<Self> { |
| 13 |
let mut fields = line.split(','); |
| 14 |
let value = fields.next().unwrap(); |
| 15 |
let description = fields.next().unwrap(); |
| 16 |
if description == "Unassigned" || description.contains("RESERVED") { |
| 17 |
return None; |
| 18 |
} |
| 19 |
|
| 20 |
let value: u8 = value.parse().ok()?; |
| 21 |
|
| 22 |
Some(Self { |
| 23 |
value, |
| 24 |
description: description.to_owned(), |
| 25 |
}) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
fn parse_csv() -> Vec<AlertCsvEntry> { |
| 30 |
let csv = include_str!("resources/tls-parameters-6.csv"); |
| 31 |
csv.lines() |
| 32 |
.skip(1) |
| 33 |
.filter_map(AlertCsvEntry::from_csv_line) |
| 34 |
.collect() |
| 35 |
} |
| 36 |
|
| 37 |
fn main() { |
| 38 |
let out_dir = env::var("OUT_DIR").unwrap(); |
| 39 |
let dest_path = Path::new(&out_dir).join("alerts_generated.rs"); |
| 40 |
|
| 41 |
let entries = parse_csv(); |
| 42 |
|
| 43 |
let mut constants = String::new(); |
| 44 |
let mut match_arms = String::new(); |
| 45 |
let mut array_entries = String::new(); |
| 46 |
|
| 47 |
for entry in &entries { |
| 48 |
constants.push_str(&format!( |
| 49 |
"pub const {}: Alert = Alert({});\n", |
| 50 |
entry.description.to_uppercase(), |
| 51 |
entry.value |
| 52 |
)); |
| 53 |
match_arms.push_str(&format!( |
| 54 |
"{} => Some(\"{}\"),\n", |
| 55 |
entry.value, entry.description |
| 56 |
)); |
| 57 |
array_entries.push_str(&format!( |
| 58 |
"Alert::{},", |
| 59 |
entry.description.to_ascii_uppercase() |
| 60 |
)); |
| 61 |
} |
| 62 |
|
| 63 |
let count = entries.len(); |
| 64 |
let generated = format!( |
| 65 |
"\ |
| 66 |
// Auto-generated from tls-parameters-6.csv — do not edit. |
| 67 |
|
| 68 |
impl Alert {{ |
| 69 |
{constants} |
| 70 |
|
| 71 |
pub const DEFINED_ALERTS: [Alert; {count}] = [{array_entries}]; |
| 72 |
|
| 73 |
pub fn get_description(&self) -> Option<&'static str> {{ |
| 74 |
match self.0 {{ |
| 75 |
{match_arms} |
| 76 |
_ => None, |
| 77 |
}} |
| 78 |
}} |
| 79 |
}} |
| 80 |
|
| 81 |
" |
| 82 |
); |
| 83 |
|
| 84 |
fs::File::create(&dest_path) |
| 85 |
.unwrap() |
| 86 |
.write_all(generated.as_bytes()) |
| 87 |
.unwrap(); |
| 88 |
|
| 89 |
println!("cargo::rerun-if-changed=resources/tls-parameters-6.csv"); |
| 90 |
} |