Skip to content

Commit 7ad0ca0

Browse files
committed
Apply clippy suggestions for Error::other and format string interpolation
1 parent 77fb4f9 commit 7ad0ca0

File tree

13 files changed

+62
-66
lines changed

13 files changed

+62
-66
lines changed

src/config/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::HashMap;
2-
use std::io::{Error, ErrorKind};
2+
use std::io::Error;
33
use std::path::Path;
44
use std::sync::OnceLock;
55

@@ -124,7 +124,7 @@ pub fn init_configs (cmd : Cmd) -> std::io::Result<()> {
124124
Ok(())
125125
}
126126
Err(e) => {
127-
Err(Error::new(ErrorKind::Other,e))
127+
Err(Error::other(e))
128128
}
129129
}
130130
}
@@ -160,12 +160,12 @@ fn open_config_file(path : &str) -> std::io::Result<ServerConfig> {
160160
Ok(config)
161161
}
162162
Err(e) => {
163-
Err(Error::new(ErrorKind::Other,e))
163+
Err(Error::other(e))
164164
}
165165
}
166166
}
167167
Err(e) => {
168-
Err(Error::new(ErrorKind::Other,e))
168+
Err(Error::other(e))
169169
}
170170
}
171171
}

src/database/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::io::{Error, ErrorKind};
1+
use std::io::Error;
22
use std::sync::Arc;
33
use log::info;
44
use tokio::sync::Mutex;
@@ -59,7 +59,7 @@ pub fn init () -> std::io::Result<Arc<Mutex<dyn Database + Send>>> {
5959
Ok(Arc::new(Mutex::new(NoneDB)))
6060
}
6161
_ => {
62-
Err(Error::new(ErrorKind::Other,"Invalid database type."))
62+
Err(Error::other("Invalid database type."))
6363
}
6464
}
6565
}

src/database/mysql.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::io::{Error, ErrorKind};
1+
use std::io::Error;
22
use mysql::{Conn, Row};
33
use mysql::prelude::Queryable;
44
use crate::database::{Database, DBRawToStruct};
@@ -10,7 +10,7 @@ pub struct MySql {
1010

1111
pub fn init (username : &Option<String>,password : &Option<String>,host_name : &Option<String>,db_name : &Option<String>) -> std::io::Result<String> {
1212
if username.is_none() || password.is_none() || host_name.is_none() || db_name.is_none() {
13-
Err(Error::new(ErrorKind::Other,"Error mysql initialize parameters."))
13+
Err(Error::other("Error mysql initialize parameters."))
1414
} else {
1515
let conn_url = format!("mysql://{}:{}@{}/{}",username.clone().unwrap(),password.clone().unwrap(),host_name.clone().unwrap(),db_name.clone().unwrap());
1616
let connection = Conn::new(conn_url.as_str());
@@ -37,12 +37,12 @@ pub fn init (username : &Option<String>,password : &Option<String>,host_name : &
3737
Ok(conn_url)
3838
}
3939
Err(e) => {
40-
Err(Error::new(ErrorKind::Other,format!("Error setup mysql {:?}",e)))
40+
Err(Error::other(format!("Error setup mysql {:?}",e)))
4141
}
4242
}
4343
}
4444
Err(e) => {
45-
Err(Error::new(ErrorKind::Other,format!("Error setup mysql {:?}",e)))
45+
Err(Error::other(format!("Error setup mysql {:?}",e)))
4646
}
4747
}
4848
}
@@ -83,7 +83,7 @@ impl Database for MySql {
8383
Ok(())
8484
}
8585
Err(e) => {
86-
Err(Error::new(ErrorKind::Other, format!("Error insert mysql {:?}", e)))
86+
Err(Error::other( format!("Error insert mysql {:?}", e)))
8787
}
8888
}
8989
}
@@ -104,7 +104,7 @@ impl Database for MySql {
104104
}
105105
}
106106
Err(e) => {
107-
Err(Error::new(ErrorKind::Other, format!("Error select mysql {:?}",e)))
107+
Err(Error::other(format!("Error select mysql {:?}",e)))
108108
}
109109
}
110110
}
@@ -119,7 +119,7 @@ impl Database for MySql {
119119
Ok(result)
120120
}
121121
Err(e) => {
122-
Err(Error::new(ErrorKind::Other, format!("Error select mysql {:?}", e)))
122+
Err(Error::other(format!("Error select mysql {:?}", e)))
123123
}
124124
}
125125
}

src/database/none.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::io::{Error, ErrorKind};
1+
use std::io::Error;
22
use crate::database::Database;
33
use crate::results::TelemetryData;
44

@@ -7,12 +7,12 @@ pub struct NoneDB;
77
impl Database for NoneDB {
88
fn insert(&mut self,data : TelemetryData) -> std::io::Result<()> {
99
drop(data);
10-
Err(Error::new(ErrorKind::Other,"Database disabled"))
10+
Err(Error::other("Database disabled"))
1111
}
1212
fn fetch_by_uuid(&mut self,_uuid : &str) -> std::io::Result<Option<TelemetryData>> {
13-
Err(Error::new(ErrorKind::Other,"Database disabled"))
13+
Err(Error::other("Database disabled"))
1414
}
1515
fn fetch_last_100(&mut self) -> std::io::Result<Vec<TelemetryData>> {
16-
Err(Error::new(ErrorKind::Other,"Database disabled"))
16+
Err(Error::other("Database disabled"))
1717
}
1818
}

src/database/postgres.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::io::{Error, ErrorKind};
1+
use std::io::Error;
22
use postgres::{Client, NoTls, Row};
33
use tokio::task::block_in_place;
44
use crate::database::{Database, DBRawToStruct};
@@ -10,7 +10,7 @@ pub struct Postgres {
1010

1111
pub fn init (username : &Option<String>,password : &Option<String>,host_name : &Option<String>,db_name : &Option<String>) -> std::io::Result<Client> {
1212
if username.is_none() || password.is_none() || host_name.is_none() || db_name.is_none() {
13-
Err(Error::new(ErrorKind::Other,"Error postgres initialize parameters."))
13+
Err(Error::other("Error postgres initialize parameters."))
1414
} else {
1515
let conn_url = format!("postgresql://{}:{}@{}/{}",username.clone().unwrap(),password.clone().unwrap(),host_name.clone().unwrap(),db_name.clone().unwrap());
1616
block_in_place(|| {
@@ -40,12 +40,12 @@ pub fn init (username : &Option<String>,password : &Option<String>,host_name : &
4040
Ok(client)
4141
}
4242
Err(e) => {
43-
Err(Error::new(ErrorKind::Other,format!("Error setup postgres {:?}",e)))
43+
Err(Error::other(format!("Error setup postgres {:?}",e)))
4444
}
4545
}
4646
}
4747
Err(e) => {
48-
Err(Error::new(ErrorKind::Other,format!("Error setup postgres {:?}",e)))
48+
Err(Error::other(format!("Error setup postgres {:?}",e)))
4949
}
5050
}
5151
})
@@ -86,7 +86,7 @@ impl Database for Postgres {
8686
Ok(())
8787
}
8888
Err(e) => {
89-
Err(Error::new(ErrorKind::Other, format!("Error insert postgres {:?}", e)))
89+
Err(Error::other(format!("Error insert postgres {:?}", e)))
9090
}
9191
}
9292
}
@@ -99,7 +99,7 @@ impl Database for Postgres {
9999
Ok(Some(row.to_telemetry_struct().unwrap()))
100100
}
101101
Err(e) => {
102-
Err(Error::new(ErrorKind::Other, format!("Error select postgres {:?}", e)))
102+
Err(Error::other(format!("Error select postgres {:?}", e)))
103103
}
104104
}
105105
}
@@ -113,7 +113,7 @@ impl Database for Postgres {
113113
Ok(result)
114114
}
115115
Err(e) => {
116-
Err(Error::new(ErrorKind::Other, format!("Error select postgres {:?}", e)))
116+
Err(Error::other(format!("Error select postgres {:?}", e)))
117117
}
118118
}
119119
}

src/database/sqlite.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::io::{Error, ErrorKind};
1+
use std::io::Error;
22
use rusqlite::{Connection, Row};
33
use crate::database::{Database, DBRawToStruct};
44
use crate::results::TelemetryData;
@@ -10,7 +10,7 @@ pub struct SQLite {
1010
pub fn init (database_file : &Option<String>) -> std::io::Result<Connection> {
1111
match database_file {
1212
None => {
13-
Err(Error::new(ErrorKind::Other,"Error setup sqlite invalid database file."))
13+
Err(Error::other("Error setup sqlite invalid database file."))
1414
}
1515
Some(database_file) => {
1616
let connection = Connection::open(database_file);
@@ -39,12 +39,12 @@ pub fn init (database_file : &Option<String>) -> std::io::Result<Connection> {
3939
Ok(connection)
4040
}
4141
Err(e) => {
42-
Err(Error::new(ErrorKind::Other,format!("Error setup sqlite {:?}",e)))
42+
Err(Error::other(format!("Error setup sqlite {:?}",e)))
4343
}
4444
}
4545
}
4646
Err(e) => {
47-
Err(Error::new(ErrorKind::Other,format!("Error setup sqlite {:?}",e)))
47+
Err(Error::other(format!("Error setup sqlite {:?}",e)))
4848
}
4949
}
5050
}
@@ -83,7 +83,7 @@ impl Database for SQLite {
8383
Ok(())
8484
}
8585
Err(e) => {
86-
Err(Error::new(ErrorKind::Other, format!("Error insert sqlite {:?}", e)))
86+
Err(Error::other(format!("Error insert sqlite {:?}", e)))
8787
}
8888
}
8989
}
@@ -103,7 +103,7 @@ impl Database for SQLite {
103103
}
104104
}
105105
Err(e) => {
106-
Err(Error::new(ErrorKind::Other, format!("Error select sqlite {:?}", e)))
106+
Err(Error::other(format!("Error select sqlite {:?}", e)))
107107
}
108108
}
109109
}
@@ -124,7 +124,7 @@ impl Database for SQLite {
124124
}
125125
}
126126
Err(e) => {
127-
Err(Error::new(ErrorKind::Other, format!("Error select sqlite {:?}", e)))
127+
Err(Error::other(format!("Error select sqlite {:?}", e)))
128128
}
129129
}
130130
}

src/http/http_client.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::fs::File;
2-
use std::io::{Error, ErrorKind};
2+
use std::io::{Error};
33
use std::pin::Pin;
44
use std::task::{Context, Poll};
55
use serde_json::Value;
@@ -118,8 +118,8 @@ impl HttpClient {
118118
let parsed_headers = header_parser(buf_reader).await;
119119
//read body
120120
let body_len = parsed_headers.get("Content-Length");
121-
if body_len.is_some() {
122-
let body_len = body_len.unwrap().parse::<usize>().unwrap();
121+
if let Some(body_len) = body_len {
122+
let body_len = body_len.parse::<usize>().unwrap();
123123
let pb = ProgressBar::new(body_len as u64);
124124
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
125125
.unwrap()
@@ -152,7 +152,7 @@ impl HttpClient {
152152
let port = if scheme == "https" { 443 } else { 80 };
153153
Ok((scheme.to_string(),host.to_string(),port,path.to_string()))
154154
} else {
155-
Err(Error::new(ErrorKind::Other,"Error parsing input url"))
155+
Err(Error::other("Error parsing input url"))
156156
}
157157
}
158158

src/http/http_server.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl HttpServer {
2525
pub async fn init () -> std::io::Result<Self> {
2626
let config = SERVER_CONFIG.get().unwrap();
2727
let tcp_socket = TcpSocket::make_listener(config)?;
28-
info!("Server started on {}",tcp_socket.to_string());
28+
info!("Server started on {}",tcp_socket);
2929
info!("Server base url : {}/",config.base_url);
3030
let mut tls_acceptor = None;
3131
if config.enable_tls {
@@ -51,17 +51,9 @@ impl HttpServer {
5151

5252
let remote_addr = find_remote_ip_addr(&mut socket).await;
5353

54-
if tls_acceptor.is_none() {
54+
if let Some(tls_acceptor) = tls_acceptor {
5555

56-
let (socket_r,socket_w) = socket.split();
57-
let mut buff_reader = BufReader::with_capacity(8 * 1024,socket_r);
58-
let mut buff_writer = BufWriter::with_capacity(8 * 1024,socket_w);
59-
Self::handle_connection(&remote_addr,&mut buff_reader,&mut buff_writer,&mut database).await;
60-
61-
62-
} else {
63-
64-
let stream = tls_acceptor.unwrap().accept(socket).await;
56+
let stream = tls_acceptor.accept(socket).await;
6557
match stream {
6658
Ok(stream) => {
6759

@@ -72,17 +64,24 @@ impl HttpServer {
7264

7365
}
7466
Err(e) => {
75-
trace!("Error tcp connection : {}",e.to_string())
67+
trace!("Error tcp connection : {e}")
7668
}
7769
}
7870

71+
} else {
72+
73+
let (socket_r,socket_w) = socket.split();
74+
let mut buff_reader = BufReader::with_capacity(8 * 1024,socket_r);
75+
let mut buff_writer = BufWriter::with_capacity(8 * 1024,socket_w);
76+
Self::handle_connection(&remote_addr,&mut buff_reader,&mut buff_writer,&mut database).await;
77+
7978
}
8079

8180
});
8281

8382
}
8483
Err(e) => {
85-
trace!("Error tcp connection : {}",e.to_string())
84+
trace!("Error tcp connection : {e}")
8685
}
8786
}
8887

src/http/request.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,20 @@ F: Send + Sync + Fn(Request) -> Pin<Box<dyn Future<Output = Response> + Send>>
159159
form_data : body_form_data.clone().unwrap_or(HashMap::new())
160160
}).await;
161161
if let Err(e) = buf_writer.write_all(&response.data).await {
162-
trace!("Error socket write : {}",e.to_string())
162+
trace!("Error socket write : {e}")
163163
}
164164
if response.chunk_count > 0 {
165165
for _ in 0..response.chunk_count {
166166
if let Err(e) = buf_writer.write_all(GARBAGE_DATA.get().unwrap()).await {
167-
trace!("Error socket write chunk : {}",e.to_string())
167+
trace!("Error socket write chunk : {e}")
168168
}
169169
}
170170
if let Err(e) = buf_writer.write_all(b"0\r\n\r\n").await {
171-
trace!("Error socket write eof : {}",e.to_string())
171+
trace!("Error socket write eof : {e}")
172172
}
173173
}
174174
if let Err(e) = buf_writer.flush().await {
175-
trace!("Error socket flush : {}",e.to_string())
175+
trace!("Error socket flush : {e}")
176176
}
177177
}
178178
}
@@ -184,10 +184,7 @@ fn check_is_status_line (line : String) -> bool {
184184

185185
#[allow(dead_code)]
186186
fn hex_string_to_int(hex_string: &str) -> Option<u64> {
187-
match u64::from_str_radix(hex_string, 16) {
188-
Ok(parsed_int) => Some(parsed_int),
189-
Err(_) => None,
190-
}
187+
u64::from_str_radix(hex_string, 16).ok()
191188
}
192189

193190
pub async fn header_parser<R>(buf_reader: &mut BufReader<R>) -> CIHashMap<String>

src/http/tcp_socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl IpParser for &str {
138138
IpAddr::V4(_) => Ok((ip, Domain::IPV4)),
139139
IpAddr::V6(_) => Ok((ip, Domain::IPV6)),
140140
},
141-
Err(e) => Err(Error::new(ErrorKind::Other, e)),
141+
Err(e) => Err(Error::other(e)),
142142
}
143143
}
144144
}

0 commit comments

Comments
 (0)