11//! Wrapper for enums introduced in C.
22
3- use crate :: bindings:: {
4- IS_ARRAY , IS_CALLABLE , IS_CONSTANT_AST , IS_DOUBLE , IS_FALSE , IS_LONG , IS_NULL , IS_OBJECT ,
5- IS_REFERENCE , IS_RESOURCE , IS_STRING , IS_TRUE , IS_UNDEF , IS_VOID ,
6- } ;
3+ use std:: { convert:: TryFrom , fmt:: Display } ;
74
8- use super :: types:: long:: ZendLong ;
5+ use crate :: {
6+ bindings:: {
7+ IS_ARRAY , IS_CALLABLE , IS_CONSTANT_AST , IS_DOUBLE , IS_FALSE , IS_LONG , IS_NULL , IS_OBJECT ,
8+ IS_REFERENCE , IS_RESOURCE , IS_STRING , IS_TRUE , IS_UNDEF , IS_VOID ,
9+ } ,
10+ errors:: { Error , Result } ,
11+ } ;
912
1013/// Valid data types for PHP.
1114#[ derive( Clone , Copy , Debug ) ]
@@ -29,30 +32,48 @@ pub enum DataType {
2932 Void = IS_VOID ,
3033}
3134
32- impl From < ZendLong > for DataType {
33- fn from ( _: ZendLong ) -> Self {
34- Self :: Long
35- }
36- }
35+ impl TryFrom < u8 > for DataType {
36+ type Error = Error ;
3737
38- impl From < bool > for DataType {
39- fn from ( x : bool ) -> Self {
40- if x {
41- Self :: True
42- } else {
43- Self :: False
44- }
45- }
46- }
38+ fn try_from ( value : u8 ) -> Result < Self > {
39+ match value as u32 {
40+ IS_UNDEF => Ok ( DataType :: Undef ) ,
41+ IS_NULL => Ok ( DataType :: Null ) ,
42+ IS_FALSE => Ok ( DataType :: False ) ,
43+ IS_TRUE => Ok ( DataType :: True ) ,
44+ IS_LONG => Ok ( DataType :: Long ) ,
45+ IS_DOUBLE => Ok ( DataType :: Double ) ,
46+ IS_STRING => Ok ( DataType :: String ) ,
47+ IS_ARRAY => Ok ( DataType :: Array ) ,
48+ IS_OBJECT => Ok ( DataType :: Object ) ,
49+ IS_RESOURCE => Ok ( DataType :: Resource ) ,
50+ IS_REFERENCE => Ok ( DataType :: Reference ) ,
51+ IS_CALLABLE => Ok ( DataType :: Callable ) ,
52+ IS_CONSTANT_AST => Ok ( DataType :: ConstantExpression ) ,
53+ IS_VOID => Ok ( DataType :: Void ) ,
4754
48- impl From < f64 > for DataType {
49- fn from ( _: f64 ) -> Self {
50- Self :: Double
55+ _ => Err ( Error :: UnknownDatatype ( value) ) ,
56+ }
5157 }
5258}
5359
54- impl From < String > for DataType {
55- fn from ( _: String ) -> Self {
56- Self :: String
60+ impl Display for DataType {
61+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
62+ match self {
63+ DataType :: Undef => write ! ( f, "Undefined" ) ,
64+ DataType :: Null => write ! ( f, "Null" ) ,
65+ DataType :: False => write ! ( f, "False" ) ,
66+ DataType :: True => write ! ( f, "True" ) ,
67+ DataType :: Long => write ! ( f, "Long" ) ,
68+ DataType :: Double => write ! ( f, "Double" ) ,
69+ DataType :: String => write ! ( f, "String" ) ,
70+ DataType :: Array => write ! ( f, "Array" ) ,
71+ DataType :: Object => write ! ( f, "Object" ) ,
72+ DataType :: Resource => write ! ( f, "Resource" ) ,
73+ DataType :: Reference => write ! ( f, "Reference" ) ,
74+ DataType :: Callable => write ! ( f, "Callable" ) ,
75+ DataType :: ConstantExpression => write ! ( f, "Constant Expression" ) ,
76+ DataType :: Void => write ! ( f, "Void" ) ,
77+ }
5778 }
5879}
0 commit comments