7#include "openFileStream.h"
17 bool hasDefaultValue =
false;
18 for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end() && !hasDefaultValue; ++it){
19 hasDefaultValue |= it->getDefaultValue() !=
"";
21 return hasDefaultValue;
30 PString name(classConfig.
getName());
31 const std::vector<PClassAttribute> & vecAttr(classConfig.
getListAttribute());
32 if(vecAttr.size() == 0LU){
33 fs <<
"//Error : missing enum values as attribute for '" << name <<
"'" << std::endl;
37 fs <<
"#[derive(Debug, Clone, Copy, PartialEq, Eq)]" << std::endl;
38 fs <<
"pub enum " << name <<
" {" << std::endl;
41 for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end(); ++it){
42 if(it->getDocumentation() !=
""){fs <<
"\t" << it->getDocumentation() << std::endl;}
43 fs <<
"\t" << it->getName().toLower().firstToUpper();
44 if(isDefaultCounterActivated){
46 }
else if(it->getDefaultValue() !=
""){
47 fs <<
" = " << it->getDefaultValue();
49 fs <<
"," << std::endl;
52 fs <<
"}" << std::endl << std::endl;
54 fs <<
"impl Default for " << name <<
" {" << std::endl;
55 fs <<
"\tfn default() -> Self {" << std::endl;
56 fs <<
"\t\t" << name <<
"::" << vecAttr.front().getName().toLower().firstToUpper() << std::endl;
57 fs <<
"\t}" << std::endl;
58 fs <<
"}" << std::endl << std::endl;
70 PString name(classConfig.
getName());
71 const std::vector<PClassAttribute> & vecAttr(classConfig.
getListAttribute());
74 fs <<
"#[derive(Debug, Clone, PartialEq)]" << std::endl;
75 fs <<
"pub struct " << name <<
" {" << std::endl;
76 for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end(); ++it){
77 if(it->getDocumentation() !=
""){fs <<
"\t" << it->getDocumentation() << std::endl;}
78 fs <<
"\t" << it->getName().toSnakeCase() <<
": " <<
getRustType(it->getType()) <<
"," << std::endl;
80 fs <<
"}" << std::endl << std::endl;
82 fs <<
"impl Default for " << name <<
" {" << std::endl;
83 fs <<
"\tfn default() -> Self {" << std::endl;
84 fs <<
"\t\t" << name <<
" {" << std::endl;
85 for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end(); ++it){
88 fs <<
"\t\t}" << std::endl;
89 fs <<
"\t}" << std::endl;
90 fs <<
"}" << std::endl << std::endl;
92 fs <<
"impl " << name <<
" {" << std::endl;
93 fs <<
"\t///Creates a new " << name <<
" with default values" << std::endl;
94 fs <<
"\tpub fn new() -> Self { Self::default() }" << std::endl << std::endl;
96 fs <<
"}" << std::endl << std::endl;
109 if(baseFileName ==
""){
return false;}
110 PPath rsFile = outputSourceDir / PPath(baseFileName.toSnakeCase() + PString(
".rs"));
112 if(!openFileStream(fs, rsFile)){
return false;}
117 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
138 PString moduleName(baseFileName.toSnakeCase());
139 PPath testFile = outputTestDir / PPath(moduleName + PString(
"_test.rs"));
141 if(!openFileStream(fs, testFile)){
return false;}
144 fs <<
"use " << crateName <<
"::" << moduleName <<
"::{";
145 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
146 if(it->getIsEnum()){
continue;}
147 fs << comma << it->getName();
150 fs <<
"};" << std::endl << std::endl;
151 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
170 PPath baseFileName = config.
getFileName().getFileName().eraseExtension();
Class to describe a basic class.
const PString & getClassDocumentation() const
Returns the class documentation.
const std::vector< PClassAttribute > & getListAttribute() const
Returns the list of attributes of the class.
const PString & getName() const
Returns the class name.
Class to describe a basic class.
const PVecClassConfig & getVecClassConfig() const
Get the vector of all config class of the current pdata file.
const PPath & getFileName() const
Get the file name of the current PDataConfig.
Manager of the Rust trait backends.
void classExtraDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of extra declarations.
void headerExtraInclude(std::ofstream &fs, const GeneratorMode &mode) const
Add extra include on the header.
void testFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of test function.
void implMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of the impl block methods.
bool generator_rust_enum_file(std::ofstream &fs, const PClassConfig &classConfig)
Write the Rust enum matching the given PClassConfig.
bool generator_rust_class_full(const PRustTraitBackendManager &manager, const ProjectParam &projectParam)
Generate the full Rust sources (and related integration tests) from configuration.
bool generator_rust_class(const PRustTraitBackendManager &manager, const std::vector< PClassConfig > &vecClassConfig, const PPath &outputSourceDir, const PPath &baseFileName, const GeneratorMode &mode)
Write the Rust module (one file per pdata file) containing every struct/enum it defines.
bool rust_checkDefaultValueExist(const std::vector< PClassAttribute > &vecAttr)
Check if the vector of PClassAttribute has default value.
bool generator_rust_class_test(const PRustTraitBackendManager &manager, const PPath &outputTestDir, const PString &crateName, const PPath &baseFileName, const std::vector< PClassConfig > &vecClassConfig, const GeneratorMode &mode)
Write the Rust integration test file (one file per pdata file) exercising every non-enum class it def...
bool generator_rust_class_file(const PRustTraitBackendManager &manager, std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
Write the Rust struct, Default implementation and getter/setter methods matching the given PClassConf...
PString getRustType(const PString &varType)
Get the Rust type matching a pdata type.
PString getRustAttributeDefaultValue(const PClassAttribute &attr)
Get the Rust literal default value of an attribute, as written into its Default impl.
PString moduleName
Name of the wrapper python module.
bool enableUnitTest
True to enable the unit tests.
Set of parameters to generate a project.
GeneratorMode mode
Mode to be used to generate the project.
PPath outputTestDir
Output path of the unit tests.
PVecDataConfig vecDataConfig
Configuration of classes to be generated.
PPath outputSourceDir
Output path of the sources.