rust-enum-derive 0.4.0

A simple library (and program) for generating rust enums and associated traits from text files.
Documentation
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# rust-enum-derive

rust-enum-derive is a simple program for generating rust enums and associated
traits from text files. To simplify converting C code these text files are
allowed to look like C enums or C #defines.

## Usage
```
Usage: ./rust-enum-derive <options>
A simple program for generating rust enums and associated traits from text files.
Options:
    -i, --input NAME    input file name (stdin if not specified)
        --input_dir NAME
                        input directory to traverse
    -o, --output NAME   output directory to traverse
        --output_dir NAME
                        output file name (stdout if not specified)
        --name NAME     the enum name (Name if not specified)
        --derive DERIVE Which traits to derive. Ex: "Debug, PartialEq"

    -h, --help          print this help menu
        --define        parse C #define input instead of enum
    -a, --all           implement all of the traits (equivalent to --display
                        --fromprimative --fromstr)
        --default       implement the Default trait with the first value
        --display       implement the std::fmt::Display trait
        --fromprimative
                        implement the num::traits::FromPrimitive trait
        --fromstr       implement the std::str::FromStr trait
        --hex           hexadecimal output
        --pretty_fmt    implement pretty_fmt()
```

## Simple examples
All of the following examples produce the same code:

```c
ZERO = 0,
ONE = 1,
TWO = 2,
```

```c
ZERO,
ONE,
TWO,
```

```c
#define ZERO 0
#define ONE 1
#define TWO 2
```

This is the rust enum that will result:

```rust
pub enum Name {
    ZERO = 0,
    ONE = 1,
    TWO = 2,
}
```

rust-enum-derive can take input from standard in, a file, or a directory full of files (and associated TOML configuration). In turn this output can be directed to standard out, a file, or a directory full of files.

In the case of input from a directory structure it will look for TOML files (with filenames ending in ".toml" with the following structure:

```toml
[rust-enum-derive]
name = "Name"
derive = "Debug, PartialEq"
define = false
default = false
display = false
fromprimative = false
fromstr = false
pretty_fmt = false
```

The meaning of these fields matches their meaning on the command-line. You don't need to include any fields if you don't mean to change them from their default (false) value, however you do need to include a `[rust-enum-derive]` table. You will also need to include a file with the same name as your .toml file except ending in ".in". Your directory structure in your --input_dir will be replicated in your --output_dir. For example:

```
$ rust-enum-derive --input_dir ./input/ --output_dir /tmp/output--input_dir ./input/ --output_dir /tmp/output
$ tree input/
input/
├── one.in
├── one.toml
└── sub
    ├── two.in
    └── two.toml

1 directory, 4 files
$ tree input/
input/
├── one.in
├── one.toml
└── sub
    ├── two.in
    └── two.toml

1 directory, 4 files
$ tree /tmp/output/
/tmp/output/
├── one.rs
└── sub
    └── two.rs

1 directory, 2 files
$ tree /tmp/output/
/tmp/output/
├── one.rs
└── sub
    └── two.rs

1 directory, 2 files
```

## Complex example
For example, rust-enum-derive can take the following text file (linux/if.h):

```c
enum net_device_flags {
	IFF_UP				= 1<<0,  /* sysfs */
	IFF_BROADCAST			= 1<<1,  /* __volatile__ */
	IFF_DEBUG			= 1<<2,  /* sysfs */
	IFF_LOOPBACK			= 1<<3,  /* __volatile__ */
	IFF_POINTOPOINT			= 1<<4,  /* __volatile__ */
	IFF_NOTRAILERS			= 1<<5,  /* sysfs */
	IFF_RUNNING			= 1<<6,  /* __volatile__ */
	IFF_NOARP			= 1<<7,  /* sysfs */
	IFF_PROMISC			= 1<<8,  /* sysfs */
	IFF_ALLMULTI			= 1<<9,  /* sysfs */
	IFF_MASTER			= 1<<10, /* __volatile__ */
	IFF_SLAVE			= 1<<11, /* __volatile__ */
	IFF_MULTICAST			= 1<<12, /* sysfs */
	IFF_PORTSEL			= 1<<13, /* sysfs */
	IFF_AUTOMEDIA			= 1<<14, /* sysfs */
	IFF_DYNAMIC			= 1<<15, /* sysfs */
	IFF_LOWER_UP			= 1<<16, /* __volatile__ */
	IFF_DORMANT			= 1<<17, /* __volatile__ */
	IFF_ECHO			= 1<<18, /* __volatile__ */
};
```

and generate the following code:

```rust
pub enum Name {
    IFF_UP = 0x1,
    IFF_BROADCAST = 0x2,
    IFF_DEBUG = 0x4,
    IFF_LOOPBACK = 0x8,
    IFF_POINTOPOINT = 0x10,
    IFF_NOTRAILERS = 0x20,
    IFF_RUNNING = 0x40,
    IFF_NOARP = 0x80,
    IFF_PROMISC = 0x100,
    IFF_ALLMULTI = 0x200,
    IFF_MASTER = 0x400,
    IFF_SLAVE = 0x800,
    IFF_MULTICAST = 0x1000,
    IFF_PORTSEL = 0x2000,
    IFF_AUTOMEDIA = 0x4000,
    IFF_DYNAMIC = 0x8000,
    IFF_LOWER_UP = 0x10000,
    IFF_DORMANT = 0x20000,
    IFF_ECHO = 0x40000,
}
impl ::std::str::FromStr for Name {
    type Err = ();
    #[allow(dead_code)]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "IFF_UP" => Ok(Name::IFF_UP),
            "IFF_BROADCAST" => Ok(Name::IFF_BROADCAST),
            "IFF_DEBUG" => Ok(Name::IFF_DEBUG),
            "IFF_LOOPBACK" => Ok(Name::IFF_LOOPBACK),
            "IFF_POINTOPOINT" => Ok(Name::IFF_POINTOPOINT),
            "IFF_NOTRAILERS" => Ok(Name::IFF_NOTRAILERS),
            "IFF_RUNNING" => Ok(Name::IFF_RUNNING),
            "IFF_NOARP" => Ok(Name::IFF_NOARP),
            "IFF_PROMISC" => Ok(Name::IFF_PROMISC),
            "IFF_ALLMULTI" => Ok(Name::IFF_ALLMULTI),
            "IFF_MASTER" => Ok(Name::IFF_MASTER),
            "IFF_SLAVE" => Ok(Name::IFF_SLAVE),
            "IFF_MULTICAST" => Ok(Name::IFF_MULTICAST),
            "IFF_PORTSEL" => Ok(Name::IFF_PORTSEL),
            "IFF_AUTOMEDIA" => Ok(Name::IFF_AUTOMEDIA),
            "IFF_DYNAMIC" => Ok(Name::IFF_DYNAMIC),
            "IFF_LOWER_UP" => Ok(Name::IFF_LOWER_UP),
            "IFF_DORMANT" => Ok(Name::IFF_DORMANT),
            "IFF_ECHO" => Ok(Name::IFF_ECHO),
            _ => Err( () )
        }
    }
}
impl Default for Name {
    fn default() -> Name {
        Name::IFF_UP
    }
}
impl ::std::fmt::Display for Name {
    #[allow(dead_code)]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        match *self {
            Name::IFF_UP => write!(f, "IFF_UP"),
            Name::IFF_BROADCAST => write!(f, "IFF_BROADCAST"),
            Name::IFF_DEBUG => write!(f, "IFF_DEBUG"),
            Name::IFF_LOOPBACK => write!(f, "IFF_LOOPBACK"),
            Name::IFF_POINTOPOINT => write!(f, "IFF_POINTOPOINT"),
            Name::IFF_NOTRAILERS => write!(f, "IFF_NOTRAILERS"),
            Name::IFF_RUNNING => write!(f, "IFF_RUNNING"),
            Name::IFF_NOARP => write!(f, "IFF_NOARP"),
            Name::IFF_PROMISC => write!(f, "IFF_PROMISC"),
            Name::IFF_ALLMULTI => write!(f, "IFF_ALLMULTI"),
            Name::IFF_MASTER => write!(f, "IFF_MASTER"),
            Name::IFF_SLAVE => write!(f, "IFF_SLAVE"),
            Name::IFF_MULTICAST => write!(f, "IFF_MULTICAST"),
            Name::IFF_PORTSEL => write!(f, "IFF_PORTSEL"),
            Name::IFF_AUTOMEDIA => write!(f, "IFF_AUTOMEDIA"),
            Name::IFF_DYNAMIC => write!(f, "IFF_DYNAMIC"),
            Name::IFF_LOWER_UP => write!(f, "IFF_LOWER_UP"),
            Name::IFF_DORMANT => write!(f, "IFF_DORMANT"),
            Name::IFF_ECHO => write!(f, "IFF_ECHO"),
        }
    }
}
impl ::num::traits::FromPrimitive for Name {
    #[allow(dead_code)]
    fn from_i64(n: i64) -> Option<Self> {
        match n {
            0x1 => Some(Name::IFF_UP),
            0x2 => Some(Name::IFF_BROADCAST),
            0x4 => Some(Name::IFF_DEBUG),
            0x8 => Some(Name::IFF_LOOPBACK),
            0x10 => Some(Name::IFF_POINTOPOINT),
            0x20 => Some(Name::IFF_NOTRAILERS),
            0x40 => Some(Name::IFF_RUNNING),
            0x80 => Some(Name::IFF_NOARP),
            0x100 => Some(Name::IFF_PROMISC),
            0x200 => Some(Name::IFF_ALLMULTI),
            0x400 => Some(Name::IFF_MASTER),
            0x800 => Some(Name::IFF_SLAVE),
            0x1000 => Some(Name::IFF_MULTICAST),
            0x2000 => Some(Name::IFF_PORTSEL),
            0x4000 => Some(Name::IFF_AUTOMEDIA),
            0x8000 => Some(Name::IFF_DYNAMIC),
            0x10000 => Some(Name::IFF_LOWER_UP),
            0x20000 => Some(Name::IFF_DORMANT),
            0x40000 => Some(Name::IFF_ECHO),
            _ => None
        }
    }
    #[allow(dead_code)]
    fn from_u64(n: u64) -> Option<Self> {
        match n {
            0x1 => Some(Name::IFF_UP),
            0x2 => Some(Name::IFF_BROADCAST),
            0x4 => Some(Name::IFF_DEBUG),
            0x8 => Some(Name::IFF_LOOPBACK),
            0x10 => Some(Name::IFF_POINTOPOINT),
            0x20 => Some(Name::IFF_NOTRAILERS),
            0x40 => Some(Name::IFF_RUNNING),
            0x80 => Some(Name::IFF_NOARP),
            0x100 => Some(Name::IFF_PROMISC),
            0x200 => Some(Name::IFF_ALLMULTI),
            0x400 => Some(Name::IFF_MASTER),
            0x800 => Some(Name::IFF_SLAVE),
            0x1000 => Some(Name::IFF_MULTICAST),
            0x2000 => Some(Name::IFF_PORTSEL),
            0x4000 => Some(Name::IFF_AUTOMEDIA),
            0x8000 => Some(Name::IFF_DYNAMIC),
            0x10000 => Some(Name::IFF_LOWER_UP),
            0x20000 => Some(Name::IFF_DORMANT),
            0x40000 => Some(Name::IFF_ECHO),
            _ => None
        }
    }
}
impl Name {
    fn pretty_fmt(f: &mut ::std::fmt::Formatter, flags: u32) -> ::std::fmt::Result {
        let mut shift: u32 = 0;
        let mut result: u32 = 1<<shift;
        let mut found = false;
        while result <= Name::IFF_ECHO as u32 {
            let tmp = result & flags;
            if tmp > 0 {
                if found {
                    try!(write!(f, "|"));
                }
                let flag = Name::from_u32(tmp).unwrap();
                try!(write!(f, "{}", flag));
                found = true;
            }
            shift += 1;
            result = 1<<shift;
        }
        write!(f, "")
    }
}pub enum Name {
    IFF_UP = 0x1,
    IFF_BROADCAST = 0x2,
    IFF_DEBUG = 0x4,
    IFF_LOOPBACK = 0x8,
    IFF_POINTOPOINT = 0x10,
    IFF_NOTRAILERS = 0x20,
    IFF_RUNNING = 0x40,
    IFF_NOARP = 0x80,
    IFF_PROMISC = 0x100,
    IFF_ALLMULTI = 0x200,
    IFF_MASTER = 0x400,
    IFF_SLAVE = 0x800,
    IFF_MULTICAST = 0x1000,
    IFF_PORTSEL = 0x2000,
    IFF_AUTOMEDIA = 0x4000,
    IFF_DYNAMIC = 0x8000,
    IFF_LOWER_UP = 0x10000,
    IFF_DORMANT = 0x20000,
    IFF_ECHO = 0x40000,
}
impl ::std::str::FromStr for Name {
    type Err = ();
    #[allow(dead_code)]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "IFF_UP" => Ok(Name::IFF_UP),
            "IFF_BROADCAST" => Ok(Name::IFF_BROADCAST),
            "IFF_DEBUG" => Ok(Name::IFF_DEBUG),
            "IFF_LOOPBACK" => Ok(Name::IFF_LOOPBACK),
            "IFF_POINTOPOINT" => Ok(Name::IFF_POINTOPOINT),
            "IFF_NOTRAILERS" => Ok(Name::IFF_NOTRAILERS),
            "IFF_RUNNING" => Ok(Name::IFF_RUNNING),
            "IFF_NOARP" => Ok(Name::IFF_NOARP),
            "IFF_PROMISC" => Ok(Name::IFF_PROMISC),
            "IFF_ALLMULTI" => Ok(Name::IFF_ALLMULTI),
            "IFF_MASTER" => Ok(Name::IFF_MASTER),
            "IFF_SLAVE" => Ok(Name::IFF_SLAVE),
            "IFF_MULTICAST" => Ok(Name::IFF_MULTICAST),
            "IFF_PORTSEL" => Ok(Name::IFF_PORTSEL),
            "IFF_AUTOMEDIA" => Ok(Name::IFF_AUTOMEDIA),
            "IFF_DYNAMIC" => Ok(Name::IFF_DYNAMIC),
            "IFF_LOWER_UP" => Ok(Name::IFF_LOWER_UP),
            "IFF_DORMANT" => Ok(Name::IFF_DORMANT),
            "IFF_ECHO" => Ok(Name::IFF_ECHO),
            _ => Err( () )
        }
    }
}
impl Default for Name {
    fn default() -> Name {
        Name::IFF_UP
    }
}
impl ::std::fmt::Display for Name {
    #[allow(dead_code)]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        match *self {
            Name::IFF_UP => write!(f, "IFF_UP"),
            Name::IFF_BROADCAST => write!(f, "IFF_BROADCAST"),
            Name::IFF_DEBUG => write!(f, "IFF_DEBUG"),
            Name::IFF_LOOPBACK => write!(f, "IFF_LOOPBACK"),
            Name::IFF_POINTOPOINT => write!(f, "IFF_POINTOPOINT"),
            Name::IFF_NOTRAILERS => write!(f, "IFF_NOTRAILERS"),
            Name::IFF_RUNNING => write!(f, "IFF_RUNNING"),
            Name::IFF_NOARP => write!(f, "IFF_NOARP"),
            Name::IFF_PROMISC => write!(f, "IFF_PROMISC"),
            Name::IFF_ALLMULTI => write!(f, "IFF_ALLMULTI"),
            Name::IFF_MASTER => write!(f, "IFF_MASTER"),
            Name::IFF_SLAVE => write!(f, "IFF_SLAVE"),
            Name::IFF_MULTICAST => write!(f, "IFF_MULTICAST"),
            Name::IFF_PORTSEL => write!(f, "IFF_PORTSEL"),
            Name::IFF_AUTOMEDIA => write!(f, "IFF_AUTOMEDIA"),
            Name::IFF_DYNAMIC => write!(f, "IFF_DYNAMIC"),
            Name::IFF_LOWER_UP => write!(f, "IFF_LOWER_UP"),
            Name::IFF_DORMANT => write!(f, "IFF_DORMANT"),
            Name::IFF_ECHO => write!(f, "IFF_ECHO"),
        }
    }
}
impl ::num::traits::FromPrimitive for Name {
    #[allow(dead_code)]
    fn from_i64(n: i64) -> Option<Self> {
        match n {
            0x1 => Some(Name::IFF_UP),
            0x2 => Some(Name::IFF_BROADCAST),
            0x4 => Some(Name::IFF_DEBUG),
            0x8 => Some(Name::IFF_LOOPBACK),
            0x10 => Some(Name::IFF_POINTOPOINT),
            0x20 => Some(Name::IFF_NOTRAILERS),
            0x40 => Some(Name::IFF_RUNNING),
            0x80 => Some(Name::IFF_NOARP),
            0x100 => Some(Name::IFF_PROMISC),
            0x200 => Some(Name::IFF_ALLMULTI),
            0x400 => Some(Name::IFF_MASTER),
            0x800 => Some(Name::IFF_SLAVE),
            0x1000 => Some(Name::IFF_MULTICAST),
            0x2000 => Some(Name::IFF_PORTSEL),
            0x4000 => Some(Name::IFF_AUTOMEDIA),
            0x8000 => Some(Name::IFF_DYNAMIC),
            0x10000 => Some(Name::IFF_LOWER_UP),
            0x20000 => Some(Name::IFF_DORMANT),
            0x40000 => Some(Name::IFF_ECHO),
            _ => None
        }
    }
    #[allow(dead_code)]
    fn from_u64(n: u64) -> Option<Self> {
        match n {
            0x1 => Some(Name::IFF_UP),
            0x2 => Some(Name::IFF_BROADCAST),
            0x4 => Some(Name::IFF_DEBUG),
            0x8 => Some(Name::IFF_LOOPBACK),
            0x10 => Some(Name::IFF_POINTOPOINT),
            0x20 => Some(Name::IFF_NOTRAILERS),
            0x40 => Some(Name::IFF_RUNNING),
            0x80 => Some(Name::IFF_NOARP),
            0x100 => Some(Name::IFF_PROMISC),
            0x200 => Some(Name::IFF_ALLMULTI),
            0x400 => Some(Name::IFF_MASTER),
            0x800 => Some(Name::IFF_SLAVE),
            0x1000 => Some(Name::IFF_MULTICAST),
            0x2000 => Some(Name::IFF_PORTSEL),
            0x4000 => Some(Name::IFF_AUTOMEDIA),
            0x8000 => Some(Name::IFF_DYNAMIC),
            0x10000 => Some(Name::IFF_LOWER_UP),
            0x20000 => Some(Name::IFF_DORMANT),
            0x40000 => Some(Name::IFF_ECHO),
            _ => None
        }
    }
}
impl Name {
    fn pretty_fmt(f: &mut ::std::fmt::Formatter, flags: u32) -> ::std::fmt::Result {
        let mut shift: u32 = 0;
        let mut result: u32 = 1<<shift;
        let mut found = false;
        while result <= Name::IFF_ECHO as u32 {
            let tmp = result & flags;
            if tmp > 0 {
                if found {
                    try!(write!(f, "|"));
                }
                let flag = Name::from_u32(tmp).unwrap();
                try!(write!(f, "{}", flag));
                found = true;
            }
            shift += 1;
            result = 1<<shift;
        }
        write!(f, "")
    }
}
```

You can choose to have rust-enum-derive implement all, some, or none of the methods/traits.