sierradb_server/request/
hello.rs1use combine::Parser;
2use indexmap::indexmap;
3use libp2p::PeerId;
4use redis_protocol::resp3::types::BytesFrame;
5
6use crate::parser::{FrameStream, number_i64};
7use crate::request::{HandleRequest, blob_str, map, number};
8use crate::server::Conn;
9
10pub struct Hello {
11 pub version: i64,
12}
13
14impl Hello {
15 pub fn parser<'a>() -> impl Parser<FrameStream<'a>, Output = Hello> + 'a {
16 number_i64().map(|version| Hello { version })
17 }
18}
19
20impl HandleRequest for Hello {
21 type Error = String;
22 type Ok = HelloResp;
23
24 async fn handle_request(self, conn: &mut Conn) -> Result<Option<Self::Ok>, Self::Error> {
25 if self.version != 3 {
26 return Err(format!(
27 "unsupported version {}, only 3 is supported",
28 self.version
29 ));
30 }
31
32 Ok(Some(HelloResp {
33 server: "sierradb",
34 version: env!("CARGO_PKG_VERSION"),
35 peer_id: *conn.cluster_ref.id().peer_id().unwrap(),
36 num_partitions: conn.num_partitions,
37 }))
38 }
39}
40
41pub struct HelloResp {
42 server: &'static str,
43 version: &'static str,
44 peer_id: PeerId,
45 num_partitions: u16,
46}
47
48impl From<HelloResp> for BytesFrame {
49 fn from(resp: HelloResp) -> Self {
50 map(indexmap! {
51 blob_str("server") => blob_str(resp.server),
52 blob_str("version") => blob_str(resp.version),
53 blob_str("peer_id") => blob_str(resp.peer_id.to_base58()),
54 blob_str("num_partitions") => number(resp.num_partitions.into()),
55 })
56 }
57}