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§
- Case
Types - Code
Body - Code
Generation Info - CodeSet
- Const
Define - Display
Handler - Enum
- ForLoop
- Function
- Function
Call - Function
Declaration - Function
Signature - Header
File - Header
Plus Body - IfStatement
- Include
- Indentation
- Indentation generator
- Joined
Code - 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
- Separated
Code - Struct
- TypeDef
- While
Statement
Enums§
- Case
Type - Code
Style - Generator
Context - Indentation
Style - Indentation
Type - Name
Type - 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.
- NewLine
Type