use redis::ProtocolVersion;
use std::{convert::TryFrom, time::Duration};
#[derive(Debug)]
pub(crate) struct QueueDescriptor {
pub vt: Duration,
pub delay: Duration,
pub maxsize: i64,
pub ts: u64,
pub uid: Option<String>,
}
#[derive(Debug, Clone)]
pub struct RsmqOptions {
pub host: String,
pub port: u16,
pub db: u8,
pub realtime: bool,
pub username: Option<String>,
pub password: Option<String>,
pub ns: String,
pub protocol: ProtocolVersion,
}
impl Default for RsmqOptions {
fn default() -> Self {
RsmqOptions {
host: "localhost".to_string(),
port: 6379,
db: 0,
realtime: false,
username: None,
password: None,
ns: "rsmq".to_string(),
protocol: ProtocolVersion::RESP2,
}
}
}
#[derive(Debug, Clone)]
pub struct RsmqMessage<T: TryFrom<RedisBytes> = String> {
pub id: String,
pub message: T,
pub rc: u64,
pub fr: u64,
pub sent: u64,
}
#[derive(Debug, Clone)]
pub struct RsmqQueueAttributes {
pub vt: Duration,
pub delay: Duration,
pub maxsize: i64,
pub totalrecv: u64,
pub totalsent: u64,
pub created: u64,
pub modified: u64,
pub msgs: u64,
pub hiddenmsgs: u64,
}
#[derive(Debug)]
pub struct RedisBytes(pub(crate) Vec<u8>);
impl RedisBytes {
pub fn into_bytes(self) -> Vec<u8> {
self.0
}
}
impl TryFrom<RedisBytes> for String {
type Error = Vec<u8>;
fn try_from(bytes: RedisBytes) -> Result<Self, Self::Error> {
String::from_utf8(bytes.0).map_err(|e| e.into_bytes())
}
}
impl TryFrom<RedisBytes> for Vec<u8> {
type Error = Vec<u8>;
fn try_from(bytes: RedisBytes) -> Result<Self, Vec<u8>> {
Ok(bytes.0)
}
}
impl From<String> for RedisBytes {
fn from(t: String) -> RedisBytes {
RedisBytes(t.into())
}
}
impl From<&str> for RedisBytes {
fn from(t: &str) -> RedisBytes {
RedisBytes(t.into())
}
}
impl From<Vec<u8>> for RedisBytes {
fn from(t: Vec<u8>) -> RedisBytes {
RedisBytes(t)
}
}
impl From<&[u8]> for RedisBytes {
fn from(t: &[u8]) -> RedisBytes {
RedisBytes(t.into())
}
}