@@ -16,7 +16,7 @@ pub enum HomiePropertyFormat {
16
16
IntegerRange ( IntegerRange ) ,
17
17
Enum ( Vec < String > ) ,
18
18
Color ( Vec < ColorFormat > ) ,
19
- Boolean { false_val : String , true_val : String } ,
19
+ Boolean ( BooleanFormat ) ,
20
20
Json ( String ) , // Raw JSON schema as string
21
21
Custom ( String ) ,
22
22
Empty ,
@@ -32,7 +32,7 @@ impl HomiePropertyFormat {
32
32
HomiePropertyFormat :: IntegerRange ( r) => r. is_empty ( ) ,
33
33
HomiePropertyFormat :: Enum ( values) => values. is_empty ( ) ,
34
34
HomiePropertyFormat :: Color ( formats) => formats. is_empty ( ) ,
35
- HomiePropertyFormat :: Boolean { false_val , true_val } => false_val . is_empty ( ) || true_val . is_empty ( ) ,
35
+ HomiePropertyFormat :: Boolean ( bf ) => bf . is_empty ( ) ,
36
36
HomiePropertyFormat :: Json ( data) => data. is_empty ( ) ,
37
37
HomiePropertyFormat :: Custom ( data) => data. is_empty ( ) ,
38
38
HomiePropertyFormat :: Empty => true ,
@@ -56,16 +56,49 @@ impl Display for HomiePropertyFormat {
56
56
formats. iter( ) . map( |c| c. to_string( ) ) . collect:: <Vec <String >>( ) . join( "," )
57
57
)
58
58
}
59
- HomiePropertyFormat :: Boolean { false_val, true_val } => {
60
- write ! ( f, "{},{}" , false_val, true_val)
61
- }
59
+ HomiePropertyFormat :: Boolean ( bf) => bf. fmt ( f) ,
62
60
HomiePropertyFormat :: Json ( data) => write ! ( f, "{}" , data) ,
63
61
HomiePropertyFormat :: Custom ( data) => write ! ( f, "{}" , data) ,
64
62
HomiePropertyFormat :: Empty => write ! ( f, "" ) ,
65
63
}
66
64
}
67
65
}
68
66
67
+ #[ derive( Debug , Serialize , Deserialize , Clone , Hash , PartialEq , PartialOrd ) ]
68
+ pub struct BooleanFormat {
69
+ pub false_val : String ,
70
+ pub true_val : String ,
71
+ }
72
+
73
+ impl BooleanFormat {
74
+ pub fn is_empty ( & self ) -> bool {
75
+ self . false_val . is_empty ( ) && self . true_val . is_empty ( )
76
+ }
77
+ }
78
+
79
+ impl Display for BooleanFormat {
80
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
81
+ write ! ( f, "{},{}" , self . false_val, self . true_val)
82
+ }
83
+ }
84
+
85
+ impl FromStr for BooleanFormat {
86
+ type Err = HomiePropertyFormatError ;
87
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
88
+ let tokens = s. split ( ',' ) . collect :: < Vec < & str > > ( ) ;
89
+ if tokens. len ( ) != 2 {
90
+ return Err ( HomiePropertyFormatError :: BooleanFormatError ) ;
91
+ }
92
+ if tokens[ 0 ] . is_empty ( ) || tokens[ 1 ] . is_empty ( ) || tokens[ 0 ] == tokens[ 1 ] {
93
+ return Err ( HomiePropertyFormatError :: BooleanFormatError ) ;
94
+ }
95
+
96
+ Ok ( Self {
97
+ false_val : tokens[ 0 ] . to_owned ( ) ,
98
+ true_val : tokens[ 1 ] . to_owned ( ) ,
99
+ } )
100
+ }
101
+ }
69
102
#[ derive( Debug , Serialize , Deserialize , Clone , PartialEq , Hash , PartialOrd ) ]
70
103
pub enum ColorFormat {
71
104
Rgb ,
@@ -141,20 +174,7 @@ impl HomiePropertyFormat {
141
174
}
142
175
Ok ( Self :: Color ( formats) )
143
176
}
144
- HomieDataType :: Boolean => {
145
- let tokens = raw. split ( ',' ) . collect :: < Vec < & str > > ( ) ;
146
- if tokens. len ( ) != 2 {
147
- return Err ( HomiePropertyFormatError :: BooleanFormatError ) ;
148
- }
149
- if tokens[ 0 ] . is_empty ( ) || tokens[ 1 ] . is_empty ( ) || tokens[ 0 ] == tokens[ 1 ] {
150
- return Err ( HomiePropertyFormatError :: BooleanFormatError ) ;
151
- }
152
-
153
- Ok ( Self :: Boolean {
154
- false_val : tokens[ 0 ] . to_owned ( ) ,
155
- true_val : tokens[ 1 ] . to_owned ( ) ,
156
- } )
157
- }
177
+ HomieDataType :: Boolean => Ok ( Self :: Boolean ( BooleanFormat :: from_str ( raw) ?) ) ,
158
178
HomieDataType :: JSON => Ok ( Self :: Json ( raw. to_owned ( ) ) ) , // todo: we need to check if
159
179
// this contains valid json
160
180
// string data
0 commit comments