Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make control::Description From implementation fallible with TryFrom #100

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum Type {
}

impl TryFrom<u32> for Type {
type Error = ();
type Error = &'static str;

fn try_from(repr: u32) -> Result<Self, Self::Error> {
match repr {
Expand All @@ -45,7 +45,7 @@ impl TryFrom<u32> for Type {
0x0101 => Ok(Type::U16),
0x0102 => Ok(Type::U32),
0x0106 => Ok(Type::Area),
_ => Err(()),
_ => Err("Number is not a valid Type"),
}
}
}
Expand Down Expand Up @@ -122,7 +122,7 @@ impl fmt::Display for MenuItem {
}

impl TryFrom<(Type, v4l2_querymenu)> for MenuItem {
type Error = ();
type Error = &'static str;

fn try_from(item: (Type, v4l2_querymenu)) -> Result<Self, Self::Error> {
unsafe {
Expand All @@ -134,7 +134,7 @@ impl TryFrom<(Type, v4l2_querymenu)> for MenuItem {
.to_string(),
)),
Type::IntegerMenu => Ok(MenuItem::Value(item.1.__bindgen_anon_1.value)),
_ => Err(()),
_ => Err("Type is not a Menu or IntegerMenu"),
}
}
}
Expand Down 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 = &'static str;

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(|_| "String is not UTF-8")?
.to_string(),
minimum: ctrl.minimum,
maximum: ctrl.maximum,
step: ctrl.step,
default: ctrl.default_value,
flags: Flags::from(ctrl.flags),
items: None,
}
})
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ 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(|e| {
io::Error::other(format!("Description::try_from failed: {e}"))
})?;

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