1
//! Library specific error messages.
2

            
3
use serde_json::{Map, Value};
4
use thiserror::Error;
5

            
6
use crate::{MpvDataType, Property};
7

            
8
/// Any error that can occur when interacting with mpv.
9
#[derive(Error, Debug)]
10
pub enum MpvError {
11
    #[error("Mpv returned error in response to command: {message}\nCommand: {command:#?}")]
12
    MpvError {
13
        command: Vec<Value>,
14
        message: String,
15
    },
16

            
17
    #[error("Error communicating over mpv socket: {0}")]
18
    MpvSocketConnectionError(String),
19

            
20
    #[error("Internal connection error: {0}")]
21
    InternalConnectionError(String),
22

            
23
    #[error("JsonParseError: {0}")]
24
    JsonParseError(#[from] serde_json::Error),
25

            
26
    #[error(
27
        "Mpv sent a value with an unexpected type:\nExpected {expected_type}, received {received:#?}"
28
    )]
29
    ValueContainsUnexpectedType {
30
        expected_type: String,
31
        received: Value,
32
    },
33

            
34
    #[error(
35
        "Mpv sent data with an unexpected type:\nExpected {expected_type}, received {received:#?}"
36
    )]
37
    DataContainsUnexpectedType {
38
        expected_type: String,
39
        received: MpvDataType,
40
    },
41

            
42
    #[error("Missing expected 'data' field in mpv message")]
43
    MissingMpvData,
44

            
45
    #[error("Missing key in object:\nExpected {key} in {map:#?}")]
46
    MissingKeyInObject {
47
        key: String,
48
        map: Map<String, Value>,
49
    },
50

            
51
    #[error("Unexpected property: {0:?}")]
52
    UnexpectedProperty(Property),
53

            
54
    #[error("Unknown error: {0}")]
55
    Other(String),
56
}
57

            
58
impl PartialEq for MpvError {
59
6
    fn eq(&self, other: &Self) -> bool {
60
6
        match (self, other) {
61
            (
62
                Self::MpvError {
63
                    command: l_command,
64
                    message: l_message,
65
                },
66
                Self::MpvError {
67
                    command: r_command,
68
                    message: r_message,
69
                },
70
            ) => l_command == r_command && l_message == r_message,
71
4
            (Self::MpvSocketConnectionError(l0), Self::MpvSocketConnectionError(r0)) => l0 == r0,
72
            (Self::InternalConnectionError(l0), Self::InternalConnectionError(r0)) => l0 == r0,
73
            (Self::JsonParseError(l0), Self::JsonParseError(r0)) => {
74
                l0.to_string() == r0.to_string()
75
            }
76
            (
77
                Self::ValueContainsUnexpectedType {
78
2
                    expected_type: l_expected_type,
79
2
                    received: l_received,
80
                },
81
                Self::ValueContainsUnexpectedType {
82
2
                    expected_type: r_expected_type,
83
2
                    received: r_received,
84
                },
85
2
            ) => l_expected_type == r_expected_type && l_received == r_received,
86
            (
87
                Self::DataContainsUnexpectedType {
88
                    expected_type: l_expected_type,
89
                    received: l_received,
90
                },
91
                Self::DataContainsUnexpectedType {
92
                    expected_type: r_expected_type,
93
                    received: r_received,
94
                },
95
            ) => l_expected_type == r_expected_type && l_received == r_received,
96
            (
97
                Self::MissingKeyInObject {
98
                    key: l_key,
99
                    map: l_map,
100
                },
101
                Self::MissingKeyInObject {
102
                    key: r_key,
103
                    map: r_map,
104
                },
105
            ) => l_key == r_key && l_map == r_map,
106

            
107
            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
108
        }
109
6
    }
110
}