@@ -12,29 +12,54 @@ use crate::{
1212} ;
1313
1414/// Valid data types for PHP.
15- #[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
16- #[ repr( u32 ) ]
15+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord ) ]
1716pub enum DataType {
18- Undef = IS_UNDEF ,
19-
20- Null = IS_NULL ,
21- False = IS_FALSE ,
22- True = IS_TRUE ,
23- Long = IS_LONG ,
24- Double = IS_DOUBLE ,
25- String = IS_STRING ,
26- Array = IS_ARRAY ,
27- Object = IS_OBJECT ,
28- Resource = IS_RESOURCE ,
29- Reference = IS_REFERENCE ,
30- Callable = IS_CALLABLE ,
31-
32- ConstantExpression = IS_CONSTANT_AST ,
33- Void = IS_VOID ,
34- Mixed = IS_MIXED ,
35-
36- /// Only used when creating arguments.
37- Bool = _IS_BOOL,
17+ Undef ,
18+ Null ,
19+ False ,
20+ True ,
21+ Long ,
22+ Double ,
23+ String ,
24+ Array ,
25+ Object ( Option < & ' static str > ) ,
26+ Resource ,
27+ Reference ,
28+ Callable ,
29+ ConstantExpression ,
30+ Void ,
31+ Mixed ,
32+ Bool ,
33+ }
34+
35+ impl Default for DataType {
36+ fn default ( ) -> Self {
37+ Self :: Void
38+ }
39+ }
40+
41+ impl DataType {
42+ /// Returns the integer representation of the data type.
43+ pub const fn as_u32 ( & self ) -> u32 {
44+ match self {
45+ DataType :: Undef => IS_UNDEF ,
46+ DataType :: Null => IS_NULL ,
47+ DataType :: False => IS_FALSE ,
48+ DataType :: True => IS_TRUE ,
49+ DataType :: Long => IS_LONG ,
50+ DataType :: Double => IS_DOUBLE ,
51+ DataType :: String => IS_STRING ,
52+ DataType :: Array => IS_ARRAY ,
53+ DataType :: Object ( _) => IS_OBJECT ,
54+ DataType :: Resource => IS_RESOURCE ,
55+ DataType :: Reference => IS_RESOURCE ,
56+ DataType :: Callable => IS_CALLABLE ,
57+ DataType :: ConstantExpression => IS_CONSTANT_AST ,
58+ DataType :: Void => IS_VOID ,
59+ DataType :: Mixed => IS_MIXED ,
60+ DataType :: Bool => _IS_BOOL,
61+ }
62+ }
3863}
3964
4065// TODO: Ideally want something like this
@@ -69,12 +94,15 @@ impl TryFrom<ZvalTypeFlags> for DataType {
6994 contains ! ( Double ) ;
7095 contains ! ( String ) ;
7196 contains ! ( Array ) ;
72- contains ! ( Object ) ;
7397 contains ! ( Resource ) ;
7498 contains ! ( Callable ) ;
7599 contains ! ( ConstantExpression ) ;
76100 contains ! ( Void ) ;
77101
102+ if value. contains ( ZvalTypeFlags :: Object ) {
103+ return Ok ( DataType :: Object ( None ) ) ;
104+ }
105+
78106 Err ( Error :: UnknownDatatype ( 0 ) )
79107 }
80108}
@@ -95,18 +123,20 @@ impl TryFrom<u32> for DataType {
95123 contains ! ( IS_VOID , Void ) ;
96124 contains ! ( IS_CALLABLE , Callable ) ;
97125 contains ! ( IS_CONSTANT_AST , ConstantExpression ) ;
98- contains ! ( IS_CONSTANT_AST , ConstantExpression ) ;
99- contains ! ( IS_CONSTANT_AST , ConstantExpression ) ;
100126 contains ! ( IS_REFERENCE , Reference ) ;
101127 contains ! ( IS_RESOURCE , Resource ) ;
102- contains ! ( IS_OBJECT , Object ) ;
103128 contains ! ( IS_ARRAY , Array ) ;
104129 contains ! ( IS_STRING , String ) ;
105130 contains ! ( IS_DOUBLE , Double ) ;
106131 contains ! ( IS_LONG , Long ) ;
107132 contains ! ( IS_TRUE , True ) ;
108133 contains ! ( IS_FALSE , False ) ;
109134 contains ! ( IS_NULL , Null ) ;
135+
136+ if ( value & IS_OBJECT ) == IS_OBJECT {
137+ return Ok ( DataType :: Object ( None ) ) ;
138+ }
139+
110140 contains ! ( IS_UNDEF , Undef ) ;
111141
112142 Err ( Error :: UnknownDatatype ( value) )
@@ -124,7 +154,7 @@ impl Display for DataType {
124154 DataType :: Double => write ! ( f, "Double" ) ,
125155 DataType :: String => write ! ( f, "String" ) ,
126156 DataType :: Array => write ! ( f, "Array" ) ,
127- DataType :: Object => write ! ( f, "Object" ) ,
157+ DataType :: Object ( obj ) => write ! ( f, "{}" , obj . as_deref ( ) . unwrap_or ( " Object") ) ,
128158 DataType :: Resource => write ! ( f, "Resource" ) ,
129159 DataType :: Reference => write ! ( f, "Reference" ) ,
130160 DataType :: Callable => write ! ( f, "Callable" ) ,
@@ -163,7 +193,7 @@ mod tests {
163193 test ! ( IS_DOUBLE , Double ) ;
164194 test ! ( IS_STRING , String ) ;
165195 test ! ( IS_ARRAY , Array ) ;
166- test ! ( IS_OBJECT , Object ) ;
196+ assert_eq ! ( DataType :: try_from ( IS_OBJECT ) , Ok ( DataType :: Object ( None ) ) ) ;
167197 test ! ( IS_RESOURCE , Resource ) ;
168198 test ! ( IS_REFERENCE , Reference ) ;
169199 test ! ( IS_CONSTANT_AST , ConstantExpression ) ;
@@ -173,7 +203,7 @@ mod tests {
173203 test ! ( IS_INTERNED_STRING_EX , String ) ;
174204 test ! ( IS_STRING_EX , String ) ;
175205 test ! ( IS_ARRAY_EX , Array ) ;
176- test ! ( IS_OBJECT_EX , Object ) ;
206+ assert_eq ! ( DataType :: try_from ( IS_OBJECT_EX ) , Ok ( DataType :: Object ( None ) ) ) ;
177207 test ! ( IS_RESOURCE_EX , Resource ) ;
178208 test ! ( IS_REFERENCE_EX , Reference ) ;
179209 test ! ( IS_CONSTANT_AST_EX , ConstantExpression ) ;
0 commit comments