Crate code_generator

Source
Expand description

This generator is designed to allow easy reuse of code generation

§Example

This example shows how the same structure can be generated into two different formats.

let code = CodeSet::new(vec![
    Box::new(String::from("#include \"test.h\"")),
    Box::new(String::from("")),
    Box::new(String::from("")),
    Box::new(Function::new(FunctionSignature::new(
            Name::new("MyReturnTypeOne"),
            Name::new("MyFunctionNameOne"),
            vec![
                (Name::new("ParamTypeOne"), Name::new("ParamNameOne")),
                (Name::new("ParamTypeTwo"), Name::new("ParamNameTwo")),
                (Name::new("ParamTypeThree"), Name::new("ParamNameThree")),
            ]
        ),
        CodeSet::new(vec![
            Box::new(IfStatement::new(
                    Name::new_with_type("ParamNameThree", NameType::Member),
                CodeBody::new(vec![
                    Box::new(ForLoop::new(
                        String::from("ParamTypeTwo i = 0"),
                        String::from("i < param_name_two"),
                        String::from("i++"),
                        vec![
                            Box::new(String::from("printf(\"ParamOne: {}\\n\", param_name_one);"))
                        ]
                    ))
                ])
            ))
        ])
    )),
    Box::new(String::from("")),
]);
 
let mut info = CodeGenerationInfo::from_style(CodeStyle::KnR);
info.set_new_line_type(NewLineType::Nl);
assert_eq!(
"#include \"test.h\"


MyReturnTypeOne my_function_name_one(ParamTypeOne param_name_one, ParamTypeTwo param_name_two, ParamTypeThree param_name_three) {
    if (param_name_three) {
        for (ParamTypeTwo i = 0; i < param_name_two; i++) {
            printf(\"ParamOne: {}\\n\", param_name_one);
        }
    }
}
", format!("{}", code.display(info)));

let mut info = CodeGenerationInfo::from_style(CodeStyle::Allman);
info.set_new_line_type(NewLineType::Nl);
assert_eq!(
"#include \"test.h\"


MyReturnTypeOne my_function_name_one(ParamTypeOne param_name_one, ParamTypeTwo param_name_two, ParamTypeThree param_name_three)
{
    if (param_name_three)
    {
        for (ParamTypeTwo i = 0; i < param_name_two; i++)
        {
            printf(\"ParamOne: {}\\n\", param_name_one);
        }
    }
}
", format!("{}", code.display(info)));


Because the output format is not dependent on the input data structure, it is very easy to make code generation modular. Any pieces of generated code that share structure, can share a generator.

Macros§

join_code
Creates a JoinedCode generator

Structs§

CaseTypes
CodeBody
CodeGenerationInfo
CodeSet
ConstDefine
DisplayHandler
Enum
ForLoop
Function
FunctionCall
FunctionDeclaration
FunctionSignature
HeaderFile
HeaderPlusBody
IfStatement
Include
Indentation
Indentation generator
JoinedCode
The JoinedCode struct joins multiple sections of code with no further formatting, or configuration done outside, inside, or between units.
Name
NewLine
The NewLine struct allows the generation info to decide the new line format
SeparatedCode
Struct
TypeDef
WhileStatement

Enums§

CaseType
CodeStyle
GeneratorContext
IndentationStyle
IndentationType
NameType
The name type allows the struct to use the generation info to decide the case type of the name based on the type of name.
NewLineType

Traits§

CodeGenerate
DisplayExt