Skip to content

Commit 6d17141

Browse files
authored
control: Add Type::Unknown (#99)
1 parent ced9df0 commit 6d17141

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

src/control.rs

+37-21
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,51 @@ pub enum Type {
2424
U16 = 0x0101,
2525
U32 = 0x0102,
2626
Area = 0x0106,
27-
}
2827

29-
impl TryFrom<u32> for Type {
30-
type Error = ();
28+
Unknown(u32),
29+
}
3130

32-
fn try_from(repr: u32) -> Result<Self, Self::Error> {
31+
impl From<u32> for Type {
32+
fn from(repr: u32) -> Self {
3333
match repr {
34-
1 => Ok(Type::Integer),
35-
2 => Ok(Type::Boolean),
36-
3 => Ok(Type::Menu),
37-
4 => Ok(Type::Button),
38-
5 => Ok(Type::Integer64),
39-
6 => Ok(Type::CtrlClass),
40-
7 => Ok(Type::String),
41-
8 => Ok(Type::Bitmask),
42-
9 => Ok(Type::IntegerMenu),
43-
44-
0x0100 => Ok(Type::U8),
45-
0x0101 => Ok(Type::U16),
46-
0x0102 => Ok(Type::U32),
47-
0x0106 => Ok(Type::Area),
48-
_ => Err(()),
34+
1 => Self::Integer,
35+
2 => Self::Boolean,
36+
3 => Self::Menu,
37+
4 => Self::Button,
38+
5 => Self::Integer64,
39+
6 => Self::CtrlClass,
40+
7 => Self::String,
41+
8 => Self::Bitmask,
42+
9 => Self::IntegerMenu,
43+
44+
0x0100 => Self::U8,
45+
0x0101 => Self::U16,
46+
0x0102 => Self::U32,
47+
0x0106 => Self::Area,
48+
repr => Self::Unknown(repr),
4949
}
5050
}
5151
}
5252

5353
impl From<Type> for u32 {
5454
fn from(t: Type) -> Self {
55-
t as Self
55+
match t {
56+
Type::Integer => 1,
57+
Type::Boolean => 2,
58+
Type::Menu => 3,
59+
Type::Button => 4,
60+
Type::Integer64 => 5,
61+
Type::CtrlClass => 6,
62+
Type::String => 7,
63+
Type::Bitmask => 8,
64+
Type::IntegerMenu => 9,
65+
66+
Type::U8 => 0x0100,
67+
Type::U16 => 0x0101,
68+
Type::U32 => 0x0102,
69+
Type::Area => 0x0106,
70+
Type::Unknown(t) => t,
71+
}
5672
}
5773
}
5874

@@ -168,7 +184,7 @@ impl From<v4l2_query_ext_ctrl> for Description {
168184
fn from(ctrl: v4l2_query_ext_ctrl) -> Self {
169185
Self {
170186
id: ctrl.id,
171-
typ: Type::try_from(ctrl.type_).unwrap(),
187+
typ: Type::from(ctrl.type_),
172188
name: unsafe { ffi::CStr::from_ptr(ctrl.name.as_ptr()) }
173189
.to_str()
174190
.unwrap()

0 commit comments

Comments
 (0)