1
//! Library specific error messages.
2

            
3
use core::fmt;
4
use std::fmt::Display;
5

            
6
use serde::{Deserialize, Serialize};
7

            
8
/// All possible errors that can occur when interacting with mpv.
9
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10
pub enum ErrorCode {
11
    MpvError(String),
12
    JsonParseError(String),
13
    ConnectError(String),
14
    JsonContainsUnexptectedType,
15
    UnexpectedResult,
16
    UnexpectedValue,
17
    MissingValue,
18
    UnsupportedType,
19
    ValueDoesNotContainBool,
20
    ValueDoesNotContainF64,
21
    ValueDoesNotContainHashMap,
22
    ValueDoesNotContainPlaylist,
23
    ValueDoesNotContainString,
24
    ValueDoesNotContainUsize,
25
}
26

            
27
/// Any error that can occur when interacting with mpv.
28
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
29
pub struct Error(pub ErrorCode);
30

            
31
impl Display for Error {
32
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33
        Display::fmt(&self.0, f)
34
    }
35
}
36

            
37
impl std::error::Error for Error {}
38

            
39
impl Display for ErrorCode {
40
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41
        match *self {
42
            ErrorCode::ConnectError(ref msg) => f.write_str(&format!("ConnectError: {}", msg)),
43
            ErrorCode::JsonParseError(ref msg) => f.write_str(&format!("JsonParseError: {}", msg)),
44
            ErrorCode::MpvError(ref msg) => f.write_str(&format!("MpvError: {}", msg)),
45
            ErrorCode::JsonContainsUnexptectedType => {
46
                f.write_str("Mpv sent a value with an unexpected type")
47
            }
48
            ErrorCode::UnexpectedResult => f.write_str("Unexpected result received"),
49
            ErrorCode::UnexpectedValue => f.write_str("Unexpected value received"),
50
            ErrorCode::MissingValue => f.write_str("Missing value"),
51
            ErrorCode::UnsupportedType => f.write_str("Unsupported type received"),
52
            ErrorCode::ValueDoesNotContainBool => {
53
                f.write_str("The received value is not of type \'std::bool\'")
54
            }
55
            ErrorCode::ValueDoesNotContainF64 => {
56
                f.write_str("The received value is not of type \'std::f64\'")
57
            }
58
            ErrorCode::ValueDoesNotContainHashMap => {
59
                f.write_str("The received value is not of type \'std::collections::HashMap\'")
60
            }
61
            ErrorCode::ValueDoesNotContainPlaylist => {
62
                f.write_str("The received value is not of type \'mpvipc::Playlist\'")
63
            }
64
            ErrorCode::ValueDoesNotContainString => {
65
                f.write_str("The received value is not of type \'std::string::String\'")
66
            }
67
            ErrorCode::ValueDoesNotContainUsize => {
68
                f.write_str("The received value is not of type \'std::usize\'")
69
            }
70
        }
71
    }
72
}