Skip to content

Commit f76df7f

Browse files
committed
lint code again
1 parent 3facf2c commit f76df7f

File tree

7 files changed

+277
-338
lines changed

7 files changed

+277
-338
lines changed

src/cache.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const TABLE: TableDefinition<&str, &[u8]> = TableDefinition::new("_");
2222
impl Cache {
2323
pub fn new(min_file_size: usize, db_file_path: Option<String>) -> Cache {
2424
let mut cache = Cache {
25-
min_file_size: min_file_size,
25+
min_file_size,
2626
metadata: HashMap::new(),
2727
db: None,
2828
db_ok: None,
@@ -90,19 +90,17 @@ impl Cache {
9090
metadata_item.media_type.as_ref().expect("").to_string(),
9191
metadata_item.charset.as_ref().expect("").to_string(),
9292
));
93-
} else {
94-
if self.db_ok.is_some() && self.db_ok.unwrap() {
95-
let read_txn = self.db.as_ref().unwrap().begin_read()?;
96-
let table = read_txn.open_table(TABLE)?;
97-
let data = table.get(key)?;
98-
let bytes = data.unwrap();
93+
} else if self.db_ok.is_some() && self.db_ok.unwrap() {
94+
let read_txn = self.db.as_ref().unwrap().begin_read()?;
95+
let table = read_txn.open_table(TABLE)?;
96+
let data = table.get(key)?;
97+
let bytes = data.unwrap();
9998

100-
return Ok((
101-
bytes.value().to_vec(),
102-
metadata_item.media_type.as_ref().expect("").to_string(),
103-
metadata_item.charset.as_ref().expect("").to_string(),
104-
));
105-
}
99+
return Ok((
100+
bytes.value().to_vec(),
101+
metadata_item.media_type.as_ref().expect("").to_string(),
102+
metadata_item.charset.as_ref().expect("").to_string(),
103+
));
106104
}
107105
}
108106

src/cookies.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Cookie {
3131
}
3232

3333
pub fn matches_url(&self, url: &str) -> bool {
34-
match Url::parse(&url) {
34+
match Url::parse(url) {
3535
Ok(url) => {
3636
// Check protocol scheme
3737
match url.scheme() {
@@ -56,10 +56,8 @@ impl Cookie {
5656
{
5757
return false;
5858
}
59-
} else {
60-
if !url_host.eq_ignore_ascii_case(&self.domain) {
61-
return false;
62-
}
59+
} else if !url_host.eq_ignore_ascii_case(&self.domain) {
60+
return false;
6361
}
6462
} else {
6563
return false;
@@ -105,9 +103,9 @@ pub fn parse_cookie_file_contents(
105103
}
106104
cookies.push(Cookie {
107105
domain: fields.next().unwrap().to_string().to_lowercase(),
108-
include_subdomains: fields.next().unwrap().to_string() == "TRUE",
106+
include_subdomains: fields.next().unwrap() == "TRUE",
109107
path: fields.next().unwrap().to_string(),
110-
https_only: fields.next().unwrap().to_string() == "TRUE",
108+
https_only: fields.next().unwrap() == "TRUE",
111109
expires: fields.next().unwrap().parse::<u64>().unwrap(),
112110
name: fields.next().unwrap().to_string(),
113111
value: fields.next().unwrap().to_string(),

src/core.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ pub struct Options {
6969
pub user_agent: Option<String>,
7070
}
7171

72-
const ANSI_COLOR_RED: &'static str = "\x1b[31m";
73-
const ANSI_COLOR_RESET: &'static str = "\x1b[0m";
72+
const ANSI_COLOR_RED: &str = "\x1b[31m";
73+
const ANSI_COLOR_RESET: &str = "\x1b[0m";
7474
const FILE_SIGNATURES: [[&[u8]; 2]; 18] = [
7575
// Image
7676
[b"GIF87a", b"image/gif"],
@@ -103,16 +103,16 @@ const PLAINTEXT_MEDIA_TYPES: &[&str] = &[
103103
pub fn create_monolithic_document(
104104
source: String,
105105
options: &Options,
106-
mut cache: &mut Option<Cache>,
106+
cache: &mut Option<Cache>,
107107
) -> Result<Vec<u8>, MonolithError> {
108108
// Check if source was provided
109-
if source.len() == 0 {
109+
if source.is_empty() {
110110
return Err(MonolithError::new("no target specified"));
111111
}
112112

113113
// Check if custom encoding value is acceptable
114114
if let Some(custom_encoding) = options.encoding.clone() {
115-
if !Encoding::for_label_no_replacement(custom_encoding.as_bytes()).is_some() {
115+
if Encoding::for_label_no_replacement(custom_encoding.as_bytes()).is_none() {
116116
return Err(MonolithError::new(&format!(
117117
"unknown encoding \"{}\"",
118118
&custom_encoding
@@ -129,7 +129,7 @@ pub fn create_monolithic_document(
129129
// Set default target URL to an empty data URL; the user can set it via --base-url
130130
Url::parse("data:text/html,").unwrap()
131131
}
132-
target => match Url::parse(&target) {
132+
target => match Url::parse(target) {
133133
Ok(url) => match url.scheme() {
134134
"data" | "file" | "http" | "https" => url,
135135
unsupported_scheme => {
@@ -145,7 +145,7 @@ pub fn create_monolithic_document(
145145
match path.exists() {
146146
true => match path.is_file() {
147147
true => {
148-
let canonical_path = fs::canonicalize(&path).unwrap();
148+
let canonical_path = fs::canonicalize(path).unwrap();
149149
match Url::from_file_path(canonical_path) {
150150
Ok(url) => url,
151151
Err(_) => {
@@ -178,7 +178,7 @@ pub fn create_monolithic_document(
178178
if let Some(user_agent) = &options.user_agent {
179179
header_map.insert(
180180
USER_AGENT,
181-
HeaderValue::from_str(&user_agent).expect("Invalid User-Agent header specified"),
181+
HeaderValue::from_str(user_agent).expect("Invalid User-Agent header specified"),
182182
);
183183
}
184184
let client = Client::builder()
@@ -209,7 +209,7 @@ pub fn create_monolithic_document(
209209
|| target_url.scheme() == "https"
210210
|| target_url.scheme() == "data"
211211
{
212-
match retrieve_asset(&mut cache, &client, &target_url, &target_url, &options) {
212+
match retrieve_asset(cache, &client, &target_url, &target_url, options) {
213213
Ok((retrieved_data, final_url, media_type, charset)) => {
214214
// Provide output as text (without processing it, the way browsers do)
215215
if !media_type.eq_ignore_ascii_case("text/html")
@@ -283,7 +283,7 @@ pub fn create_monolithic_document(
283283
// Relative paths could work for documents saved from filesystem
284284
let path: &Path = Path::new(&custom_base_url);
285285
if path.exists() {
286-
match Url::from_file_path(fs::canonicalize(&path).unwrap()) {
286+
match Url::from_file_path(fs::canonicalize(path).unwrap()) {
287287
Ok(file_url) => {
288288
base_url = file_url;
289289
}
@@ -301,7 +301,7 @@ pub fn create_monolithic_document(
301301
}
302302

303303
// Traverse through the document and embed remote assets
304-
walk_and_embed_assets(&mut cache, &client, &base_url, &dom.document, &options);
304+
walk_and_embed_assets(cache, &client, &base_url, &dom.document, options);
305305

306306
// Update or add new BASE element to reroute network requests and hash-links
307307
if let Some(new_base_url) = options.base_url.clone() {
@@ -315,7 +315,7 @@ pub fn create_monolithic_document(
315315
{
316316
let favicon_ico_url: Url = resolve_url(&base_url, "/favicon.ico");
317317

318-
match retrieve_asset(&mut cache, &client, &target_url, &favicon_ico_url, &options) {
318+
match retrieve_asset(cache, &client, &target_url, &favicon_ico_url, options) {
319319
Ok((data, final_url, media_type, charset)) => {
320320
let favicon_data_url: Url =
321321
create_data_url(&media_type, &charset, &data, &final_url);
@@ -334,7 +334,7 @@ pub fn create_monolithic_document(
334334
}
335335

336336
// Serialize DOM tree
337-
let mut result: Vec<u8> = serialize_document(dom, document_encoding, &options);
337+
let mut result: Vec<u8> = serialize_document(dom, document_encoding, options);
338338

339339
// Prepend metadata comment tag
340340
if !options.no_metadata {
@@ -400,7 +400,7 @@ pub fn detect_media_type_by_file_name(filename: &str) -> String {
400400
}
401401

402402
pub fn domain_is_within_domain(domain: &str, domain_to_match_against: &str) -> bool {
403-
if domain_to_match_against.len() == 0 {
403+
if domain_to_match_against.is_empty() {
404404
return false;
405405
}
406406

@@ -444,7 +444,7 @@ pub fn domain_is_within_domain(domain: &str, domain_to_match_against: &str) -> b
444444

445445
let parts_match = domain_to_match_against_partial.eq_ignore_ascii_case(domain_partial);
446446

447-
if !parts_match && domain_to_match_against_partial.len() != 0 {
447+
if !parts_match && !domain_to_match_against_partial.is_empty() {
448448
ok = false;
449449
break;
450450
}
@@ -470,15 +470,13 @@ pub fn parse_content_type(content_type: &str) -> (String, String, bool) {
470470
let mut i: i8 = 0;
471471
for item in &content_type_items {
472472
if i == 0 {
473-
if item.trim().len() > 0 {
473+
if !item.trim().is_empty() {
474474
media_type = item.trim().to_string();
475475
}
476-
} else {
477-
if item.trim().eq_ignore_ascii_case("base64") {
478-
is_base64 = true;
479-
} else if item.trim().starts_with("charset=") {
480-
charset = item.trim().chars().skip(8).collect();
481-
}
476+
} else if item.trim().eq_ignore_ascii_case("base64") {
477+
is_base64 = true;
478+
} else if item.trim().starts_with("charset=") {
479+
charset = item.trim().chars().skip(8).collect();
482480
}
483481

484482
i += 1;
@@ -502,10 +500,9 @@ pub fn retrieve_asset(
502500
if parent_url.scheme() != "file" {
503501
if !options.silent {
504502
eprintln!(
505-
"{}{} ({}){}",
503+
"{}{} (Security Error){}",
506504
if options.no_color { "" } else { ANSI_COLOR_RED },
507505
&url,
508-
"Security Error",
509506
if options.no_color {
510507
""
511508
} else {
@@ -541,7 +538,7 @@ pub fn retrieve_asset(
541538
eprintln!("{}", &url);
542539
}
543540

544-
let file_blob: Vec<u8> = fs::read(&path).expect("Unable to read file");
541+
let file_blob: Vec<u8> = fs::read(path).expect("Unable to read file");
545542

546543
Ok((
547544
file_blob.clone(),
@@ -586,7 +583,7 @@ pub fn retrieve_asset(
586583
if let Some(domains) = &options.domains {
587584
let domain_matches = domains
588585
.iter()
589-
.any(|d| domain_is_within_domain(url.host_str().unwrap(), &d.trim()));
586+
.any(|d| domain_is_within_domain(url.host_str().unwrap(), d.trim()));
590587
if (options.blacklist_domains && domain_matches)
591588
|| (!options.blacklist_domains && !domain_matches)
592589
{
@@ -596,7 +593,7 @@ pub fn retrieve_asset(
596593

597594
// URL not in cache, we retrieve the file
598595
let mut headers = HeaderMap::new();
599-
if options.cookies.len() > 0 {
596+
if !options.cookies.is_empty() {
600597
for cookie in &options.cookies {
601598
if !cookie.is_expired() && cookie.matches_url(url.as_str()) {
602599
let cookie_header_value: String = cookie.name.clone() + "=" + &cookie.value;
@@ -651,7 +648,7 @@ pub fn retrieve_asset(
651648
.and_then(|header| header.to_str().ok())
652649
.unwrap_or("");
653650

654-
let (media_type, charset, _is_base64) = parse_content_type(&content_type);
651+
let (media_type, charset, _is_base64) = parse_content_type(content_type);
655652

656653
// Convert response into a byte array
657654
let mut data: Vec<u8> = vec![];

0 commit comments

Comments
 (0)