-
Notifications
You must be signed in to change notification settings - Fork 3
/
generator.py
50 lines (46 loc) · 1.68 KB
/
generator.py
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
def main() -> None:
with open(file='./ntstatus.txt', mode='r', encoding='utf-8') as input_file:
with open(file='./src/ntstatus.rs', mode='w+', encoding='utf-8') as output_file:
with open(file='./src/test.rs', mode='w+', encoding='utf-8') as test_file:
output_file.write("""use num_enum::TryFromPrimitive;\n
#[cfg(not(feature = "std"))]
use core::fmt::Debug;
#[derive(Debug, TryFromPrimitive)]
#[repr(u32)]
#[allow(non_camel_case_types)]
pub enum NtStatus {""")
lines = input_file.readlines()
for line in lines:
line = line.replace('\n', '')
(symbol, value) = line.split(':')
if symbol.startswith('//'):
continue
output_file.write(f'\n {symbol} = {value},')
output_file.write('\n}')
output_file.write("""\n\nimpl NtStatus {
pub fn from_u32(value: u32) -> Option<&'static str> {
match NtStatus::try_from(value) {""")
for line in lines:
line = line.replace('\n', '')
(symbol, value) = line.split(':')
if symbol.startswith('//'):
continue
output_file.write(f'\n Ok(NtStatus::{symbol}) => Some("{symbol}"),')
output_file.write("""\n Err(_) => None,
}
}
}
""")
test_file.write("""#![cfg(test)]\n
use crate::ntstatus::NtStatus;\n
#[test]
fn test_from_u32() {""")
for line in lines:
line = line.replace('\n', '')
(symbol, value) = line.split(':')
if symbol.startswith('//'):
continue
test_file.write(f'\n assert_eq!(NtStatus::from_u32({value}), Some("{symbol}"));')
test_file.write('}\n')
if __name__ == '__main__':
main()