use std::borrow::Cow;
use std::ffi::CStr;
use std::marker::PhantomData;
use std::mem;
use ::libc::c_void;
#[derive(Debug,PartialEq,Eq,Clone,Copy,Hash)]
pub enum PortType {
Serial,
USB,
Disk,
PTPIP,
Direct,
SCSI,
Other,
}
pub struct Port<'a> {
inner: ::gphoto2::GPPortInfo,
__phantom: PhantomData<&'a c_void>,
}
impl<'a> Port<'a> {
pub fn port_type(&self) -> PortType {
let mut port_type = unsafe { mem::uninitialized() };
unsafe {
assert_eq!(::gphoto2::GP_OK, ::gphoto2::gp_port_info_get_type(self.inner, &mut port_type));
}
match port_type {
::gphoto2::GP_PORT_SERIAL => PortType::Serial,
::gphoto2::GP_PORT_USB => PortType::USB,
::gphoto2::GP_PORT_DISK => PortType::Disk,
::gphoto2::GP_PORT_PTPIP => PortType::PTPIP,
::gphoto2::GP_PORT_USB_DISK_DIRECT => PortType::Direct,
::gphoto2::GP_PORT_USB_SCSI => PortType::SCSI,
::gphoto2::GP_PORT_NONE | _ => PortType::Other,
}
}
pub fn name(&self) -> Cow<str> {
let mut name = unsafe { mem::uninitialized() };
unsafe {
assert_eq!(::gphoto2::GP_OK, ::gphoto2::gp_port_info_get_name(self.inner, &mut name));
String::from_utf8_lossy(CStr::from_ptr(name).to_bytes())
}
}
pub fn path(&self) -> Cow<str> {
let mut path = unsafe { mem::uninitialized() };
unsafe {
assert_eq!(::gphoto2::GP_OK, ::gphoto2::gp_port_info_get_path(self.inner, &mut path));
String::from_utf8_lossy(CStr::from_ptr(path).to_bytes())
}
}
}
#[doc(hidden)]
pub fn from_libgphoto2<'a>(_camera: &'a ::camera::Camera, ptr: ::gphoto2::GPPortInfo) -> Port<'a> {
Port {
inner: ptr,
__phantom: PhantomData,
}
}