mpvipc_async/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Library specific error messages.

use serde_json::{Map, Value};
use thiserror::Error;

use crate::{MpvDataType, Property};

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

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

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

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

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

    #[error(
        "Mpv sent data with an unexpected type:\nExpected {expected_type}, received {received:#?}"
    )]
    DataContainsUnexpectedType {
        expected_type: String,
        received: MpvDataType,
    },

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

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

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

    #[error("Unknown error: {0}")]
    Other(String),
}

impl PartialEq for MpvError {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (
                Self::MpvError {
                    command: l_command,
                    message: l_message,
                },
                Self::MpvError {
                    command: r_command,
                    message: r_message,
                },
            ) => l_command == r_command && l_message == r_message,
            (Self::MpvSocketConnectionError(l0), Self::MpvSocketConnectionError(r0)) => l0 == r0,
            (Self::InternalConnectionError(l0), Self::InternalConnectionError(r0)) => l0 == r0,
            (Self::JsonParseError(l0), Self::JsonParseError(r0)) => {
                l0.to_string() == r0.to_string()
            }
            (
                Self::ValueContainsUnexpectedType {
                    expected_type: l_expected_type,
                    received: l_received,
                },
                Self::ValueContainsUnexpectedType {
                    expected_type: r_expected_type,
                    received: r_received,
                },
            ) => l_expected_type == r_expected_type && l_received == r_received,
            (
                Self::DataContainsUnexpectedType {
                    expected_type: l_expected_type,
                    received: l_received,
                },
                Self::DataContainsUnexpectedType {
                    expected_type: r_expected_type,
                    received: r_received,
                },
            ) => l_expected_type == r_expected_type && l_received == r_received,
            (
                Self::MissingKeyInObject {
                    key: l_key,
                    map: l_map,
                },
                Self::MissingKeyInObject {
                    key: r_key,
                    map: r_map,
                },
            ) => l_key == r_key && l_map == r_map,

            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
        }
    }
}