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