PhoenixGenerator
3.4.0
Generate code from template files and projects in C++, Rust, Python and wrapped C++ to Python
Toggle main menu visibility
Loading...
Searching...
No Matches
rust_type_utils.cpp
Go to the documentation of this file.
1
/***************************************
2
Auteur : Pierre Aubert
3
Mail : pierre.aubert@lapp.in2p3.fr
4
Licence : LGPL-3.0-only
5
****************************************/
6
7
#include "
type_utils.h
"
8
#include "
rust_type_utils.h
"
9
11
14
PString
getRustSimpleType
(
const
PString & varType){
15
bool
isUnsigned = varType.isSameBegining(
"unsigned"
);
16
PString restVarName(varType.replace(
"unsigned"
,
""
).eraseChar(
" \n\t*&"
));
17
if
(restVarName ==
"char"
){
return
isUnsigned ?
"u8"
:
"i8"
;}
18
if
(restVarName ==
"short"
){
return
isUnsigned ?
"u16"
:
"i16"
;}
19
if
(restVarName ==
"int"
){
return
isUnsigned ?
"u32"
:
"i32"
;}
20
if
(restVarName ==
"long"
|| restVarName ==
"longint"
){
return
isUnsigned ?
"u64"
:
"i64"
;}
21
if
(restVarName ==
"size_t"
){
return
"usize"
;}
22
if
(restVarName ==
"ssize_t"
){
return
"isize"
;}
23
if
(restVarName ==
"float"
){
return
"f32"
;}
24
if
(restVarName ==
"double"
){
return
"f64"
;}
25
if
(restVarName ==
"bool"
){
return
"bool"
;}
26
if
(restVarName ==
"void"
){
return
"()"
;}
27
return
""
;
28
}
29
31
34
PString
getRustType
(
const
PString & varType){
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
}
48
50
53
bool
getRustTypeIsCopy
(
const
PString & varType){
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
}
58
60
63
PString
getRustAttributeDefaultValue
(
const
PClassAttribute
& attr){
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
}
89
91
94
PString
getRustTestValue
(
const
PClassAttribute
& attr){
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
}
PClassAttribute
Describes a class attribute.
Definition
PClassAttribute.h:13
PClassAttribute::getDefaultValue
const PString & getDefaultValue() const
Gets the defaultValue of the PClassAttribute.
Definition
PClassAttribute.cpp:116
PClassAttribute::getType
const PString & getType() const
Gets the type of the PClassAttribute.
Definition
PClassAttribute.cpp:88
getRustSimpleType
PString getRustSimpleType(const PString &varType)
Get the Rust type matching a simple pdata type (int, float, bool, ...).
Definition
rust_type_utils.cpp:14
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...
Definition
rust_type_utils.cpp:53
getRustType
PString getRustType(const PString &varType)
Get the Rust type matching a pdata type.
Definition
rust_type_utils.cpp:34
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 gen...
Definition
rust_type_utils.cpp:94
getRustAttributeDefaultValue
PString getRustAttributeDefaultValue(const PClassAttribute &attr)
Get the Rust literal default value of an attribute, as written into its Default impl.
Definition
rust_type_utils.cpp:63
rust_type_utils.h
generator_getListNestedType
PString generator_getListNestedType(const PString &type)
Get the nested type inside a std::list.
Definition
type_utils.cpp:22
generator_typeIsList
bool generator_typeIsList(const PString &type)
Say if a given type is a std::list.
Definition
type_utils.cpp:14
getIsPointerType
bool getIsPointerType(const PString &varType)
Say if a given type is a pointer type (i.e. it ends with a '*').
Definition
type_utils.cpp:41
type_utils.h
src
Representation
rust_type_utils.cpp
Generated by
1.18.0