From cc418c36095bb3edbd62a022467f99bbb3b12737 Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Fri, 17 Jan 2020 11:59:47 +0000 Subject: [PATCH] imservice: Return something more resembling an Error on failure The error type is expected to be printable by logging utilities. --- src/imservice.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/imservice.rs b/src/imservice.rs index d3e4c5d0..2f39476a 100644 --- a/src/imservice.rs +++ b/src/imservice.rs @@ -1,5 +1,6 @@ use std::boxed::Box; use std::ffi::CString; +use std::fmt; use std::num::Wrapping; use std::string::String; @@ -246,10 +247,17 @@ pub enum ContentPurpose { Terminal = 13, } +// Utilities from ::logging need a printable error type +pub struct UnrecognizedValue; + +impl fmt::Display for UnrecognizedValue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Unrecognized value") + } +} + impl TryFrom for ContentPurpose { - // There's only one way to fail: number not in protocol, - // so no special error type is needed - type Error = (); + type Error = UnrecognizedValue; fn try_from(num: u32) -> Result { use self::ContentPurpose::*; match num { @@ -267,7 +275,7 @@ impl TryFrom for ContentPurpose { 11 => Ok(Time), 12 => Ok(Datetime), 13 => Ok(Terminal), - _ => Err(()), + _ => Err(UnrecognizedValue), } } } @@ -280,14 +288,12 @@ pub enum ChangeCause { } impl TryFrom for ChangeCause { - // There's only one way to fail: number not in protocol, - // so no special error type is needed - type Error = (); + type Error = UnrecognizedValue; fn try_from(num: u32) -> Result { match num { 0 => Ok(ChangeCause::InputMethod), 1 => Ok(ChangeCause::Other), - _ => Err(()) + _ => Err(UnrecognizedValue) } } }