use crate::error::{Result, fmt};
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MsgKind {
QueryRequest = 0x10,
ResultBatch = 0x11,
ResultEnd = 0x12,
QueryError = 0x13,
Cancel = 0x14,
Credit = 0x15,
ExecDone = 0x16,
CacheReset = 0x17,
ServerInfo = 0x18,
}
impl MsgKind {
pub fn from_u8(byte: u8) -> Result<Self> {
Ok(match byte {
0x10 => MsgKind::QueryRequest,
0x11 => MsgKind::ResultBatch,
0x12 => MsgKind::ResultEnd,
0x13 => MsgKind::QueryError,
0x14 => MsgKind::Cancel,
0x15 => MsgKind::Credit,
0x16 => MsgKind::ExecDone,
0x17 => MsgKind::CacheReset,
0x18 => MsgKind::ServerInfo,
other => return Err(fmt!(ProtocolError, "unknown msg_kind 0x{:02X}", other)),
})
}
pub fn as_u8(self) -> u8 {
self as u8
}
}
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum StatusCode {
SchemaMismatch = 0x03,
ParseError = 0x05,
InternalError = 0x06,
SecurityError = 0x08,
Cancelled = 0x0A,
LimitExceeded = 0x0B,
}
impl StatusCode {
pub fn from_u8(byte: u8) -> Result<Self> {
Ok(match byte {
0x03 => StatusCode::SchemaMismatch,
0x05 => StatusCode::ParseError,
0x06 => StatusCode::InternalError,
0x08 => StatusCode::SecurityError,
0x0A => StatusCode::Cancelled,
0x0B => StatusCode::LimitExceeded,
other => {
return Err(fmt!(
ProtocolError,
"unknown QWP status code 0x{:02X}",
other
));
}
})
}
pub fn as_u8(self) -> u8 {
self as u8
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn msg_kind_roundtrip() {
for &k in &[
MsgKind::QueryRequest,
MsgKind::ResultBatch,
MsgKind::ResultEnd,
MsgKind::QueryError,
MsgKind::Cancel,
MsgKind::Credit,
MsgKind::ExecDone,
MsgKind::CacheReset,
MsgKind::ServerInfo,
] {
let b = k.as_u8();
assert_eq!(MsgKind::from_u8(b).unwrap(), k);
}
}
#[test]
fn unknown_msg_kind_rejected() {
assert!(MsgKind::from_u8(0x00).is_err());
assert!(MsgKind::from_u8(0xFF).is_err());
assert!(MsgKind::from_u8(0x09).is_err());
}
#[test]
fn status_code_roundtrip() {
for &s in &[
StatusCode::SchemaMismatch,
StatusCode::ParseError,
StatusCode::InternalError,
StatusCode::SecurityError,
StatusCode::Cancelled,
StatusCode::LimitExceeded,
] {
assert_eq!(StatusCode::from_u8(s.as_u8()).unwrap(), s);
}
}
}