1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::os::raw::{c_int, c_uint, c_void};
use std::str;

mod api;
pub mod device;
pub mod graph;
pub mod log;

use api::*;

pub use device::Device;
pub use graph::Graph;

fn assert_size(label: &str, expected_size: c_uint, size: c_uint) {
    if expected_size != size {
        panic!(
            "Expected {} bytes for {}, got {}",
            expected_size, label, size
        );
    }
}

fn from_c_string(c_string: &[u8]) -> Result<String, Error> {
    match c_string.iter().position(|&c| c == 0) {
        Some(p) => str::from_utf8(&c_string[0..p])
            .map(|s| s.to_owned())
            .map_err(|_| Error::ApiError),
        None => Err(Error::ApiError),
    }
}

#[derive(Debug, PartialEq)]
pub enum Error {
    Busy,
    Error,
    OutOfMemory,
    DeviceNotFound,
    InvalidParameters,
    Timeout,
    MvcmdNotFound,
    NoData,
    Gone,
    UnsupportedGraphFile,
    MyriadError,
    ApiError,
    Idle,
}

trait IntoResult {
    fn into_result(self) -> Result<(), Error>;
}

impl IntoResult for c_int {
    fn into_result(self) -> Result<(), Error> {
        match self {
            MVNC_OK => Ok(()),
            MVNC_BUSY => Err(Error::Busy),
            MVNC_ERROR => Err(Error::Error),
            MVNC_OUT_OF_MEMORY => Err(Error::OutOfMemory),
            MVNC_DEVICE_NOT_FOUND => Err(Error::DeviceNotFound),
            MVNC_INVALID_PARAMETERS => Err(Error::InvalidParameters),
            MVNC_TIMEOUT => Err(Error::Timeout),
            MVNC_MVCMD_NOT_FOUND => Err(Error::MvcmdNotFound),
            MVNC_NO_DATA => Err(Error::NoData),
            MVNC_GONE => Err(Error::Gone),
            MVNC_UNSUPPORTED_GRAPH_FILE => Err(Error::UnsupportedGraphFile),
            MVNC_MYRIAD_ERROR => Err(Error::MyriadError),
            _ => Err(Error::ApiError),
        }
    }
}

trait DeviceHandle {
    fn handle(&self) -> *const c_void;
}