window_enumerator/
errors.rs

1use std::fmt;
2
3/// Errors that can occur during window enumeration and inspection operations.
4#[derive(Debug)]
5pub enum WindowError {
6    /// The selection string format is invalid.
7    ///
8    /// Valid formats are: "all", "1,2,3", "1-3"
9    InvalidSelectionFormat,
10
11    /// The position sort string format is invalid.
12    ///
13    /// Valid formats are: "x1", "y-1", "x1|y1"
14    InvalidPositionSortFormat,
15
16    /// The range format is invalid.
17    ///
18    /// Valid range format is: "start-end" where start <= end
19    InvalidRange,
20
21    /// The index cannot be parsed as a valid usize.
22    InvalidIndex,
23
24    /// The sort order is invalid.
25    ///
26    /// Valid orders are: 1 (ascending) or -1 (descending)
27    InvalidSortOrder,
28
29    /// A Windows API call failed.
30    ///
31    /// Contains the Windows error code.
32    WindowsApiError(u32),
33
34    /// Other unspecified errors.
35    Other(String),
36}
37
38impl fmt::Display for WindowError {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            WindowError::InvalidSelectionFormat => {
42                write!(f, "Invalid selection format. Use 'all', '1,2,3', or '1-3'")
43            }
44            WindowError::InvalidPositionSortFormat => {
45                write!(
46                    f,
47                    "Invalid position sort format. Use 'x1', 'y-1', or 'x1|y1'"
48                )
49            }
50            WindowError::InvalidRange => write!(f, "Invalid range format"),
51            WindowError::InvalidIndex => write!(f, "Invalid index"),
52            WindowError::InvalidSortOrder => {
53                write!(f, "Sort order must be 1 (ascending) or -1 (descending)")
54            }
55            WindowError::WindowsApiError(code) => write!(f, "Windows API error: 0x{:08x}", code),
56            WindowError::Other(msg) => write!(f, "{}", msg),
57        }
58    }
59}
60
61impl std::error::Error for WindowError {}
62
63// 只在启用 windows 特性时提供 From 转换实现
64#[cfg(feature = "windows")]
65impl From<windows::core::Error> for WindowError {
66    fn from(error: windows::core::Error) -> Self {
67        WindowError::WindowsApiError(error.code().0 as u32) // ← 添加类型转换
68    }
69}
70
71/// A specialized [`Result`] type for window operations.
72pub type Result<T> = std::result::Result<T, WindowError>;