imservice: Return something more resembling an Error on failure

The error type is expected to be printable by logging utilities.
This commit is contained in:
Dorota Czaplejewicz
2020-01-17 11:59:47 +00:00
parent ea84f4f031
commit cc418c3609

View File

@ -1,5 +1,6 @@
use std::boxed::Box; use std::boxed::Box;
use std::ffi::CString; use std::ffi::CString;
use std::fmt;
use std::num::Wrapping; use std::num::Wrapping;
use std::string::String; use std::string::String;
@ -246,10 +247,17 @@ pub enum ContentPurpose {
Terminal = 13, 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<u32> for ContentPurpose { impl TryFrom<u32> for ContentPurpose {
// There's only one way to fail: number not in protocol, type Error = UnrecognizedValue;
// so no special error type is needed
type Error = ();
fn try_from(num: u32) -> Result<Self, Self::Error> { fn try_from(num: u32) -> Result<Self, Self::Error> {
use self::ContentPurpose::*; use self::ContentPurpose::*;
match num { match num {
@ -267,7 +275,7 @@ impl TryFrom<u32> for ContentPurpose {
11 => Ok(Time), 11 => Ok(Time),
12 => Ok(Datetime), 12 => Ok(Datetime),
13 => Ok(Terminal), 13 => Ok(Terminal),
_ => Err(()), _ => Err(UnrecognizedValue),
} }
} }
} }
@ -280,14 +288,12 @@ pub enum ChangeCause {
} }
impl TryFrom<u32> for ChangeCause { impl TryFrom<u32> for ChangeCause {
// There's only one way to fail: number not in protocol, type Error = UnrecognizedValue;
// so no special error type is needed
type Error = ();
fn try_from(num: u32) -> Result<Self, Self::Error> { fn try_from(num: u32) -> Result<Self, Self::Error> {
match num { match num {
0 => Ok(ChangeCause::InputMethod), 0 => Ok(ChangeCause::InputMethod),
1 => Ok(ChangeCause::Other), 1 => Ok(ChangeCause::Other),
_ => Err(()) _ => Err(UnrecognizedValue)
} }
} }
} }