/// decoders.
pub trait CustomMessageReader {
/// The type of the message decoded by the implementation.
- type CustomMessage: core::fmt::Debug + Type + Writeable;
+ type CustomMessage: Type;
/// Decodes a custom message to `CustomMessageType`. If the given message type is known to the
/// implementation and the message could be decoded, must return `Ok(Some(message))`. If the
/// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
Init(msgs::Init),
Error(msgs::ErrorMessage),
+ Warning(msgs::WarningMessage),
Ping(msgs::Ping),
Pong(msgs::Pong),
OpenChannel(msgs::OpenChannel),
match self {
&Message::Init(ref msg) => msg.type_id(),
&Message::Error(ref msg) => msg.type_id(),
+ &Message::Warning(ref msg) => msg.type_id(),
&Message::Ping(ref msg) => msg.type_id(),
&Message::Pong(ref msg) => msg.type_id(),
&Message::OpenChannel(ref msg) => msg.type_id(),
/// # Errors
///
/// Returns an error if the message payload code not be decoded as the specified type.
-pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(
- buffer: &mut R,
- custom_reader: H,
-) -> Result<Message<T>, msgs::DecodeError>
-where
+pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, custom_reader: H)
+-> Result<Message<T>, (msgs::DecodeError, Option<u16>)> where
+ T: core::fmt::Debug + Type + Writeable,
+ H::Target: CustomMessageReader<CustomMessage = T>,
+{
+ let message_type = <u16 as Readable>::read(buffer).map_err(|e| (e, None))?;
+ do_read(buffer, message_type, custom_reader).map_err(|e| (e, Some(message_type)))
+}
+
+fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u16, custom_reader: H)
+-> Result<Message<T>, msgs::DecodeError> where
T: core::fmt::Debug + Type + Writeable,
H::Target: CustomMessageReader<CustomMessage = T>,
{
- let message_type = <u16 as Readable>::read(buffer)?;
match message_type {
msgs::Init::TYPE => {
Ok(Message::Init(Readable::read(buffer)?))
msgs::ErrorMessage::TYPE => {
Ok(Message::Error(Readable::read(buffer)?))
},
+ msgs::WarningMessage::TYPE => {
+ Ok(Message::Warning(Readable::read(buffer)?))
+ },
msgs::Ping::TYPE => {
Ok(Message::Ping(Readable::read(buffer)?))
},
/// Defines a type identifier for sending messages over the wire.
///
/// Messages implementing this trait specify a type and must be [`Writeable`].
-pub trait Type {
+pub trait Type: core::fmt::Debug + Writeable {
/// Returns the type identifying the message payload.
fn type_id(&self) -> u16;
}
-impl<T> Type for T where T: Encode {
+impl<T: core::fmt::Debug + Writeable> Type for T where T: Encode {
fn type_id(&self) -> u16 {
T::TYPE
}
const TYPE: u16 = 17;
}
+impl Encode for msgs::WarningMessage {
+ const TYPE: u16 = 1;
+}
+
impl Encode for msgs::Ping {
const TYPE: u16 = 18;
}
}
}
+ impl Type for () {
+ fn type_id(&self) -> u16 { unreachable!(); }
+ }
+
#[test]
fn is_even_message_type() {
let message = Message::<()>::Unknown(42);