crossterm_winapi/
lib.rs

1#![cfg(windows)]
2#![deny(unused_imports)]
3
4use std::io;
5
6use winapi::shared::minwindef::BOOL;
7use winapi::um::handleapi::INVALID_HANDLE_VALUE;
8use winapi::um::wincontypes::COORD;
9use winapi::um::winnt::HANDLE;
10
11pub use self::{
12    cfi::FontInfo,
13    console::Console,
14    console_mode::ConsoleMode,
15    csbi::ScreenBufferInfo,
16    handle::{Handle, HandleType},
17    screen_buffer::ScreenBuffer,
18    semaphore::Semaphore,
19    structs::{
20        ButtonState, ControlKeyState, Coord, EventFlags, InputRecord, KeyEventRecord, MouseEvent,
21        Size, WindowPositions,
22    },
23};
24
25mod cfi;
26mod console;
27mod console_mode;
28mod csbi;
29mod handle;
30mod screen_buffer;
31mod semaphore;
32mod structs;
33
34/// Get the result of a call to WinAPI as an [`io::Result`].
35#[inline]
36pub fn result(return_value: BOOL) -> io::Result<()> {
37    if return_value != 0 {
38        Ok(())
39    } else {
40        Err(io::Error::last_os_error())
41    }
42}
43
44/// Get the result of a call to WinAPI that returns a
45/// [`COORD`](https://docs.microsoft.com/en-us/windows/console/coord-str) as an [`io::Result`].
46#[inline]
47pub fn coord_result(return_value: COORD) -> io::Result<Coord> {
48    if return_value.X != 0 && return_value.Y != 0 {
49        Ok(Coord::from(return_value))
50    } else {
51        Err(io::Error::last_os_error())
52    }
53}
54
55/// Get the result of a call to WinAPI that returns a handle or `INVALID_HANDLE_VALUE`.
56#[inline]
57pub fn handle_result(return_value: HANDLE) -> io::Result<HANDLE> {
58    if return_value != INVALID_HANDLE_VALUE {
59        Ok(return_value)
60    } else {
61        Err(io::Error::last_os_error())
62    }
63}
64
65/// Get the result of a call to WinAPI that returns a handle or `NULL`.
66#[inline]
67pub fn nonnull_handle_result(return_value: HANDLE) -> io::Result<HANDLE> {
68    if return_value.is_null() {
69        Err(io::Error::last_os_error())
70    } else {
71        Ok(return_value)
72    }
73}