Crate csvenum

Source
Expand description

§csvenum-lib

Documentation for the csvenum-CLI lib

The following is meant for internal reference and the curious minded.

If you are looking to generate enums from a csv, refer to the Usage section of the readme @github

§Structure

  1. A .csv will be read into lines by the [reader] module.

  2. The lines will get separated into values by the TableParser.

  3. The TableParser will then create and validate the EnumTable.

  4. The EnumTable will be passed to the constructor of the EnumModule that handles code generation and CLI options.

The thin CLI calls the generate_configured_enum_from_csv function which handles the above steps.

To construct an enumtable for testing, the code will look something like this:


    use csvenum::TableParser;
 
    let rows: Vec<&str> = vec![
        "TYPES,         &str,       (usize,f64),    bool",
        "MyEnumName,    Property1,  Property2,      Property3",
        "Variant1,      standard,   (0,3.14),       true",
        "Variant2,      medium,     (0,9.82),       false",
    ];
 
    let table_parser = TableParser::from_csv_lines(rows);
    assert!(table_parser.is_ok());
    let enumtable = table_parser.unwrap().to_enumtable().unwrap();
    assert_eq!(enumtable.get_name(), "MyEnumName");
    // row and column indices have moved to only represent the associated values
    let val_var1_prop2 = enumtable.get_value_by_col_row(1,0).unwrap();
    assert_eq!(val_var1_prop2, "(0,3.14)");

§Code generation

To make writing the code-gen easier this crate implements a basic type system, consisting of enums and a unify’ing trait.

The base type being the RType that all other types are nested in.

The corresponding RTypeTrait is implemented for all nested types and provides QoL functions for validation and wrapping values in their respective container.

The code_gen module contains the necessary code for generation. A small example can be found in the code_gen::TextBlock documentation.

Re-exports§

pub use code_gen::EnumModule;

Modules§

code_gen

Structs§

EnumOptions
Contains the options for code generation
EnumTable
Gets created by the table parser
NestedValueParser
Namespace container that holds the function to parse strings with nested values into a vector of values.
TableParser
Parses a Vec<&str> with CSV to an intermediate representation

Enums§

ContainerType
Represents a container, like Vec, Array [T; N] and Tuple(T,R)
NumericType
Represents all numeric types
RType
Rust Type primitive (no value or name attached, except for enum), has functions for:
Reference
Represents a reference & and lifetime <'a>
SpecialType
Special types have vastly different behaviour than the other types and can not be directly inferred from a string representation (except for bool).
StringType
Represents known string types

Traits§

RTypeTrait
Trait to simplify handling types

Functions§

generate_configured_enum_from_csv
Main function that stages file reading, parsing, validation and code gen
read_file_lines
write_lines_to_file