mpv/
mpv_error.rs

1use std::{result, ffi, fmt, error};
2use std::error::Error as StdError;
3pub use num::FromPrimitive;
4
5use mpv_gen::mpv_error_string;
6pub use mpv_gen::Error;
7
8pub type Result<T> = result::Result<T, Error>;
9
10impl error::Error for Error {
11    fn description(&self) -> &str {
12        let str_ptr = unsafe { mpv_error_string(*self as ::std::os::raw::c_int) };
13        assert!(!str_ptr.is_null());
14        unsafe { ffi::CStr::from_ptr(str_ptr).to_str().unwrap() }
15    }
16}
17
18impl fmt::Display for Error {
19    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20        write!(f, "{} ({:?})", self.description(), self)
21    }
22}
23
24/// utility function to transform an int (sent by libmpv) into a Result,
25/// depending if the received int is 0 or something else
26pub fn ret_to_result<T>(ret: i32, default: T) -> Result<T> {
27    if ret < 0 {
28        Err(Error::from_i32(ret).unwrap())
29    } else {
30        Ok(default)
31    }
32}