1use std::os::raw::{c_int, c_uint, c_void};
2use std::str;
3
4mod api;
5pub mod device;
6pub mod graph;
7pub mod log;
8
9use api::*;
10
11pub use device::Device;
12pub use graph::Graph;
13
14fn assert_size(label: &str, expected_size: c_uint, size: c_uint) {
15 if expected_size != size {
16 panic!(
17 "Expected {} bytes for {}, got {}",
18 expected_size, label, size
19 );
20 }
21}
22
23fn from_c_string(c_string: &[u8]) -> Result<String, Error> {
24 match c_string.iter().position(|&c| c == 0) {
25 Some(p) => str::from_utf8(&c_string[0..p])
26 .map(|s| s.to_owned())
27 .map_err(|_| Error::ApiError),
28 None => Err(Error::ApiError),
29 }
30}
31
32#[derive(Debug, PartialEq)]
33pub enum Error {
34 Busy,
35 Error,
36 OutOfMemory,
37 DeviceNotFound,
38 InvalidParameters,
39 Timeout,
40 MvcmdNotFound,
41 NoData,
42 Gone,
43 UnsupportedGraphFile,
44 MyriadError,
45 ApiError,
46 Idle,
47}
48
49trait IntoResult {
50 fn into_result(self) -> Result<(), Error>;
51}
52
53impl IntoResult for c_int {
54 fn into_result(self) -> Result<(), Error> {
55 match self {
56 MVNC_OK => Ok(()),
57 MVNC_BUSY => Err(Error::Busy),
58 MVNC_ERROR => Err(Error::Error),
59 MVNC_OUT_OF_MEMORY => Err(Error::OutOfMemory),
60 MVNC_DEVICE_NOT_FOUND => Err(Error::DeviceNotFound),
61 MVNC_INVALID_PARAMETERS => Err(Error::InvalidParameters),
62 MVNC_TIMEOUT => Err(Error::Timeout),
63 MVNC_MVCMD_NOT_FOUND => Err(Error::MvcmdNotFound),
64 MVNC_NO_DATA => Err(Error::NoData),
65 MVNC_GONE => Err(Error::Gone),
66 MVNC_UNSUPPORTED_GRAPH_FILE => Err(Error::UnsupportedGraphFile),
67 MVNC_MYRIAD_ERROR => Err(Error::MyriadError),
68 _ => Err(Error::ApiError),
69 }
70 }
71}
72
73trait DeviceHandle {
74 fn handle(&self) -> *const c_void;
75}