Browse Source

Simplify ProtoDecodeError conversion to io::Error.

wip
Titouan Rigoudy 5 years ago
parent
commit
97e380eab6
1 changed files with 6 additions and 19 deletions
  1. +6
    -19
      src/proto/base_codec.rs

+ 6
- 19
src/proto/base_codec.rs View File

@ -96,28 +96,15 @@ pub enum ProtoDecodeError {
impl From<ProtoDecodeError> for io::Error { impl From<ProtoDecodeError> for io::Error {
fn from(error: ProtoDecodeError) -> Self { fn from(error: ProtoDecodeError) -> Self {
match &error {
&ProtoDecodeError::NotEnoughData { .. } => unexpected_eof_error(format!("{}", error)),
_ => invalid_data_error(format!("{}", error)),
}
let kind = match &error {
&ProtoDecodeError::NotEnoughData { .. } => io::ErrorKind::UnexpectedEof,
_ => io::ErrorKind::InvalidData,
};
let message = format!("{}", &error);
io::Error::new(kind, message)
} }
} }
/// Builds an UnexpectedEof error with the given message.
fn unexpected_eof_error(message: String) -> io::Error {
io::Error::new(io::ErrorKind::UnexpectedEof, message)
}
/// Builds an InvalidData error with the given message.
fn invalid_data_error(message: String) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, message)
}
/// Annotates the given error with the given context.
fn annotate_error(error: &io::Error, context: &str) -> io::Error {
io::Error::new(error.kind(), format!("{}: {}", context, error))
}
/// A type for decoding various types of values from protocol messages. /// A type for decoding various types of values from protocol messages.
pub struct ProtoDecoder<'a> { pub struct ProtoDecoder<'a> {
// The buffer we are decoding from. // The buffer we are decoding from.


Loading…
Cancel
Save