Model HandshakeContext with explicit state transitions. (#532)

Instead of having one struct with uninitialised fields
which are mutated, have explicit states and ensure that the types
show that there is no ambiguity which data is available or not.
Consequently, this removes quite a few `unwrap`/`expect` calls.
This commit is contained in:
Toralf Wittner
2018-10-08 14:37:36 +02:00
committed by GitHub
parent e05422c05f
commit f2c3a149d7
2 changed files with 309 additions and 261 deletions

View File

@ -21,6 +21,7 @@
//! Defines the `SecioError` enum that groups all possible errors in SECIO.
use aes_ctr::stream_cipher::LoopError;
use protobuf::error::ProtobufError;
use std::error;
use std::fmt;
use std::io::Error as IoError;
@ -31,6 +32,9 @@ pub enum SecioError {
/// I/O error.
IoError(IoError),
/// Protocol buffer error.
Protobuf(ProtobufError),
/// Failed to parse one of the handshake protobuf messages.
HandshakeParsingFailure,
@ -66,12 +70,16 @@ pub enum SecioError {
/// We received an invalid proposition from remote.
InvalidProposition(&'static str),
#[doc(hidden)]
__Nonexhaustive
}
impl error::Error for SecioError {
fn cause(&self) -> Option<&error::Error> {
match *self {
SecioError::IoError(ref err) => Some(err),
SecioError::Protobuf(ref err) => Some(err),
// TODO: The type doesn't implement `Error`
/*SecioError::CipherError(ref err) => {
Some(err)
@ -87,6 +95,8 @@ impl fmt::Display for SecioError {
match self {
SecioError::IoError(e) =>
write!(f, "I/O error: {}", e),
SecioError::Protobuf(e) =>
write!(f, "protobuf error: {}", e),
SecioError::HandshakeParsingFailure =>
f.write_str("Failed to parse one of the handshake protobuf messages"),
SecioError::NoSupportIntersection =>
@ -110,7 +120,9 @@ impl fmt::Display for SecioError {
SecioError::HmacNotMatching =>
f.write_str("The hashes of the message didn't match"),
SecioError::InvalidProposition(msg) =>
write!(f, "invalid proposition: {}", msg)
write!(f, "invalid proposition: {}", msg),
SecioError::__Nonexhaustive =>
f.write_str("__Nonexhaustive")
}
}
}
@ -128,3 +140,10 @@ impl From<IoError> for SecioError {
SecioError::IoError(err)
}
}
impl From<ProtobufError> for SecioError {
#[inline]
fn from(err: ProtobufError) -> SecioError {
SecioError::Protobuf(err)
}
}