libobs_window_helper/
error.rs

1use std::fmt::Display;
2
3/// Error type for window helper operations.
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub enum WindowHelperError {
6    /// Error from Windows API
7    WindowsApiError(String),
8    /// Failed to get file name from path
9    FileNameError,
10    /// Failed to convert to string
11    StringConversionError,
12    /// Microsoft internal executable (filtered out)
13    MicrosoftInternalExe,
14    /// OBS executable (filtered out)
15    ObsExe,
16    /// Invalid state encountered
17    InvalidState(String),
18    /// No window found
19    NoWindowFound,
20    /// Integer conversion error
21    IntConversionError(String),
22}
23
24impl Display for WindowHelperError {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            WindowHelperError::WindowsApiError(e) => write!(f, "Windows API error: {}", e),
28            WindowHelperError::FileNameError => write!(f, "Failed to get file name"),
29            WindowHelperError::StringConversionError => write!(f, "Failed to convert to string"),
30            WindowHelperError::MicrosoftInternalExe => {
31                write!(f, "Handle is a Microsoft internal exe")
32            }
33            WindowHelperError::ObsExe => write!(f, "Handle is obs64.exe"),
34            WindowHelperError::InvalidState(msg) => write!(f, "Invalid state: {}", msg),
35            WindowHelperError::NoWindowFound => write!(f, "No window found"),
36            WindowHelperError::IntConversionError(e) => {
37                write!(f, "Integer conversion error: {}", e)
38            }
39        }
40    }
41}
42
43impl std::error::Error for WindowHelperError {}
44
45#[cfg(windows)]
46impl From<windows::core::Error> for WindowHelperError {
47    fn from(err: windows::core::Error) -> Self {
48        WindowHelperError::WindowsApiError(err.to_string())
49    }
50}
51
52impl From<std::num::TryFromIntError> for WindowHelperError {
53    fn from(err: std::num::TryFromIntError) -> Self {
54        WindowHelperError::IntConversionError(err.to_string())
55    }
56}
57
58impl From<std::convert::Infallible> for WindowHelperError {
59    fn from(_: std::convert::Infallible) -> Self {
60        // Infallible can never actually be constructed, so this is unreachable
61        unreachable!()
62    }
63}