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
/*
 * SPDX-License-Identifier: MIT
 */

//! Common [Error] types used across the library.
//!
use std::fmt::{Debug, Display, Formatter, Pointer};

use crate::spa::pod::PodError;

/// Error types
pub enum Error {
    /// Linux error code
    ErrorCode(u32),
    /// Error message
    ErrorMessage(&'static str),
    /// Trying to call interface method on null
    MethodCallOnNull,
    /// Method not found in the interface
    MethodNotFound(String),
    /// Interface has unexpected version
    VersionMismatch(u32, u32),
    /// Unexpected type
    TypeMismatch,
    /// Invalid time format
    WrongTimeFormat,
    /// Got the null from constructor method
    CannotCreateInstance,
    /// Unexpected null pointer
    NullPointer,
    /// Pod parse/write error
    PodParseError(PodError),
}

impl Debug for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        (self as &dyn Display).fmt(f)
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::ErrorCode(code) => write!(f, "ErrorCode({})", code),
            Error::ErrorMessage(message) => write!(f, "ErrorMessage({})", message),
            Error::MethodCallOnNull => write!(f, "MethodCallOnNull"),
            Error::MethodNotFound(method) => {
                write!(f, "MethodNotFound: method {} not found", method)
            }
            Error::VersionMismatch(expected, actual) => write!(
                f,
                "Version too low, expected {}, actual {}",
                expected, actual
            ),
            Error::TypeMismatch => write!(f, "TypeMismatch"),
            Error::WrongTimeFormat => write!(f, "WrongTimeFormat"),
            Error::CannotCreateInstance => write!(f, "CannotCreateInstance"),
            Error::NullPointer => write!(f, "NullPointer"),
            Error::PodParseError(pod_error) => write!(f, "PodParseError({:?})", pod_error),
        }
    }
}

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