refactor option write / reads

This commit is contained in:
2025-07-01 16:42:30 +02:00
parent 02aaef1560
commit 87eb32eb5d
9 changed files with 128 additions and 84 deletions

View File

@@ -1,8 +1,13 @@
use std::io::Cursor;
use bytes::{BufMut as _, BytesMut};
use bytes::BytesMut;
use crate::{Result, buffer::try_get_string, connection::Connection, database::Database};
use crate::{
Result,
buffer::{ArchiveBuf as _, ArchiveBufMut},
connection::Connection,
database::Database,
};
#[derive(Debug, Clone)]
pub struct Delete {
@@ -14,22 +19,15 @@ impl Delete {
let value = db.delete(&self.key).await;
let mut buf = BytesMut::new();
match value {
Some(v) => {
buf.put_u8(1);
buf.put_u32(v.len() as u32);
buf.put_slice(&v);
}
None => buf.put_u8(0),
}
buf.put_option(value, ArchiveBufMut::put_bytes_with_length);
connection.write(buf.into()).await?;
Ok(())
}
pub fn parse(bytes: &mut Cursor<&[u8]>) -> Result<Self> {
let key = try_get_string(bytes)?;
pub fn parse(buf: &mut Cursor<&[u8]>) -> Result<Self> {
let key = buf.try_get_string()?;
Ok(Self { key })
}