Skip to content

Commit

Permalink
Make control::Description From implementation fallible with `TryF…
Browse files Browse the repository at this point in the history
…rom`
  • Loading branch information
MarijnS95 committed Apr 15, 2024
1 parent ced9df0 commit c25b87a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,24 @@ pub struct Description {
pub items: Option<Vec<(u32, MenuItem)>>,
}

impl From<v4l2_query_ext_ctrl> for Description {
fn from(ctrl: v4l2_query_ext_ctrl) -> Self {
Self {
impl TryFrom<v4l2_query_ext_ctrl> for Description {
type Error = ();

fn try_from(ctrl: v4l2_query_ext_ctrl) -> Result<Self, Self::Error> {
Ok(Self {
id: ctrl.id,
typ: Type::try_from(ctrl.type_).unwrap(),
typ: Type::try_from(ctrl.type_)?,
name: unsafe { ffi::CStr::from_ptr(ctrl.name.as_ptr()) }
.to_str()
.unwrap()
.map_err(|_| ())?
.to_string(),
minimum: ctrl.minimum,
maximum: ctrl.maximum,
step: ctrl.step,
default: ctrl.default_value,
flags: Flags::from(ctrl.flags),
items: None,
}
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ impl Device {
) {
Ok(_) => {
// get the basic control information
let mut control = Description::from(v4l2_ctrl);
let mut control = Description::try_from(v4l2_ctrl)
.map_err(|()| io::Error::other("Description::try_from failed"))?;

// if this is a menu control, enumerate its items
if control.typ == control::Type::Menu
Expand Down

0 comments on commit c25b87a

Please sign in to comment.