use std::io::Cursor; use bytes::{Buf as _, BufMut as _, Bytes, BytesMut}; use crate::{ Result, buffer::{ArchiveBuf as _, ArchiveBufMut as _}, connection::Connection, database::Database, }; #[derive(Debug, Clone)] pub struct Expire { key: String, seconds: u64, } impl Expire { pub fn new(key: String, seconds: u64) -> Self { Self { key, seconds } } pub async fn execute(self, db: &Database, connection: &mut Connection) -> Result<()> { let success = db.expire(&self.key, self.seconds).await?; connection .write(Bytes::from_static(if success { &[1] } else { &[0] })) .await?; Ok(()) } pub fn parse(buf: &mut Cursor<&[u8]>) -> Result { let key = buf.try_get_short_string()?; let seconds = buf.try_get_u64()?; Ok(Self { key, seconds }) } pub fn put(&self, buf: &mut BytesMut) -> Result<()> { buf.put_short_string("expire")?; self.put_without_cmd_name(buf) } pub fn put_without_cmd_name(&self, buf: &mut BytesMut) -> Result<()> { buf.put_short_string(&self.key)?; buf.put_u64(self.seconds); Ok(()) } }