Debian Codegraph @

build.rs

raw symbols SCIP
1 use std::env;
2 use std::fs;
3 use std::io::prelude::*;
4 use std::io::BufReader;
5 use std::iter::FromIterator;
6 use std::path::Path;
7 use std::string::String;
8
9 fn main() -> Result<(), Box<dyn std::error::Error>> {
10 let out_dir = env::var_os("OUT_DIR").unwrap();
11 let field_def_file = "src/audit-specs/fields/field-dictionary.csv";
12 let msgtype_def_file = "src/audit-specs/messages/message-dictionary.csv";
13 let const_file = Path::new(&out_dir).join("const.rs");
14 let msgtype_file = Path::new(&out_dir).join("message_type_impl.rs");
15
16 let constants: Vec<(String, String)> = BufReader::new(fs::File::open(msgtype_def_file)?)
17 .lines()
18 .skip(1) // skip over header
19 .map(|line| {
20 line.unwrap()
21 .split(',')
22 .map(|x| x.to_string())
23 .collect::<Vec<_>>()
24 })
25 .map(|fields| {
26 (
27 fields[0].strip_prefix("AUDIT_").unwrap().to_string(),
28 fields[1].clone(),
29 )
30 })
31 .collect();
32
33 let fields: Vec<(String, String)> = BufReader::new(fs::File::open(field_def_file)?)
34 .lines()
35 .skip(3) // skip over heder and regex describing a* mess
36 .map(|line| {
37 line.unwrap()
38 .split(',')
39 .map(|x| x.to_string())
40 .collect::<Vec<_>>()
41 })
42 .map(|fields| (fields[0].clone(), fields[1].clone()))
43 .collect();
44
45 let mut template = Vec::new();
46 fs::File::open("src/const.rs.in")?.read_to_end(&mut template)?;
47 let template = String::from_utf8(template)?;
48
49 let buf = template
50 .replace(
51 "/* @EVENT_CONST@ */",
52 &String::from_iter(
53 constants
54 .iter()
55 .map(|(name, value)| format!(r#"("{name}", {value}), "#)),
56 ),
57 )
58 .replace(
59 "/* @FIELD_TYPES@ */",
60 &String::from_iter(
61 fields
62 .iter()
63 .filter(|(_, typ)| typ == "encoded" || typ.starts_with("numeric"))
64 .map(|(name, typ)| match typ.as_str() {
65 "numeric hexadecimal" => format!(r#"("{name}", FieldType::NumericHex),"#),
66 "numeric decimal" => format!(r#"("{name}", FieldType::NumericDec),"#),
67 "numeric octal" => format!(r#"("{name}", FieldType::NumericOct),"#),
68 "numeric" => format!(r#"("{name}", FieldType::Numeric),"#),
69 "encoded" => format!(r#"("{name}", FieldType::Encoded),"#),
70 _ => format!(r#"("{name}", FieldType::Invalid),"#),
71 }),
72 ),
73 )
74 .into_bytes();
75 fs::write(const_file, buf)?;
76
77 let mut template = Vec::new();
78 fs::File::open("src/message_type_impl.rs.in")?.read_to_end(&mut template)?;
79 let template = String::from_utf8(template)?;
80 let buf = template.replace(
81 "/* @MSG_TYPE_CONST@ */",
82 &String::from_iter(
83 constants
84 .iter()
85 .map(|(name, value)| format!("pub const {name}: Self = Self({value});\n",)),
86 ),
87 );
88
89 fs::write(msgtype_file, buf)?;
90
91 println!("cargo:rerun-if-changed=build.rs");
92 println!("cargo:rerun-if-changed=const.rs.in");
93 println!("cargo:rerun-if-changed={msgtype_def_file}");
94 println!("cargo:rerun-if-changed={field_def_file}");
95
96 Ok(())
97 }