use std::io::Cursor; use crate::{Result, connection::Connection, database::Database, errors::AppError}; use bytes::{Buf as _, Bytes}; #[derive(Debug, Clone)] pub struct Set { key: String, data: Box<[u8]>, expiration: Option, } impl Set { pub async fn execute(self, db: &Database, connection: &mut Connection) -> Result<()> { db.set(self.key, self.data, self.expiration).await?; connection.write(Bytes::from_static(&[1])).await?; Ok(()) } pub fn parse(bytes: &mut Cursor<&[u8]>) -> Result { let key_length = bytes.try_get_u16()? as usize; if bytes.remaining() < key_length { return Err(AppError::IncompleteCommandBuffer); } let key = String::from_utf8(bytes.copy_to_bytes(key_length).to_vec())?; let value_length = bytes.try_get_u32()? as usize; if bytes.remaining() < value_length { return Err(AppError::IncompleteCommandBuffer); } let data = bytes.copy_to_bytes(value_length); let expiration: Option = match bytes.try_get_u8()? { 1 => Some(bytes.try_get_u64()?), 0 => None, _ => return Err(AppError::UnexpectedCommandData), }; Ok(Self { key, data: (*data).into(), expiration, }) } }