Skip to content

Commit 17b53b1

Browse files
committed
multiboot2: make FramebufferTypeId used again
1 parent 04483e3 commit 17b53b1

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

multiboot2/src/framebuffer.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ impl FramebufferTag {
104104
/// The type of framebuffer, one of: `Indexed`, `RGB` or `Text`.
105105
pub fn buffer_type(&self) -> Result<FramebufferType, UnknownFramebufferType> {
106106
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 => {
109110
let num_colors = reader.read_u32();
110111
let palette = unsafe {
111112
slice::from_raw_parts(
@@ -115,7 +116,7 @@ impl FramebufferTag {
115116
} as &'static [FramebufferColor];
116117
Ok(FramebufferType::Indexed { palette })
117118
}
118-
1 => {
119+
FramebufferTypeId::RGB => {
119120
let red_pos = reader.read_u8(); // These refer to the bit positions of the LSB of each field
120121
let red_mask = reader.read_u8(); // And then the length of the field from LSB to MSB
121122
let green_pos = reader.read_u8();
@@ -137,8 +138,7 @@ impl FramebufferTag {
137138
},
138139
})
139140
}
140-
2 => Ok(FramebufferType::Text),
141-
no => Err(UnknownFramebufferType(no)),
141+
FramebufferTypeId::Text => Ok(FramebufferType::Text),
142142
}
143143
}
144144
}
@@ -187,16 +187,29 @@ impl PartialEq for FramebufferTag {
187187
}
188188

189189
/// Helper struct for [`FramebufferType`].
190-
#[derive(Debug, PartialEq, Eq)]
190+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
191191
#[repr(u8)]
192192
#[allow(clippy::upper_case_acronyms)]
193-
pub enum FramebufferTypeId {
193+
enum FramebufferTypeId {
194194
Indexed = 0,
195195
RGB = 1,
196196
Text = 2,
197197
// spec says: there may be more variants in the future
198198
}
199199

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+
200213
/// The type of framebuffer.
201214
#[derive(Debug, PartialEq, Eq)]
202215
pub enum FramebufferType<'a> {
@@ -224,8 +237,8 @@ pub enum FramebufferType<'a> {
224237
Text,
225238
}
226239

240+
#[cfg(feature = "builder")]
227241
impl<'a> FramebufferType<'a> {
228-
#[cfg(feature = "builder")]
229242
fn to_bytes(&self) -> Vec<u8> {
230243
let mut v = Vec::new();
231244
match self {

0 commit comments

Comments
 (0)