Skip to main content

win_context_menu/
error.rs

1//! Error types for the `win-context-menu` crate.
2
3use std::path::PathBuf;
4
5/// Errors that can occur when working with Windows shell context menus.
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8    /// COM runtime failed to initialize.
9    #[error("COM initialization failed: {0}")]
10    ComInit(windows::core::Error),
11
12    /// A filesystem path could not be resolved to a shell item (PIDL).
13    #[error("Failed to parse path to shell item: {path}")]
14    ParsePath {
15        /// The path that failed to resolve.
16        path: PathBuf,
17        /// The underlying Windows error.
18        #[source]
19        source: windows::core::Error,
20    },
21
22    /// `SHBindToParent` could not locate the parent `IShellFolder`.
23    #[error("Failed to bind to parent shell folder: {0}")]
24    BindToParent(windows::core::Error),
25
26    /// Multiple paths were provided that do not share a common parent folder.
27    #[error("Selected paths do not share a common parent folder")]
28    NoCommonParent,
29
30    /// The shell folder refused to hand out an `IContextMenu` interface.
31    #[error("Failed to get context menu interface: {0}")]
32    GetContextMenu(windows::core::Error),
33
34    /// `IContextMenu::QueryContextMenu` failed.
35    #[error("QueryContextMenu failed: {0}")]
36    QueryContextMenu(windows::core::Error),
37
38    /// `TrackPopupMenu` failed to display the context menu.
39    #[error("TrackPopupMenu failed: {0}")]
40    TrackPopupMenu(windows::core::Error),
41
42    /// `IContextMenu::InvokeCommand` failed to execute the selected command.
43    #[error("Failed to invoke command: {0}")]
44    InvokeCommand(windows::core::Error),
45
46    /// `IContextMenu::GetCommandString` failed.
47    #[error("Failed to get command string: {0}")]
48    GetCommandString(windows::core::Error),
49
50    /// `GetMenuItemInfoW` failed to retrieve menu item metadata.
51    #[error("Failed to get menu item info: {0}")]
52    GetMenuItemInfo(windows::core::Error),
53
54    /// Failed to create the hidden owner window used for submenu rendering.
55    #[error("Failed to create hidden window: {0}")]
56    CreateWindow(windows::core::Error),
57
58    /// Failed to register the window class for the hidden owner window.
59    #[error("Failed to register window class: {0}")]
60    RegisterClass(windows::core::Error),
61
62    /// Catch-all for other Windows API errors.
63    #[error("Windows API error: {0}")]
64    Windows(#[from] windows::core::Error),
65}
66
67/// A specialized `Result` type for this crate.
68pub type Result<T> = std::result::Result<T, Error>;