Skip to content

Commit 7d42d6b

Browse files
committed
refactor: project layout
1 parent bb506e3 commit 7d42d6b

File tree

6 files changed

+26
-0
lines changed

6 files changed

+26
-0
lines changed

src/backend.rs renamed to src/backend/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use dashmap::DashMap;
33
use std::ops::Deref;
44
use std::sync::Arc;
55

6+
// region: --- Enums and Structs
67
#[derive(Debug, Clone)]
78
pub struct Backend(Arc<BackendInner>);
89

@@ -11,7 +12,9 @@ pub struct BackendInner {
1112
pub(crate) map: DashMap<String, RespFrame>,
1213
pub(crate) hmap: DashMap<String, DashMap<String, RespFrame>>,
1314
}
15+
// endregion: --- Enums and Structs
1416

17+
// region: --- impls
1518
impl Deref for Backend {
1619
type Target = BackendInner;
1720

@@ -63,3 +66,4 @@ impl Backend {
6366
self.hmap.get(key).map(|v| v.clone())
6467
}
6568
}
69+
// endregion: --- impls

src/cmd/hmap.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use super::{
44
extract_args, validate_command, CommandError, CommandExecutor, HGet, HGetAll, HSet, RESP_OK,
55
};
66

7+
// region: --- impls
8+
9+
// endregion: --- impls
10+
711
impl CommandExecutor for HGet {
812
fn execute(self, backend: &crate::Backend) -> RespFrame {
913
match backend.hget(&self.key, &self.field) {

src/cmd.rs renamed to src/cmd/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ mod hmap;
88
mod map;
99

1010
// you could also use once_cell instead of lazy_static
11+
// lazy_static:
12+
// 1. init in runtime
13+
// 2. thread safe
14+
// 3. improve performance
1115
lazy_static! {
1216
static ref RESP_OK: RespFrame = SimpleString::new("OK").into();
1317
}

src/resp/decode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::{
99
calc_total_length, extract_fixed_data, extract_simple_frame_data, parse_length, CRLF_LEN,
1010
};
1111

12+
// region: --- impls
1213
impl RespDecode for RespFrame {
1314
const PREFIX: &'static str = "";
1415
fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
@@ -324,6 +325,7 @@ impl RespDecode for RespSet {
324325
calc_total_length(buf, end, len, Self::PREFIX)
325326
}
326327
}
328+
// endregion: --- impls
327329

328330
#[cfg(test)]
329331
mod tests {

src/resp/encode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010

1111
const BUF_CAP: usize = 4096;
1212

13+
// region: --- impls
1314
// - simple string: "+<string>\r\n"
1415
impl RespEncode for SimpleString {
1516
fn encode(self) -> Vec<u8> {
@@ -202,6 +203,7 @@ impl Deref for RespSet {
202203
&self.0
203204
}
204205
}
206+
// endregion: --- impls
205207

206208
// enum_dispatch
207209
// 这里因为 enum_dispatch 的原因, 会自动为变体类型生成, From<xxx> for Enum_Name

src/resp.rs renamed to src/resp/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod encode;
99
const CRLF: &[u8] = b"\r\n";
1010
const CRLF_LEN: usize = CRLF.len();
1111

12+
// region: --- Traits
1213
#[enum_dispatch]
1314
pub trait RespEncode {
1415
fn encode(self) -> Vec<u8>;
@@ -21,7 +22,9 @@ pub trait RespDecode: Sized {
2122
fn decode(buf: &mut BytesMut) -> Result<Self, RespError>;
2223
fn expect_length(buf: &[u8]) -> Result<usize, RespError>;
2324
}
25+
// endregion: --- Traits
2426

27+
// region: --- Enum and Structs
2528
#[derive(Error, Debug, PartialEq, Eq)]
2629
pub enum RespError {
2730
// region: --- thiserror format usage
@@ -99,6 +102,9 @@ pub struct RespMap(pub(crate) BTreeMap<String, RespFrame>);
99102
#[derive(Debug, Clone, PartialEq)]
100103
pub struct RespSet(pub(crate) Vec<RespFrame>); // 改为 Vec, 用于有序的集合数据
101104

105+
// endregion: --- Enum and Structs
106+
107+
// region: --- impls
102108
impl SimpleString {
103109
pub fn new(s: impl Into<String>) -> Self {
104110
SimpleString(s.into())
@@ -176,7 +182,9 @@ impl<const N: usize> From<&[u8; N]> for BulkString {
176182
BulkString(s.to_vec())
177183
}
178184
}
185+
// endregion: --- impls
179186

187+
// region: --- Functions
180188
// utility functions
181189
fn extract_fixed_data(
182190
buf: &mut BytesMut,
@@ -265,3 +273,5 @@ fn calc_total_length(buf: &[u8], end: usize, len: usize, prefix: &str) -> Result
265273
_ => Ok(len + CRLF_LEN),
266274
}
267275
}
276+
277+
// endregion: --- Functions

0 commit comments

Comments
 (0)