PhoenixGenerator  3.4.0
Generate code from template files and projects in C++, Rust, Python and wrapped C++ to Python
Loading...
Searching...
No Matches
rust_type_utils.h File Reference
#include "PString.h"
#include "PClassAttribute.h"
Include dependency graph for rust_type_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

PString getRustAttributeDefaultValue (const PClassAttribute &attr)
 Get the Rust literal default value of an attribute, as written into its Default impl.
PString getRustTestValue (const PClassAttribute &attr)
 Get a non-default Rust literal value of an attribute, to be used to exercise its setter/getter in generated tests.
PString getRustType (const PString &varType)
 Get the Rust type matching a pdata type.
bool getRustTypeIsCopy (const PString &varType)
 Say if the Rust type matching a pdata type implements Copy (so getters can return it by value instead of by reference).

Function Documentation

◆ getRustAttributeDefaultValue()

PString getRustAttributeDefaultValue ( const PClassAttribute & attr)

Get the Rust literal default value of an attribute, as written into its Default impl.

Parameters
attr: attribute to be checked
Returns
Rust literal expression for the default value of the attribute

Definition at line 63 of file rust_type_utils.cpp.

63 {
64 PString varType(attr.getType());
65 bool isStringType(varType == "PString" || varType == "std::string" || varType == "PPath");
66 PString simpleType(getRustSimpleType(varType));
67 if(attr.getDefaultValue() != ""){
68 //pdata default values are written between braces (eg "{0}", "{\"Shadoko\"}")
69 PString rawValue(attr.getDefaultValue().eraseFirstLastChar("{} \n\t"));
70 if(isStringType){
71 //The pdata default is a double-quoted &str literal : String has no implicit conversion from &str, unlike C++'s std::string
72 return rawValue + ".to_string()";
73 }
74 if((simpleType == "f32" || simpleType == "f64") && !rawValue.find(".eE")){
75 //Rust has no implicit int->float coercion, unlike the C++ NSDMI this default value was written for
76 return rawValue + ".0";
77 }
78 return rawValue;
79 }
80 if(getIsPointerType(varType)){return "None";}
81 if(generator_typeIsList(varType)){return "Vec::new()";}
82 if(isStringType){return "String::new()";}
83 if(simpleType == "f32" || simpleType == "f64"){return "0.0";}
84 if(simpleType == "bool"){return "false";}
85 if(simpleType != ""){return "0";}
86 //Nested generated struct or enum : rely on its own Default implementation
87 return "Default::default()";
88}
const PString & getDefaultValue() const
Gets the defaultValue of the PClassAttribute.
const PString & getType() const
Gets the type of the PClassAttribute.
PString getRustSimpleType(const PString &varType)
Get the Rust type matching a simple pdata type (int, float, bool, ...).
bool generator_typeIsList(const PString &type)
Say if a given type is a std::list.
bool getIsPointerType(const PString &varType)
Say if a given type is a pointer type (i.e. it ends with a '*').

References generator_typeIsList(), PClassAttribute::getDefaultValue(), getIsPointerType(), getRustAttributeDefaultValue(), getRustSimpleType(), and PClassAttribute::getType().

Referenced by generator_rust_class_file(), and getRustAttributeDefaultValue().

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

◆ getRustTestValue()

PString getRustTestValue ( const PClassAttribute & attr)

Get a non-default Rust literal value of an attribute, to be used to exercise its setter/getter in generated tests.

Parameters
attr: attribute to be checked
Returns
Rust literal expression for a test value of the attribute, or an empty PString for pointer/list/nested types (handled separately by the caller)

Definition at line 94 of file rust_type_utils.cpp.

94 {
95 PString varType(attr.getType());
96 if(varType == "PString" || varType == "std::string" || varType == "PPath"){return "\"Some string\".to_string()";}
97 PString simpleType(getRustSimpleType(varType));
98 if(simpleType == "bool"){return "true";}
99 if(simpleType == "f32" || simpleType == "f64"){return "1.0";}
100 if(simpleType != ""){return "42";}
101 return "";
102}

References getRustSimpleType(), getRustTestValue(), and PClassAttribute::getType().

Referenced by getRustTestValue(), and RustTraitGetterSetter::testFunction().

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

◆ getRustType()

PString getRustType ( const PString & varType)

Get the Rust type matching a pdata type.

Parameters
varType: pdata type to be converted (eg "int", "std::string", "ConfigShadok *", "std::list<int>")
Returns
matching Rust type (eg "i32", "String", "Option<Box<ConfigShadok>>", "Vec<i32>")

Definition at line 34 of file rust_type_utils.cpp.

34 {
35 if(getIsPointerType(varType)){
36 PString pointeeType(varType.eraseFirstLastChar(" \n\t*"));
37 return "Option<Box<" + getRustType(pointeeType) + ">>";
38 }
39 if(generator_typeIsList(varType)){
40 return "Vec<" + getRustType(generator_getListNestedType(varType)) + ">";
41 }
42 PString simpleType(getRustSimpleType(varType));
43 if(simpleType != ""){return simpleType;}
44 if(varType == "PString" || varType == "std::string" || varType == "PPath"){return "String";}
45 //Nested generated struct or enum : the pdata type name is reused as-is for the Rust type name
46 return varType;
47}
PString getRustType(const PString &varType)
Get the Rust type matching a pdata type.
PString generator_getListNestedType(const PString &type)
Get the nested type inside a std::list.

References generator_getListNestedType(), generator_typeIsList(), getIsPointerType(), getRustSimpleType(), and getRustType().

Referenced by generator_rust_class_file(), getRustType(), RustTraitGetterSetter::implMethodDeclaration(), and RustTraitGetterSetter::testFunction().

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

◆ getRustTypeIsCopy()

bool getRustTypeIsCopy ( const PString & varType)

Say if the Rust type matching a pdata type implements Copy (so getters can return it by value instead of by reference).

Parameters
varType: pdata type to be checked
Returns
true if the matching Rust type is Copy, false otherwise

Definition at line 53 of file rust_type_utils.cpp.

53 {
54 if(getIsPointerType(varType) || generator_typeIsList(varType)){return false;}
55 if(varType == "PString" || varType == "std::string" || varType == "PPath"){return false;}
56 return getRustSimpleType(varType) != "";
57}

References generator_typeIsList(), getIsPointerType(), getRustSimpleType(), and getRustTypeIsCopy().

Referenced by getRustTypeIsCopy(), RustTraitGetterSetter::implMethodDeclaration(), and RustTraitGetterSetter::testFunction().

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