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("Mpv sent a value with an unexpected type:\nExpected {expected_type}, received {received:#?}")]
27
    ValueContainsUnexpectedType {
28
        expected_type: String,
29
        received: Value,
30
    },
31

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

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

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

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

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

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

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