Crates.io | csv2struct |
lib.rs | csv2struct |
version | 0.1.0 |
source | src |
created_at | 2019-01-26 03:05:45.344606 |
updated_at | 2019-01-26 03:05:45.344606 |
description | Generate Rust struct definitions from CSV |
homepage | |
repository | https://github.com/KyussCaesar/csv2struct |
max_upload_size | |
id | 110720 |
size | 11,021 |
csv2struct
Generates struct definitions from CSV using some very basic rules.
$ cat test.csv
foo,bar,baz,qux
1,2,3,green
4.4,5,6,red
7.2,,8,blue
$ cat test.csv | csv2struct
#[derive(Debug, Clone, Copy, Eq)]
pub struct Record {
pub foo: f32,
pub bar: Option<i32>,
pub baz: i32,
pub qux: Qux,
}
#[derive(Debug, Clone, Copy, Eq)]
pub enum Qux {
Green,
Red,
Blue,
}
There are two sets of rules at play. First, we apply the following set of rules to each value in each column and record the results.
if value == "" => Empty
if let Some(_) = value.parse::<i32>() => Integer
if let Some(_) = value.parse::<f32>() => Real
else => Factor(value)
Next, we apply the following rules to the results for each column:
Option
.Finally, we generate a struct definition with one field for each column. For factors, we generate an enum as well.