1use std::error::Error;
2use std::net::TcpStream;
3
4use packetio::{PacketReceiver, PacketSender};
5use rustacean_db_protocol::{Request, Response};
6
7pub mod blocking {
8 use super::*;
9
10 pub fn get(ip: &str, key: &str) -> Result<Option<Vec<u8>>, Box<dyn Error>> {
11 let mut connection = TcpStream::connect(ip)?;
12 connection.send_packet(Request::Get(key.to_string()))?;
13 let resp: Response = connection.recv_packet()?;
14 match resp {
15 Response::Value(v) => Ok(v),
16 _ => Err("Unexpected response".into()),
17 }
18 }
19
20 pub fn post(ip: &str, key: &str, value: Vec<u8>) -> Result<(), Box<dyn Error>> {
21 let mut connection = TcpStream::connect(ip)?;
22 connection.send_packet(Request::Post(key.to_string(), value))?;
23 let resp: Response = connection.recv_packet()?;
24 match resp {
25 Response::Ack(true) => Ok(()),
26 _ => Err("Server did not acknowledge".into()),
27 }
28 }
29}