Browse Source

Add packet enums

ipv6
Denis Drakhnia 1 year ago
parent
commit
ce742f091d
  1. 20
      protocol/src/admin.rs
  2. 5
      protocol/src/cursor.rs
  3. 25
      protocol/src/master.rs

20
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<Self, Error> {
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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

5
protocol/src/cursor.rs

@ -323,10 +323,7 @@ macro_rules! impl_put {
impl<'a> CursorMut<'a> { impl<'a> CursorMut<'a> {
pub fn new(buffer: &'a mut [u8]) -> Self { pub fn new(buffer: &'a mut [u8]) -> Self {
let (buffer, buffer_mut) = buffer.split_at_mut(0); let (buffer, buffer_mut) = buffer.split_at_mut(0);
Self { Self { buffer, buffer_mut }
buffer,
buffer_mut,
}
} }
pub fn buffer(&self) -> &'a [u8] { pub fn buffer(&self) -> &'a [u8] {

25
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<Self, Error> {
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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

Loading…
Cancel
Save