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("MpvError: {0}")]
12
    MpvError(String),
13

            
14
    #[error("Error communicating over mpv socket: {0}")]
15
    MpvSocketConnectionError(String),
16

            
17
    #[error("Internal connection error: {0}")]
18
    InternalConnectionError(String),
19

            
20
    #[error("JsonParseError: {0}")]
21
    JsonParseError(#[from] serde_json::Error),
22

            
23
    #[error("Mpv sent a value with an unexpected type:\nExpected {expected_type}, received {received:#?}")]
24
    ValueContainsUnexpectedType {
25
        expected_type: String,
26
        received: Value,
27
    },
28

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

            
37
    #[error("Missing expected 'data' field in mpv message")]
38
    MissingMpvData,
39

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

            
46
    #[error("Unexpected property: {0:?}")]
47
    UnexpectedProperty(Property),
48

            
49
    #[error("Unknown error: {0}")]
50
    Other(String),
51
}
52

            
53
impl PartialEq for MpvError {
54
12
    fn eq(&self, other: &Self) -> bool {
55
12
        match (self, other) {
56
6
            (Self::MpvError(l0), Self::MpvError(r0)) => l0 == r0,
57
4
            (Self::MpvSocketConnectionError(l0), Self::MpvSocketConnectionError(r0)) => l0 == r0,
58
            (Self::InternalConnectionError(l0), Self::InternalConnectionError(r0)) => l0 == r0,
59
            (Self::JsonParseError(l0), Self::JsonParseError(r0)) => {
60
                l0.to_string() == r0.to_string()
61
            }
62
            (
63
                Self::ValueContainsUnexpectedType {
64
2
                    expected_type: l_expected_type,
65
2
                    received: l_received,
66
2
                },
67
2
                Self::ValueContainsUnexpectedType {
68
2
                    expected_type: r_expected_type,
69
2
                    received: r_received,
70
2
                },
71
2
            ) => l_expected_type == r_expected_type && l_received == r_received,
72
            (
73
                Self::DataContainsUnexpectedType {
74
                    expected_type: l_expected_type,
75
                    received: l_received,
76
                },
77
                Self::DataContainsUnexpectedType {
78
                    expected_type: r_expected_type,
79
                    received: r_received,
80
                },
81
            ) => l_expected_type == r_expected_type && l_received == r_received,
82
            (
83
                Self::MissingKeyInObject {
84
                    key: l_key,
85
                    map: l_map,
86
                },
87
                Self::MissingKeyInObject {
88
                    key: r_key,
89
                    map: r_map,
90
                },
91
            ) => l_key == r_key && l_map == r_map,
92

            
93
            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
94
        }
95
12
    }
96
}