]> git.proxmox.com Git - rustc.git/blame - src/vendor/magenta/tools/gen_status.py
New upstream version 1.22.1+dfsg1
[rustc.git] / src / vendor / magenta / tools / gen_status.py
CommitLineData
ea8adc8c
XL
1#!/usr/bin/env python
2
3# Copyright 2016 The Fuchsia Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# A tool for autogenerating the mapping between Status and mx_status_t
8# Usage: python gen_status.py magenta/system/public/magenta/errors.h {sys,enum,match}
9import re
10import sys
11
12status_re = re.compile('#define\s+(MX_\w+)\s+\((\-?\d+)\)$')
13
14def parse(in_filename):
15 result = []
16 for line in file(in_filename):
17 m = status_re.match(line)
18 if m:
19 result.append((m.group(1), int(m.group(2))))
20 return result
21
22def to_snake_case(name):
23 result = []
24 for element in name.split('_'):
25 result.append(element[0] + element[1:].lower())
26 return ''.join(result)
27
28def out(style, l):
29 print('// Auto-generated using tools/gen_status.py')
30 longest = max(len(name) for (name, num) in l)
31 if style == 'sys':
32 for (name, num) in l:
33 print('pub const %s : mx_status_t = %d;' % (name.ljust(longest), num))
34 if style == 'enum':
35 print('pub enum Status {')
36 for (name, num) in l:
37 print(' %s = %d,' % (to_snake_case(name[3:]), num))
38 print('');
39 print(' /// Any mx_status_t not in the set above will map to the following:')
40 print(' UnknownOther = -32768,')
41 print('}')
42 if style == 'match':
43 for (name, num) in l:
44 print(' sys::%s => Status::%s,' % (name, to_snake_case(name[3:])))
45 print(' _ => Status::UnknownOther,')
46
47
48l = parse(sys.argv[1])
49out(sys.argv[2], l)