From 060a3b740833ee86535f76e8099b75a13e0b4465 Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Sat, 12 Feb 2022 23:10:59 +0100 Subject: [PATCH] =?UTF-8?q?Custom=20Error=20type,=20as=20per=20clippy?= =?UTF-8?q?=E2=80=99s=20wishes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index eefe7f4..20338f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,8 +22,21 @@ //! CMD’s support for ANSI’s escaped characters on your Windows builds! And //! 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)] -pub fn try_init() -> Result<(), ()> { +pub fn try_init() -> Result<(), InitError> { use winapi::shared::minwindef::DWORD; use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode}; use winapi::um::processenv::GetStdHandle; @@ -36,13 +49,13 @@ pub fn try_init() -> Result<(), ()> { let mut ret: Result<(), _> = Ok(()); unsafe { if GetConsoleMode(console_out, &mut state) == 0 { - ret = Err(()); + ret = Err(InitError); } if ret.is_ok() { state |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; state &= !DISABLE_NEWLINE_AUTO_RETURN; if SetConsoleMode(console_out, state) == 0 { - ret = Err(()); + ret = Err(InitError); } } } @@ -55,14 +68,12 @@ pub fn init() { } #[cfg(not(windows))] -pub fn try_init() -> Result<(), ()> { +pub fn try_init() -> Result<(), InitError> { Ok(()) } #[cfg(not(windows))] -pub fn init() { - ; -} +pub fn init() {} #[cfg(test)] mod tests {