1use super::ffi::{mpv_error, mpv_error_string};
2
3use std::ffi::{CStr, NulError};
4use std::fmt;
5use std::str::Utf8Error;
6
7#[derive(Debug)]
8pub struct Error(mpv_error);
9pub type Result<T> = std::result::Result<T, Error>;
10
11impl Error {
12 pub fn new(error: mpv_error) -> Self {
13 Self(error)
14 }
15}
16
17impl From<NulError> for Error {
18 fn from(_: NulError) -> Self {
19 Self::new(mpv_error::GENERIC)
20 }
21}
22
23impl From<Utf8Error> for Error {
24 fn from(_: Utf8Error) -> Self {
25 Self::new(mpv_error::GENERIC)
26 }
27}
28
29impl std::error::Error for Error {
30 fn description(&self) -> &str {
31 unsafe {
32 CStr::from_ptr(mpv_error_string(self.0))
33 .to_str()
34 .unwrap_or("unknow error")
35 }
36 }
37}
38
39impl fmt::Display for Error {
40 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41 let error = unsafe {
42 CStr::from_ptr(mpv_error_string(self.0))
43 .to_str()
44 .unwrap_or("unknown error")
45 };
46 write!(f, "[{}] {}", self.0 as i32, error)
47 }
48}