@@ -104,8 +104,9 @@ impl FramebufferTag {
104
104
/// The type of framebuffer, one of: `Indexed`, `RGB` or `Text`.
105
105
pub fn buffer_type ( & self ) -> Result < FramebufferType , UnknownFramebufferType > {
106
106
let mut reader = Reader :: new ( self . buffer . as_ptr ( ) ) ;
107
- match self . type_no {
108
- 0 => {
107
+ let typ = FramebufferTypeId :: try_from ( self . type_no ) ?;
108
+ match typ {
109
+ FramebufferTypeId :: Indexed => {
109
110
let num_colors = reader. read_u32 ( ) ;
110
111
let palette = unsafe {
111
112
slice:: from_raw_parts (
@@ -115,7 +116,7 @@ impl FramebufferTag {
115
116
} as & ' static [ FramebufferColor ] ;
116
117
Ok ( FramebufferType :: Indexed { palette } )
117
118
}
118
- 1 => {
119
+ FramebufferTypeId :: RGB => {
119
120
let red_pos = reader. read_u8 ( ) ; // These refer to the bit positions of the LSB of each field
120
121
let red_mask = reader. read_u8 ( ) ; // And then the length of the field from LSB to MSB
121
122
let green_pos = reader. read_u8 ( ) ;
@@ -137,8 +138,7 @@ impl FramebufferTag {
137
138
} ,
138
139
} )
139
140
}
140
- 2 => Ok ( FramebufferType :: Text ) ,
141
- no => Err ( UnknownFramebufferType ( no) ) ,
141
+ FramebufferTypeId :: Text => Ok ( FramebufferType :: Text ) ,
142
142
}
143
143
}
144
144
}
@@ -187,16 +187,29 @@ impl PartialEq for FramebufferTag {
187
187
}
188
188
189
189
/// Helper struct for [`FramebufferType`].
190
- #[ derive( Debug , PartialEq , Eq ) ]
190
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
191
191
#[ repr( u8 ) ]
192
192
#[ allow( clippy:: upper_case_acronyms) ]
193
- pub enum FramebufferTypeId {
193
+ enum FramebufferTypeId {
194
194
Indexed = 0 ,
195
195
RGB = 1 ,
196
196
Text = 2 ,
197
197
// spec says: there may be more variants in the future
198
198
}
199
199
200
+ impl TryFrom < u8 > for FramebufferTypeId {
201
+ type Error = UnknownFramebufferType ;
202
+
203
+ fn try_from ( value : u8 ) -> Result < Self , Self :: Error > {
204
+ match value {
205
+ 0 => Ok ( Self :: Indexed ) ,
206
+ 1 => Ok ( Self :: RGB ) ,
207
+ 2 => Ok ( Self :: Text ) ,
208
+ val => Err ( UnknownFramebufferType ( val) )
209
+ }
210
+ }
211
+ }
212
+
200
213
/// The type of framebuffer.
201
214
#[ derive( Debug , PartialEq , Eq ) ]
202
215
pub enum FramebufferType < ' a > {
@@ -224,8 +237,8 @@ pub enum FramebufferType<'a> {
224
237
Text ,
225
238
}
226
239
240
+ #[ cfg( feature = "builder" ) ]
227
241
impl < ' a > FramebufferType < ' a > {
228
- #[ cfg( feature = "builder" ) ]
229
242
fn to_bytes ( & self ) -> Vec < u8 > {
230
243
let mut v = Vec :: new ( ) ;
231
244
match self {
0 commit comments