Skip to content

Commit 6bdcf0b

Browse files
committed
fix: warning
1 parent 69a9572 commit 6bdcf0b

File tree

8 files changed

+85
-105
lines changed

8 files changed

+85
-105
lines changed

src/bin/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub async fn execute(command: &str, client: &mut DoreaClient) -> (NetPacketState
203203
);
204204
}
205205

206-
return match client.get(slice.get(0).unwrap()).await {
206+
return match client.get(slice.first().unwrap()).await {
207207
Some(v) => (NetPacketState::OK, v.to_string()),
208208
None => (NetPacketState::ERR, "Value not found.".to_string()),
209209
};
@@ -272,7 +272,7 @@ pub async fn execute(command: &str, client: &mut DoreaClient) -> (NetPacketState
272272
}
273273

274274
// 子命令和目标 key
275-
let sub = slice.get(0).unwrap();
275+
let sub = slice.first().unwrap();
276276
let key = slice.get(1).unwrap();
277277

278278
if sub.to_uppercase() == "STRINGIFY" {

src/bin/server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ async fn main() {
8080
| |__| | | |__| | | | \\ \\ | |____ / ____ \\
8181
|_____/ \\____/ |_| \\_\\ |______| /_/ \\_\\
8282
83-
「 Dorea:{} 」server address: {}
83+
「 Dorea:{} 」server address: dorea://{}:{}
8484
",
8585
crate::DOREA_VERSION,
86-
format!("dorea://{}:{}", hostname, port)
86+
hostname,
87+
port,
8788
);
8889

8990
// 生成服务器实例

src/client.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl DoreaClient {
133133
}
134134

135135
pub async fn info(&mut self, dtype: InfoType) -> crate::Result<String> {
136-
let command = format!("info {}", dtype.to_string());
136+
let command = format!("info {}", dtype);
137137

138138
let v = self.execute(&command).await?;
139139
if v.0 == NetPacketState::OK {
@@ -180,22 +180,17 @@ pub enum InfoType {
180180
// DataType(String),
181181
}
182182

183-
impl std::string::ToString for InfoType {
184-
fn to_string(&self) -> String {
183+
impl std::fmt::Display for InfoType {
184+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
185185
match self {
186-
InfoType::CurrentDataBase => "current",
187-
InfoType::MaxConnectionNumber => "max-connect-num",
188-
InfoType::CurrentConnectionNumber => "current-connect-num",
189-
InfoType::PreloadDatabaseList => "preload-db-list",
190-
InfoType::ServerStartupTime => "server-startup-time",
191-
InfoType::ServerVersion => "version",
192-
InfoType::TotalIndexNumber => "total-index-number",
193-
InfoType::KeyList => "keys",
194-
// InfoType::DataType(v) => {
195-
// format!("${} type", v).as_str()
196-
// },
197-
// _ => "", /* 预留数据 */
186+
InfoType::CurrentDataBase => write!(f, "current"),
187+
InfoType::MaxConnectionNumber => write!(f, "max-connect-num"),
188+
InfoType::CurrentConnectionNumber => write!(f, "current-connect-num"),
189+
InfoType::PreloadDatabaseList => write!(f, "preload-db-list"),
190+
InfoType::ServerStartupTime => write!(f, "server-startup-time"),
191+
InfoType::ServerVersion => write!(f, "version"),
192+
InfoType::TotalIndexNumber => write!(f, "total-index-number"),
193+
InfoType::KeyList => write!(f, "keys"),
198194
}
199-
.to_string()
200195
}
201196
}

src/command.rs

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ pub enum CommandList {
4141
UNKNOWN,
4242
}
4343

44-
impl std::string::ToString for CommandList {
45-
fn to_string(&self) -> String {
46-
return format!("{:?}", self);
44+
impl std::fmt::Display for CommandList {
45+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46+
write!(f, "{:?}", self)
4747
}
4848
}
4949

@@ -112,7 +112,7 @@ impl CommandManager {
112112

113113
let mut slice: Vec<&str> = message.split(' ').collect();
114114

115-
let command_str = match slice.get(0) {
115+
let command_str = match slice.first() {
116116
Some(v) => v,
117117
None => "unknown",
118118
};
@@ -177,7 +177,7 @@ impl CommandManager {
177177

178178
// log in to dorea db [AUTH]
179179
if command == CommandList::AUTH {
180-
let input_password = slice.get(0).unwrap();
180+
let input_password = slice.first().unwrap();
181181

182182
let local_password = &config.connection.connection_password;
183183

@@ -199,7 +199,7 @@ impl CommandManager {
199199
}
200200

201201
if command == CommandList::SET {
202-
let key = slice.get(0).unwrap();
202+
let key = slice.first().unwrap();
203203
let value = slice.get(1).unwrap();
204204

205205
let data_value = DataValue::from(value);
@@ -258,7 +258,7 @@ impl CommandManager {
258258
}
259259

260260
if command == CommandList::GET {
261-
let key = slice.get(0).unwrap().to_string();
261+
let key = slice.first().unwrap().to_string();
262262

263263
// 为读取增加 1 的权重
264264
database_manager
@@ -281,7 +281,7 @@ impl CommandManager {
281281
// 惰性删除数据
282282
let exp = v.timestamp();
283283
let current_time = chrono::Local::now().timestamp() as u64;
284-
if current_time >= (exp.0 as u64 + exp.1) as u64 && exp.1 != 0 {
284+
if current_time >= (exp.0 as u64 + exp.1) && exp.1 != 0 {
285285
let _ = database_manager
286286
.lock()
287287
.await
@@ -308,7 +308,7 @@ impl CommandManager {
308308
}
309309

310310
if command == CommandList::DELETE {
311-
let key = slice.get(0).unwrap();
311+
let key = slice.first().unwrap();
312312

313313
// 为删除数据增加 5 的权重
314314
database_manager
@@ -355,7 +355,7 @@ impl CommandManager {
355355
}
356356

357357
if command == CommandList::SELECT {
358-
let db_name = slice.get(0).unwrap();
358+
let db_name = slice.first().unwrap();
359359

360360
// 将当前使用的库加入到 DB统计 中(防止被动态卸载)
361361
crate::server::db_stat_set(*connect_id, db_name.to_string()).await;
@@ -378,7 +378,7 @@ impl CommandManager {
378378
// @key 数据内部信息获取
379379

380380
if command == CommandList::INFO {
381-
let argument: &str = slice.get(0).unwrap();
381+
let argument: &str = slice.first().unwrap();
382382

383383
if argument == "current" {
384384
return (NetPacketState::OK, current.as_bytes().to_vec());
@@ -489,7 +489,7 @@ impl CommandManager {
489489
);
490490
}
491491

492-
let sub_info: &str = sub_arg.get(0).unwrap_or(&"");
492+
let sub_info: &str = sub_arg.first().unwrap_or(&"");
493493
let mut _result: String = format!("{:?}", data);
494494

495495
if sub_info == "expire" {
@@ -519,7 +519,7 @@ impl CommandManager {
519519
// sort 对数组进行排序(仅支持 list )
520520
// reverse 对数组进行反转(仅支持 list )
521521
if command == CommandList::EDIT {
522-
let key: &str = slice.get(0).unwrap();
522+
let key: &str = slice.first().unwrap();
523523
let operation: &str = slice.get(1).unwrap();
524524

525525
if &key[0..1] == "@" {
@@ -600,7 +600,7 @@ impl CommandManager {
600600
let mut incr_num = 1;
601601

602602
if sub_arg.len() == 1 {
603-
let number: &str = sub_arg.get(0).unwrap();
603+
let number: &str = sub_arg.first().unwrap();
604604
incr_num = number.parse::<i32>().unwrap_or(1);
605605
}
606606

@@ -613,7 +613,7 @@ impl CommandManager {
613613
);
614614
}
615615

616-
let data: &str = sub_arg.get(0).unwrap();
616+
let data: &str = sub_arg.first().unwrap();
617617

618618
match &data[0..1] {
619619
"+" => {
@@ -659,7 +659,7 @@ impl CommandManager {
659659
);
660660
}
661661

662-
let data: &str = sub_arg.get(0).unwrap();
662+
let data: &str = sub_arg.first().unwrap();
663663
let mut idx: &str = "";
664664

665665
if sub_arg.len() == 2 {
@@ -688,7 +688,7 @@ impl CommandManager {
688688
);
689689
}
690690

691-
let key = sub_arg.get(0).unwrap();
691+
let key = sub_arg.first().unwrap();
692692

693693
_result = edit_operation::remove(origin_value, key.to_string());
694694
} else if operation == "push" {
@@ -702,7 +702,7 @@ impl CommandManager {
702702
);
703703
}
704704

705-
let data = sub_arg.get(0).unwrap();
705+
let data = sub_arg.first().unwrap();
706706

707707
let data_val = DataValue::from(data);
708708

@@ -738,19 +738,13 @@ impl CommandManager {
738738
);
739739
}
740740

741-
let asc: bool;
742-
743741
// 检查排序方式
744-
if !sub_arg.is_empty() {
745-
let temp: &str = sub_arg.get(0).unwrap_or(&"asc");
746-
if temp.to_uppercase() == "DESC" {
747-
asc = false;
748-
} else {
749-
asc = true;
750-
}
742+
let asc = if !sub_arg.is_empty() {
743+
let temp: &str = sub_arg.first().unwrap_or(&"asc");
744+
temp.to_uppercase() != "DESC"
751745
} else {
752-
asc = true;
753-
}
746+
true
747+
};
754748

755749
_result = edit_operation::sort(origin_value, asc);
756750
} else if operation == "reverse" {
@@ -798,7 +792,7 @@ impl CommandManager {
798792
}
799793

800794
if command == CommandList::VALUE {
801-
let operation: &str = slice.get(0).unwrap();
795+
let operation: &str = slice.first().unwrap();
802796

803797
if operation == "style" {
804798
if slice.len() < 2 {
@@ -824,7 +818,7 @@ impl CommandManager {
824818
}
825819

826820
if command == CommandList::DB {
827-
let operation: &str = slice.get(0).unwrap();
821+
let operation: &str = slice.first().unwrap();
828822

829823
if operation == "unload" {
830824
if slice.len() != 2 {
@@ -914,10 +908,7 @@ impl CommandManager {
914908
);
915909
} else if operation == "num" {
916910
let size = database_manager.lock().await.db_list.len();
917-
return (
918-
NetPacketState::OK,
919-
format!("{}", size).as_bytes().to_vec(),
920-
);
911+
return (NetPacketState::OK, format!("{}", size).as_bytes().to_vec());
921912
} else if operation == "lock" {
922913
if slice.len() != 2 {
923914
return (
@@ -1028,7 +1019,7 @@ impl CommandManager {
10281019
);
10291020
}
10301021

1031-
let target: &str = slice.get(0).unwrap();
1022+
let target: &str = slice.first().unwrap();
10321023

10331024
if target.to_uppercase() == "SERVICE" {
10341025
return (
@@ -1078,7 +1069,7 @@ impl CommandManager {
10781069
}
10791070

10801071
if command == CommandList::SERVICE {
1081-
let operation: &str = slice.get(0).unwrap();
1072+
let operation: &str = slice.first().unwrap();
10821073

10831074
if operation == "account" || operation == "acc" {
10841075
if !database_manager.lock().await.db_list.contains_key("system")
@@ -1133,7 +1124,7 @@ impl CommandManager {
11331124
let password: &str = slice.get(3).unwrap();
11341125

11351126
// if username is `master`, we cannot register it.
1136-
// because this name is a
1127+
// because this name is a
11371128
if username == "master" {
11381129
return (
11391130
NetPacketState::ERR,
@@ -1301,7 +1292,7 @@ impl CommandManager {
13011292

13021293
// 暂时不支持具体内容查询(这玩意我确实无法进行设计qwq)
13031294
if command == CommandList::SEARCH {
1304-
let expression: &str = slice.get(0).unwrap();
1295+
let expression: &str = slice.first().unwrap();
13051296

13061297
let mut limit = 0;
13071298

@@ -1336,10 +1327,10 @@ impl CommandManager {
13361327
}
13371328

13381329
// unknown operation.
1339-
return (
1330+
(
13401331
NetPacketState::ERR,
13411332
"Unknown operation.".as_bytes().to_vec(),
1342-
);
1333+
)
13431334
}
13441335
}
13451336

src/configure.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::Result;
22

33
use serde::{Deserialize, Serialize};
4-
use std::{fs, path::{PathBuf, Path}};
4+
use std::{
5+
fs,
6+
path::{Path, PathBuf},
7+
};
58

69
/// Dorea File Config Struct
710
#[derive(Debug, Serialize, Deserialize, Clone)]
@@ -114,7 +117,7 @@ fn init_config(path: PathBuf) -> Result<()> {
114117

115118
let service_path = &path.parent().unwrap().to_path_buf();
116119

117-
fs::write(&service_path.join("service.toml"), rest)?;
120+
fs::write(service_path.join("service.toml"), rest)?;
118121

119122
Ok(())
120123
}

0 commit comments

Comments
 (0)