rusty_duplication/
error.rs

1use thiserror::Error;
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[derive(Debug, Error, Clone)]
6pub enum Error {
7  #[error("invalid buffer length")]
8  InvalidBufferLength,
9
10  /// A Windows error.
11  #[error("{api}: {err}")]
12  Windows {
13    api: &'static str,
14    err: windows::core::Error,
15  },
16}
17
18impl Error {
19  /// Create a new Windows error.
20  #[inline]
21  const fn windows(api: &'static str, err: windows::core::Error) -> Self {
22    Self::Windows { api, err }
23  }
24
25  /// Create a new Windows error from `GetLastError`.
26  #[inline]
27  pub(crate) fn last_win_err(api: &'static str) -> Self {
28    Self::windows(api, windows::core::Error::from_win32())
29  }
30
31  /// Return an error mapper to convert a Windows error to an [`Error`].
32  #[inline]
33  pub(crate) fn from_win_err(api: &'static str) -> impl FnOnce(windows::core::Error) -> Self {
34    move |e| Self::windows(api, e)
35  }
36}
37
38#[cfg(test)]
39mod tests {
40  use super::*;
41
42  #[test]
43  fn format_error() {
44    assert_eq!(
45      format!("{}", Error::InvalidBufferLength),
46      "invalid buffer length"
47    );
48
49    assert_eq!(
50      format!(
51        "{}",
52        Error::Windows {
53          api: "api",
54          err: windows::core::Error::empty()
55        }
56      ),
57      "api: The operation completed successfully. (0x00000000)"
58    );
59  }
60}