diff --git a/src/backend.rs b/src/backend/mod.rs similarity index 92% rename from src/backend.rs rename to src/backend/mod.rs index 85a6b32..427ca32 100644 --- a/src/backend.rs +++ b/src/backend/mod.rs @@ -3,6 +3,7 @@ use dashmap::DashMap; use std::ops::Deref; use std::sync::Arc; +// region: --- Enums and Structs #[derive(Debug, Clone)] pub struct Backend(Arc); @@ -11,7 +12,9 @@ pub struct BackendInner { pub(crate) map: DashMap, pub(crate) hmap: DashMap>, } +// endregion: --- Enums and Structs +// region: --- impls impl Deref for Backend { type Target = BackendInner; @@ -63,3 +66,4 @@ impl Backend { self.hmap.get(key).map(|v| v.clone()) } } +// endregion: --- impls diff --git a/src/cmd/hmap.rs b/src/cmd/hmap.rs index 3a37f11..1642e19 100644 --- a/src/cmd/hmap.rs +++ b/src/cmd/hmap.rs @@ -4,6 +4,10 @@ use super::{ extract_args, validate_command, CommandError, CommandExecutor, HGet, HGetAll, HSet, RESP_OK, }; +// region: --- impls + +// endregion: --- impls + impl CommandExecutor for HGet { fn execute(self, backend: &crate::Backend) -> RespFrame { match backend.hget(&self.key, &self.field) { diff --git a/src/cmd.rs b/src/cmd/mod.rs similarity index 97% rename from src/cmd.rs rename to src/cmd/mod.rs index 5aa8988..207acb0 100644 --- a/src/cmd.rs +++ b/src/cmd/mod.rs @@ -8,6 +8,10 @@ mod hmap; mod map; // you could also use once_cell instead of lazy_static +// lazy_static: +// 1. init in runtime +// 2. thread safe +// 3. improve performance lazy_static! { static ref RESP_OK: RespFrame = SimpleString::new("OK").into(); } diff --git a/src/resp/decode.rs b/src/resp/decode.rs index a511d42..4b29236 100644 --- a/src/resp/decode.rs +++ b/src/resp/decode.rs @@ -9,6 +9,7 @@ use super::{ calc_total_length, extract_fixed_data, extract_simple_frame_data, parse_length, CRLF_LEN, }; +// region: --- impls impl RespDecode for RespFrame { const PREFIX: &'static str = ""; fn decode(buf: &mut BytesMut) -> Result { @@ -324,6 +325,7 @@ impl RespDecode for RespSet { calc_total_length(buf, end, len, Self::PREFIX) } } +// endregion: --- impls #[cfg(test)] mod tests { diff --git a/src/resp/encode.rs b/src/resp/encode.rs index cbba66c..0ab5c1d 100644 --- a/src/resp/encode.rs +++ b/src/resp/encode.rs @@ -10,6 +10,7 @@ use crate::{ const BUF_CAP: usize = 4096; +// region: --- impls // - simple string: "+\r\n" impl RespEncode for SimpleString { fn encode(self) -> Vec { @@ -202,6 +203,7 @@ impl Deref for RespSet { &self.0 } } +// endregion: --- impls // enum_dispatch // 这里因为 enum_dispatch 的原因, 会自动为变体类型生成, From for Enum_Name diff --git a/src/resp.rs b/src/resp/mod.rs similarity index 97% rename from src/resp.rs rename to src/resp/mod.rs index 557cfc3..ba940ec 100644 --- a/src/resp.rs +++ b/src/resp/mod.rs @@ -9,6 +9,7 @@ mod encode; const CRLF: &[u8] = b"\r\n"; const CRLF_LEN: usize = CRLF.len(); +// region: --- Traits #[enum_dispatch] pub trait RespEncode { fn encode(self) -> Vec; @@ -21,7 +22,9 @@ pub trait RespDecode: Sized { fn decode(buf: &mut BytesMut) -> Result; fn expect_length(buf: &[u8]) -> Result; } +// endregion: --- Traits +// region: --- Enum and Structs #[derive(Error, Debug, PartialEq, Eq)] pub enum RespError { // region: --- thiserror format usage @@ -99,6 +102,9 @@ pub struct RespMap(pub(crate) BTreeMap); #[derive(Debug, Clone, PartialEq)] pub struct RespSet(pub(crate) Vec); // 改为 Vec, 用于有序的集合数据 +// endregion: --- Enum and Structs + +// region: --- impls impl SimpleString { pub fn new(s: impl Into) -> Self { SimpleString(s.into()) @@ -176,7 +182,9 @@ impl From<&[u8; N]> for BulkString { BulkString(s.to_vec()) } } +// endregion: --- impls +// region: --- Functions // utility functions fn extract_fixed_data( buf: &mut BytesMut, @@ -265,3 +273,5 @@ fn calc_total_length(buf: &[u8], end: usize, len: usize, prefix: &str) -> Result _ => Ok(len + CRLF_LEN), } } + +// endregion: --- Functions