use std::fmt::{self, Display, Formatter};
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub enum MessageType {
Query,
Response,
}
impl MessageType {
#[inline]
pub fn as_str(self) -> &'static str {
match self {
MessageType::Query => "Query",
MessageType::Response => "Response",
}
}
#[inline]
pub fn is_query(self) -> bool {
matches!(self, MessageType::Query)
}
#[inline]
pub fn is_response(self) -> bool {
matches!(self, MessageType::Response)
}
}
impl From<bool> for MessageType {
fn from(value: bool) -> Self {
if value {
Self::Response
} else {
Self::Query
}
}
}
impl From<MessageType> for bool {
fn from(mt: MessageType) -> Self {
match mt {
MessageType::Query => false,
MessageType::Response => true,
}
}
}
impl Display for MessageType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.pad(self.as_str())
}
}