Custom Error type, as per clippy’s wishes

This commit is contained in:
Lucien Cartier-Tilet 2022-02-12 23:10:59 +01:00
parent 2611597b65
commit 060a3b7408
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
1 changed files with 18 additions and 7 deletions

View File

@ -22,8 +22,21 @@
//! CMDs support for ANSIs escaped characters on your Windows builds! And //! CMDs support for ANSIs escaped characters on your Windows builds! And
//! you can leave this line in your Unix builds too, it will simply do nothing. //! you can leave this line in your Unix builds too, it will simply do nothing.
use std::fmt;
#[derive(Debug)]
pub struct InitError;
impl fmt::Display for InitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "output-vt100-rs: Could not initialize")
}
}
impl std::error::Error for InitError {}
#[cfg(windows)] #[cfg(windows)]
pub fn try_init() -> Result<(), ()> { pub fn try_init() -> Result<(), InitError> {
use winapi::shared::minwindef::DWORD; use winapi::shared::minwindef::DWORD;
use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode}; use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
use winapi::um::processenv::GetStdHandle; use winapi::um::processenv::GetStdHandle;
@ -36,13 +49,13 @@ pub fn try_init() -> Result<(), ()> {
let mut ret: Result<(), _> = Ok(()); let mut ret: Result<(), _> = Ok(());
unsafe { unsafe {
if GetConsoleMode(console_out, &mut state) == 0 { if GetConsoleMode(console_out, &mut state) == 0 {
ret = Err(()); ret = Err(InitError);
} }
if ret.is_ok() { if ret.is_ok() {
state |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; state |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
state &= !DISABLE_NEWLINE_AUTO_RETURN; state &= !DISABLE_NEWLINE_AUTO_RETURN;
if SetConsoleMode(console_out, state) == 0 { if SetConsoleMode(console_out, state) == 0 {
ret = Err(()); ret = Err(InitError);
} }
} }
} }
@ -55,14 +68,12 @@ pub fn init() {
} }
#[cfg(not(windows))] #[cfg(not(windows))]
pub fn try_init() -> Result<(), ()> { pub fn try_init() -> Result<(), InitError> {
Ok(()) Ok(())
} }
#[cfg(not(windows))] #[cfg(not(windows))]
pub fn init() { pub fn init() {}
;
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {