#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Proto {
#[default]
Resp2,
Resp3,
}
impl Proto {
#[inline]
pub const fn version(self) -> i64 {
match self {
Proto::Resp2 => 2,
Proto::Resp3 => 3,
}
}
#[inline]
pub const fn from_version(v: i64) -> Option<Proto> {
match v {
2 => Some(Proto::Resp2),
3 => Some(Proto::Resp3),
_ => None,
}
}
#[inline]
pub const fn is_resp3(self) -> bool {
matches!(self, Proto::Resp3)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Limits {
pub max_multibulk: usize,
pub max_bulk: usize,
pub max_inline: usize,
pub max_depth: usize,
}
impl Limits {
pub const DEFAULT: Limits = Limits {
max_multibulk: 1024 * 1024,
max_bulk: 512 * 1024 * 1024,
max_inline: 64 * 1024,
max_depth: 128,
};
}
impl Default for Limits {
fn default() -> Limits {
Limits::DEFAULT
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_connection_starts_at_resp2() {
assert_eq!(Proto::default(), Proto::Resp2);
assert!(!Proto::default().is_resp3());
}
#[test]
fn hello_takes_two_and_three_and_nothing_else() {
assert_eq!(Proto::from_version(2), Some(Proto::Resp2));
assert_eq!(Proto::from_version(3), Some(Proto::Resp3));
for v in [-1, 0, 1, 4, 300] {
assert_eq!(Proto::from_version(v), None, "HELLO {v}");
}
for p in [Proto::Resp2, Proto::Resp3] {
assert_eq!(Proto::from_version(p.version()), Some(p));
}
}
#[test]
fn the_limits_are_the_redis_numbers() {
let l = Limits::default();
assert_eq!(l.max_multibulk, 1024 * 1024);
assert_eq!(l.max_bulk, 512 * 1024 * 1024);
assert_eq!(l.max_inline, 64 * 1024);
}
}