basic get / set / delete / has

This commit is contained in:
2025-06-12 15:52:01 +02:00
commit 7a6b1b6954
11 changed files with 916 additions and 0 deletions

137
src/client.rs Normal file
View File

@@ -0,0 +1,137 @@
use bytes::{Buf, BufMut as _, Bytes, BytesMut};
use tokio::net::{TcpStream, ToSocketAddrs};
use crate::{Result, connection::Connection, database::Value, errors::AppError};
pub struct Client {
connection: Connection,
}
impl Client {
pub async fn new<Addr: ToSocketAddrs>(addr: Addr) -> Result<Self> {
let socket = TcpStream::connect(addr).await?;
let connection = Connection::new(socket);
Ok(Self { connection })
}
pub async fn get(&mut self, key: &str) -> Result<Option<Bytes>> {
let mut bytes = BytesMut::new();
bytes.put_u16(3);
bytes.put_slice(b"get");
let key_length: u16 = key
.len()
.try_into()
.map_err(|_| AppError::KeyLength(key.len()))?;
bytes.put_u16(key_length);
bytes.put_slice(key.as_bytes());
self.connection.write(bytes.into()).await?;
let mut r = self.get_response().await?;
let response = match r.try_get_u8()? {
0 => None,
1 => {
let len = r.try_get_u32()? as usize;
if r.remaining() < len {
return Err(AppError::InvalidCommandResponse);
}
Some(r.copy_to_bytes(len))
}
_ => return Err(AppError::InvalidCommandResponse),
};
Ok(response)
}
pub async fn set(&mut self, key: &str, value: Value) -> Result<()> {
let mut bytes = BytesMut::new();
bytes.put_u16(3);
bytes.put_slice(b"set");
let key_length: u16 = key
.len()
.try_into()
.map_err(|_| AppError::KeyLength(key.len()))?;
bytes.put_u16(key_length);
bytes.put_slice(key.as_bytes());
value.write_to_bytes(&mut bytes);
self.connection.write(bytes.into()).await?;
let mut r = self.get_response().await?;
match r.try_get_u8()? {
1 => return Ok(()),
_ => return Err(AppError::InvalidCommandResponse),
}
}
pub async fn delete(&mut self, key: &str) -> Result<Option<Bytes>> {
let mut bytes = BytesMut::new();
bytes.put_u16(6);
bytes.put_slice(b"delete");
let key_length: u16 = key
.len()
.try_into()
.map_err(|_| AppError::KeyLength(key.len()))?;
bytes.put_u16(key_length);
bytes.put_slice(key.as_bytes());
self.connection.write(bytes.into()).await?;
let mut r = self.get_response().await?;
let response = match r.try_get_u8()? {
1 => {
let len = r.try_get_u32()?;
let bytes = r.copy_to_bytes(len as usize);
Some(bytes)
}
0 => None,
_ => return Err(AppError::InvalidCommandResponse),
};
Ok(response)
}
pub async fn has(&mut self, key: &str) -> Result<bool> {
let mut bytes = BytesMut::new();
bytes.put_u16(3);
bytes.put_slice(b"has");
let key_length: u16 = key
.len()
.try_into()
.map_err(|_| AppError::KeyLength(key.len()))?;
bytes.put_u16(key_length);
bytes.put_slice(key.as_bytes());
self.connection.write(bytes.into()).await?;
let mut r = self.get_response().await?;
Ok(r.try_get_u8()? == 1)
}
async fn get_response(&mut self) -> Result<Bytes> {
self.connection
.read_bytes()
.await?
.ok_or(AppError::NoResponse)
}
}