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::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<u32> 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<Self, Self::Error> {
use self::ContentPurpose::*;
match num {
@ -267,7 +275,7 @@ impl TryFrom<u32> for ContentPurpose {
11 => Ok(Time),
12 => Ok(Datetime),
13 => Ok(Terminal),
_ => Err(()),
_ => Err(UnrecognizedValue),
}
}
}
@ -280,14 +288,12 @@ pub enum ChangeCause {
}
impl TryFrom<u32> 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<Self, Self::Error> {
match num {
0 => Ok(ChangeCause::InputMethod),
1 => Ok(ChangeCause::Other),
_ => Err(())
_ => Err(UnrecognizedValue)
}
}
}