Skip to content

Commit db2e8f7

Browse files
committed
Fix clippy
1 parent d6e5ba9 commit db2e8f7

File tree

6 files changed

+27
-86
lines changed

6 files changed

+27
-86
lines changed

Cargo.lock

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ edition = "2018"
1212
[dependencies]
1313
clap = "2.33.0"
1414
termcolor = "1.0.5"
15-
lazy_static = "1.4.0"
1615
time = "0.1.42"
1716
chrono = "0.4.9"
1817
flate2 = "1.0.11"

src/color.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ impl Printer {
8080
count += 1;
8181
} else {
8282
return Err(StringError(format!(
83-
"Not enough arguments (need more than {})",
84-
count
83+
"Not enough arguments (need more than {count})",
8584
)));
8685
}
8786
}

src/main.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use iron::status;
2121
use iron::status::Status;
2222
use iron::{Chain, Handler, Iron, IronError, IronResult, Request, Response, Set};
2323
use iron_cors::CorsMiddleware;
24-
use lazy_static::lazy_static;
2524
use mime_guess as mime_types;
2625
use multipart::server::{Multipart, SaveResult};
2726
use path_dedot::ParseDot;
@@ -42,10 +41,7 @@ use middlewares::{AuthChecker, CompressionHandler, RequestLogger};
4241
const ORDER_ASC: &str = "asc";
4342
const ORDER_DESC: &str = "desc";
4443
const DEFAULT_ORDER: &str = ORDER_DESC;
45-
46-
lazy_static! {
47-
static ref SORT_FIELDS: Vec<&'static str> = vec!["name", "modified", "size"];
48-
}
44+
const SORT_FIELDS: &[&str] = &["name", "modified", "size"];
4945

5046
fn main() {
5147
let matches = clap::App::new("Simple HTTP(s) Server")
@@ -252,20 +248,20 @@ fn main() {
252248
let color_blue = Some(build_spec(Some(Color::Blue), false));
253249
let color_red = Some(build_spec(Some(Color::Red), false));
254250
let addr = if IpAddr::from_str(ip).unwrap().is_ipv4() {
255-
format!("{}:{}", ip, port)
251+
format!("{ip}:{port}")
256252
} else {
257-
format!("[{}]:{}", ip, port)
253+
format!("[{ip}]:{port}")
258254
};
259255
let compression_exts = compress
260256
.clone()
261257
.unwrap_or_default()
262258
.iter()
263-
.map(|s| format!("*.{}", s))
259+
.map(|s| format!("*.{s}"))
264260
.collect::<Vec<String>>();
265261
let compression_string = if compression_exts.is_empty() {
266262
"disabled".to_owned()
267263
} else {
268-
format!("{:?}", compression_exts)
264+
format!("{compression_exts:?}")
269265
};
270266

271267
let open = matches.is_present("open");
@@ -275,7 +271,7 @@ fn main() {
275271

276272
match open::that(&host) {
277273
Ok(_) => println!("Openning {} in default browser", &host),
278-
Err(err) => eprintln!("Unable to open in default browser {}", err),
274+
Err(err) => eprintln!("Unable to open in default browser {err}"),
279275
}
280276
}
281277

@@ -358,7 +354,7 @@ fn main() {
358354
sort,
359355
compress: compress
360356
.clone()
361-
.map(|exts| exts.iter().map(|s| format!(".{}", s)).collect()),
357+
.map(|exts| exts.iter().map(|s| format!(".{s}")).collect()),
362358
try_file_404: try_file_404.map(PathBuf::from),
363359
upload_size_limit,
364360
base_url: base_url.to_string(),
@@ -465,7 +461,7 @@ impl Handler for MainHandler {
465461
.decode_utf8()
466462
.map(|path| PathBuf::from(&*path))
467463
.map_err(|_err| {
468-
let err_msg = format!("invalid path: {}", s);
464+
let err_msg = format!("invalid path: {s}");
469465
println!("[BadRequest]: {err_msg}");
470466
IronError::new(StringError(err_msg), status::BadRequest)
471467
})
@@ -595,7 +591,7 @@ impl MainHandler {
595591
{
596592
return Err((
597593
status::InternalServerError,
598-
format!("Copy file failed: {}", errno),
594+
format!("Copy file failed: {errno}"),
599595
));
600596
} else {
601597
println!(" >> File saved: {}", headers.filename.clone().unwrap());
@@ -605,7 +601,7 @@ impl MainHandler {
605601
}
606602
SaveResult::Partial(_entries, reason) => Err((
607603
status::InternalServerError,
608-
format!("save file failed: {:?}", reason),
604+
format!("save file failed: {reason:?}"),
609605
)),
610606
SaveResult::Error(error) => {
611607
Err((status::InternalServerError, error.to_string()))
@@ -686,13 +682,13 @@ impl MainHandler {
686682
}
687683

688684
if let Some(field) = sort_field {
689-
if !SORT_FIELDS.iter().any(|s| *s == field.as_str()) {
690-
let err_msg = format!("Unknown sort field: {}", field);
685+
if !SORT_FIELDS.contains(&field.as_str()) {
686+
let err_msg = format!("Unknown sort field: {field}");
691687
println!("[BadRequest]: {err_msg}");
692688
return Err(IronError::new(StringError(err_msg), status::BadRequest));
693689
}
694690
if ![ORDER_ASC, ORDER_DESC].iter().any(|s| *s == order) {
695-
let err_msg = format!("Unknown sort order: {}", order);
691+
let err_msg = format!("Unknown sort order: {order}");
696692
println!("[BadRequest]: {err_msg}");
697693
return Err(IronError::new(StringError(err_msg), status::BadRequest));
698694
}
@@ -972,10 +968,9 @@ impl MainHandler {
972968
// "x-y"
973969
if x >= metadata.len() || x > y {
974970
return Err(IronError::new(
975-
StringError(format!(
976-
"Invalid range(x={}, y={})",
977-
x, y
978-
)),
971+
StringError(
972+
format!("Invalid range(x={x}, y={y})",),
973+
),
979974
status::RangeNotSatisfiable,
980975
));
981976
}

src/middlewares/logger.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
use std::ops::Deref;
2+
use std::sync::LazyLock;
23

34
use iron::status;
45
use iron::{AfterMiddleware, IronError, IronResult, Request, Response};
5-
use lazy_static::lazy_static;
66
use percent_encoding::percent_decode;
77
use termcolor::{Color, ColorSpec};
88

99
use crate::color::{build_spec, Printer};
1010
use crate::util::{error_resp, now_string};
1111

12-
lazy_static! {
13-
static ref C_BOLD_GREEN: Option<ColorSpec> = Some(build_spec(Some(Color::Green), true));
14-
static ref C_BOLD_YELLOW: Option<ColorSpec> = Some(build_spec(Some(Color::Yellow), true));
15-
static ref C_BOLD_RED: Option<ColorSpec> = Some(build_spec(Some(Color::Red), true));
16-
}
12+
static C_BOLD_GREEN: LazyLock<Option<ColorSpec>> =
13+
LazyLock::new(|| Some(build_spec(Some(Color::Green), true)));
14+
static C_BOLD_YELLOW: LazyLock<Option<ColorSpec>> =
15+
LazyLock::new(|| Some(build_spec(Some(Color::Yellow), true)));
16+
static C_BOLD_RED: LazyLock<Option<ColorSpec>> =
17+
LazyLock::new(|| Some(build_spec(Some(Color::Red), true)));
1718

1819
pub struct RequestLogger {
1920
pub printer: Printer,

src/util.rs

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ const PATH_ENCODE_SET: &AsciiSet = &FRAGMENT_ENCODE_SET.add(b'#').add(b'?').add(
2222
const PATH_SEGMENT_ENCODE_SET: &AsciiSet = &PATH_ENCODE_SET.add(b'/').add(b'%').add(b'[').add(b']');
2323

2424
pub fn root_link(baseurl: &str) -> String {
25-
format!(
26-
r#"<a href="{baseurl}"><strong>[Root]</strong></a>"#,
27-
baseurl = baseurl,
28-
)
25+
format!(r#"<a href="{baseurl}"><strong>[Root]</strong></a>"#)
2926
}
3027

3128
#[derive(Debug)]
@@ -71,55 +68,6 @@ pub fn error_io2iron(err: io::Error) -> IronError {
7168
IronError::new(err, status)
7269
}
7370

74-
/* TODO: may not used
75-
76-
use iron::headers::{Range, ByteRangeSpec};
77-
78-
#[allow(dead_code)]
79-
pub fn parse_range(ranges: &Vec<ByteRangeSpec>, total: u64)
80-
-> Result<Option<(u64, u64)>, IronError> {
81-
if let Some(range) = ranges.get(0) {
82-
let (offset, length) = match range {
83-
&ByteRangeSpec::FromTo(x, mut y) => { // "x-y"
84-
if x >= total || x > y {
85-
return Err(IronError::new(
86-
StringError(format!("Invalid range(x={}, y={})", x, y)),
87-
status::RangeNotSatisfiable
88-
));
89-
}
90-
if y >= total {
91-
y = total - 1;
92-
}
93-
(x, y - x + 1)
94-
}
95-
&ByteRangeSpec::AllFrom(x) => { // "x-"
96-
if x >= total {
97-
return Err(IronError::new(
98-
StringError(format!(
99-
"Range::AllFrom to large (x={}), Content-Length: {})",
100-
x, total)),
101-
status::RangeNotSatisfiable
102-
));
103-
}
104-
(x, total - x)
105-
}
106-
&ByteRangeSpec::Last(mut x) => { // "-x"
107-
if x > total {
108-
x = total;
109-
}
110-
(total - x, x)
111-
}
112-
};
113-
Ok(Some((offset, length)))
114-
} else {
115-
return Err(IronError::new(
116-
StringError("Empty range set".to_owned()),
117-
status::RangeNotSatisfiable
118-
));
119-
}
120-
}
121-
*/
122-
12371
pub fn now_string() -> String {
12472
Local::now().format("%Y-%m-%d %H:%M:%S").to_string()
12573
}

0 commit comments

Comments
 (0)