diff --git a/protocol/src/admin.rs b/protocol/src/admin.rs index f7738a3..f2b7a68 100644 --- a/protocol/src/admin.rs +++ b/protocol/src/admin.rs @@ -60,6 +60,26 @@ impl<'a> AdminCommand<'a> { } } +#[derive(Clone, Debug, PartialEq)] +pub enum Packet<'a> { + AdminChallenge(AdminChallenge), + AdminCommand(AdminCommand<'a>), +} + +impl<'a> Packet<'a> { + pub fn decode(src: &'a [u8]) -> Result { + if let Ok(p) = AdminChallenge::decode(src) { + return Ok(Self::AdminChallenge(p)); + } + + if let Ok(p) = AdminCommand::decode(src) { + return Ok(Self::AdminCommand(p)); + } + + Err(Error::InvalidPacket) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/protocol/src/cursor.rs b/protocol/src/cursor.rs index 825b74a..8e7f0bb 100644 --- a/protocol/src/cursor.rs +++ b/protocol/src/cursor.rs @@ -323,10 +323,7 @@ macro_rules! impl_put { impl<'a> CursorMut<'a> { pub fn new(buffer: &'a mut [u8]) -> Self { let (buffer, buffer_mut) = buffer.split_at_mut(0); - Self { - buffer, - buffer_mut, - } + Self { buffer, buffer_mut } } pub fn buffer(&self) -> &'a [u8] { diff --git a/protocol/src/master.rs b/protocol/src/master.rs index 02276bb..86787ba 100644 --- a/protocol/src/master.rs +++ b/protocol/src/master.rs @@ -133,6 +133,31 @@ impl AdminChallengeResponse { } } +#[derive(Clone, Debug, PartialEq)] +pub enum Packet<'a> { + ChallengeResponse(ChallengeResponse), + QueryServersResponse(QueryServersResponse<&'a [u8]>), + AdminChallengeResponse(AdminChallengeResponse), +} + +impl<'a> Packet<'a> { + pub fn decode(src: &'a [u8]) -> Result { + if let Ok(p) = ChallengeResponse::decode(src) { + return Ok(Self::ChallengeResponse(p)); + } + + if let Ok(p) = QueryServersResponse::decode(src) { + return Ok(Self::QueryServersResponse(p)); + } + + if let Ok(p) = AdminChallengeResponse::decode(src) { + return Ok(Self::AdminChallengeResponse(p)); + } + + Err(Error::InvalidPacket) + } +} + #[cfg(test)] mod tests { use super::*;