PhoenixGenerator  3.4.0
Generate code from template files and projects in C++, Rust, Python and wrapped C++ to Python
Loading...
Searching...
No Matches
generator_class_rust.cpp File Reference
#include "openFileStream.h"
#include "header_generator.h"
#include "rust_type_utils.h"
#include "generator_class_rust.h"
Include dependency graph for generator_class_rust.cpp:

Go to the source code of this file.

Functions

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 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 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_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 defines.
bool generator_rust_enum_file (std::ofstream &fs, const PClassConfig &classConfig)
 Write the Rust enum matching the given PClassConfig.
bool rust_checkDefaultValueExist (const std::vector< PClassAttribute > &vecAttr)
 Check if the vector of PClassAttribute has default value.

Function Documentation

◆ generator_rust_class()

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.

Parameters
manager: PRustTraitBackendManager which handles all trait backend
rsFile: file to be saved
vecClassConfig: configuration of the classes to be used
mode: mode of the generator
Returns
true on success, false otherwise

Definition at line 108 of file generator_class_rust.cpp.

108 {
109 if(baseFileName == ""){return false;}
110 PPath rsFile = outputSourceDir / PPath(baseFileName.toSnakeCase() + PString(".rs"));
111 std::ofstream fs;
112 if(!openFileStream(fs, rsFile)){return false;}
113 licenceSave(fs);
114 manager.headerExtraInclude(fs, mode);
115 fs << std::endl;
116 bool b(true);
117 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
118 if(it->getIsEnum()){
119 b &= generator_rust_enum_file(fs, *it);
120 }else{
121 b &= generator_rust_class_file(manager, fs, *it, mode);
122 }
123 }
124 fs.close();
125 return b;
126}
void headerExtraInclude(std::ofstream &fs, const GeneratorMode &mode) const
Add extra include on the header.
bool generator_rust_enum_file(std::ofstream &fs, const PClassConfig &classConfig)
Write the Rust enum matching the given PClassConfig.
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...
void licenceSave(std::ofstream &fs)
Saves the policy.

References generator_rust_class(), generator_rust_class_file(), generator_rust_enum_file(), PRustTraitBackendManager::headerExtraInclude(), and licenceSave().

Referenced by generator_rust_class(), and generator_rust_class_full().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generator_rust_class_file()

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 PClassConfig.

Parameters
manager: PRustTraitBackendManager which handles all trait backend
[out]fs: file to be completed
classConfig: PClassConfig to be used
mode: mode of the generator
Returns
true on success, false otherwise

Definition at line 69 of file generator_class_rust.cpp.

69 {
70 PString name(classConfig.getName());
71 const std::vector<PClassAttribute> & vecAttr(classConfig.getListAttribute());
72
73 if(classConfig.getClassDocumentation() != ""){fs << classConfig.getClassDocumentation() << std::endl;}
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;
79 }
80 fs << "}" << std::endl << std::endl;
81
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){
86 fs << "\t\t\t" << it->getName().toSnakeCase() << ": " << getRustAttributeDefaultValue(*it) << "," << std::endl;
87 }
88 fs << "\t\t}" << std::endl;
89 fs << "\t}" << std::endl;
90 fs << "}" << std::endl << std::endl;
91
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;
95 manager.implMethodDeclaration(fs, classConfig, mode);
96 fs << "}" << std::endl << std::endl;
97 manager.classExtraDeclaration(fs, classConfig, mode);
98 return true;
99}
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.
void classExtraDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of extra declarations.
void implMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of the impl block methods.
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.

References PRustTraitBackendManager::classExtraDeclaration(), generator_rust_class_file(), PClassConfig::getClassDocumentation(), PClassConfig::getListAttribute(), PClassConfig::getName(), getRustAttributeDefaultValue(), getRustType(), and PRustTraitBackendManager::implMethodDeclaration().

Referenced by generator_rust_class(), and generator_rust_class_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generator_rust_class_full()

bool generator_rust_class_full ( const PRustTraitBackendManager & manager,
const ProjectParam & projectParam )

Generate the full Rust sources (and related integration tests) from configuration.

Parameters
manager: PRustTraitBackendManager which handles all trait backend
projectParam: description of classes and how and where to generate sources and test
Returns
true on success, false otherwise

Definition at line 163 of file generator_class_rust.cpp.

163 {
164 bool b(true);
165 b &= projectParam.outputSourceDir.createDirectory();
166 if(projectParam.mode.enableUnitTest){
167 b &= projectParam.outputTestDir.createDirectory();
168 }
169 for(const PDataConfig & config : projectParam.vecDataConfig){
170 PPath baseFileName = config.getFileName().getFileName().eraseExtension();
171 b &= generator_rust_class(manager, config.getVecClassConfig(), projectParam.outputSourceDir, baseFileName, projectParam.mode);
172 if(projectParam.mode.enableUnitTest){
173 b &= generator_rust_class_test(manager, projectParam.outputTestDir, projectParam.mode.moduleName, baseFileName, config.getVecClassConfig(), projectParam.mode);
174 }
175 }
176 return b;
177}
Class to describe a basic class.
Definition PDataConfig.h:17
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.
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 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...
PString moduleName
Name of the wrapper python module.
bool enableUnitTest
True to enable the unit tests.
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.

References GeneratorMode::enableUnitTest, generator_rust_class(), generator_rust_class_full(), generator_rust_class_test(), PDataConfig::getFileName(), PDataConfig::getVecClassConfig(), ProjectParam::mode, GeneratorMode::moduleName, ProjectParam::outputSourceDir, ProjectParam::outputTestDir, and ProjectParam::vecDataConfig.

Referenced by generateRustClassesFull(), and generator_rust_class_full().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generator_rust_class_test()

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 defines.

Parameters
manager: PRustTraitBackendManager which handles all trait backend
outputTestDir: directory where to save the test
crateName: name of the Cargo crate the generated sources belong to
moduleName: name of the Rust module (matches the generated source file base name)
vecClassConfig: configuration of the classes to be used
mode: mode of the generator
Returns
true on success, false otherwise

Definition at line 137 of file generator_class_rust.cpp.

137 {
138 PString moduleName(baseFileName.toSnakeCase());
139 PPath testFile = outputTestDir / PPath(moduleName + PString("_test.rs"));
140 std::ofstream fs;
141 if(!openFileStream(fs, testFile)){return false;}
142 licenceSave(fs);
143 PString comma("");
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();
148 comma = ", ";
149 }
150 fs << "};" << std::endl << std::endl;
151 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
152 manager.testFunction(fs, *it, mode);
153 }
154 fs.close();
155 return true;
156}
void testFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of test function.

References generator_rust_class_test(), licenceSave(), and PRustTraitBackendManager::testFunction().

Referenced by generator_rust_class_full(), and generator_rust_class_test().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generator_rust_enum_file()

bool generator_rust_enum_file ( std::ofstream & fs,
const PClassConfig & classConfig )

Write the Rust enum matching the given PClassConfig.

Parameters
[out]fs: file to be completed
classConfig: PClassConfig to be used
Returns
true on success, false otherwise

Definition at line 29 of file generator_class_rust.cpp.

29 {
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;
34 return false;
35 }
36 if(classConfig.getClassDocumentation() != ""){fs << classConfig.getClassDocumentation() << std::endl;}
37 fs << "#[derive(Debug, Clone, Copy, PartialEq, Eq)]" << std::endl;
38 fs << "pub enum " << name << " {" << std::endl;
39 bool isDefaultCounterActivated = !rust_checkDefaultValueExist(vecAttr);
40 size_t i(0LU);
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){
45 fs << " = " << i;
46 }else if(it->getDefaultValue() != ""){
47 fs << " = " << it->getDefaultValue();
48 }
49 fs << "," << std::endl;
50 ++i;
51 }
52 fs << "}" << std::endl << std::endl;
53
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;
59 return true;
60}
bool rust_checkDefaultValueExist(const std::vector< PClassAttribute > &vecAttr)
Check if the vector of PClassAttribute has default value.

References generator_rust_enum_file(), PClassConfig::getClassDocumentation(), PClassConfig::getListAttribute(), PClassConfig::getName(), and rust_checkDefaultValueExist().

Referenced by generator_rust_class(), and generator_rust_enum_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rust_checkDefaultValueExist()

bool rust_checkDefaultValueExist ( const std::vector< PClassAttribute > & vecAttr)

Check if the vector of PClassAttribute has default value.

Parameters
vecAttr: vector of attribute to be checked
Returns
true if there is at least one default value, false if there is none

Definition at line 16 of file generator_class_rust.cpp.

16 {
17 bool hasDefaultValue = false;
18 for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end() && !hasDefaultValue; ++it){
19 hasDefaultValue |= it->getDefaultValue() != "";
20 }
21 return hasDefaultValue;
22}

References rust_checkDefaultValueExist().

Referenced by generator_rust_enum_file(), and rust_checkDefaultValueExist().

Here is the call graph for this function:
Here is the caller graph for this function: