Skip to content

Commit af0ff56

Browse files
committed
Clean up code
1 parent 18424e4 commit af0ff56

156 files changed

Lines changed: 1133 additions & 924 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

package-lock.json

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

src-tauri/src/commands/compress.rs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fs::File;
33
use std::io::{Read, Write};
44

55
/// Compress data using gzip
6+
#[allow(clippy::needless_pass_by_value)]
67
#[tauri::command]
78
pub fn gzip_compress(data: Vec<u8>) -> Result<Vec<u8>, String> {
89
use flate2::write::GzEncoder;
@@ -11,15 +12,16 @@ pub fn gzip_compress(data: Vec<u8>) -> Result<Vec<u8>, String> {
1112
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
1213
encoder
1314
.write_all(&data)
14-
.map_err(|e| format!("Compression error: {}", e))?;
15+
.map_err(|e| format!("Compression error: {e}"))?;
1516
encoder
1617
.finish()
17-
.map_err(|e| format!("Compression finish error: {}", e))
18+
.map_err(|e| format!("Compression finish error: {e}"))
1819
}
1920

2021
const MAX_DECOMPRESSED_SIZE: u64 = 100 * 1024 * 1024; // 100 MB
2122

2223
/// Decompress gzip data
24+
#[allow(clippy::needless_pass_by_value)]
2325
#[tauri::command]
2426
pub fn gzip_decompress(data: Vec<u8>) -> Result<Vec<u8>, String> {
2527
use flate2::read::GzDecoder;
@@ -29,14 +31,15 @@ pub fn gzip_decompress(data: Vec<u8>) -> Result<Vec<u8>, String> {
2931
let mut result = Vec::new();
3032
limited
3133
.read_to_end(&mut result)
32-
.map_err(|e| format!("Decompression error: {}", e))?;
34+
.map_err(|e| format!("Decompression error: {e}"))?;
3335
if result.len() as u64 > MAX_DECOMPRESSED_SIZE {
3436
return Err("Decompressed data exceeds 100 MB limit".to_string());
3537
}
3638
Ok(result)
3739
}
3840

3941
/// Compress string to gzip base64
42+
#[allow(clippy::needless_pass_by_value)]
4043
#[tauri::command]
4144
pub fn gzip_compress_text(text: String) -> Result<String, String> {
4245
use base64::{engine::general_purpose::STANDARD, Engine as _};
@@ -46,15 +49,16 @@ pub fn gzip_compress_text(text: String) -> Result<String, String> {
4649
}
4750

4851
/// Decompress gzip base64 to string
52+
#[allow(clippy::needless_pass_by_value)]
4953
#[tauri::command]
5054
pub fn gzip_decompress_text(text: String) -> Result<String, String> {
5155
use base64::{engine::general_purpose::STANDARD, Engine as _};
5256

5357
let decoded = STANDARD
5458
.decode(&text)
55-
.map_err(|e| format!("Base64 decode error: {}", e))?;
59+
.map_err(|e| format!("Base64 decode error: {e}"))?;
5660
let decompressed = gzip_decompress(decoded)?;
57-
String::from_utf8(decompressed).map_err(|e| format!("UTF-8 decode error: {}", e))
61+
String::from_utf8(decompressed).map_err(|e| format!("UTF-8 decode error: {e}"))
5862
}
5963

6064
#[derive(Debug, Serialize)]
@@ -66,18 +70,18 @@ pub struct ZipEntry {
6670
}
6771

6872
/// List contents of a zip file
73+
#[allow(clippy::needless_pass_by_value)]
6974
#[tauri::command]
7075
pub fn zip_list(path: String) -> Result<Vec<ZipEntry>, String> {
71-
let file = File::open(&path).map_err(|e| format!("Failed to open zip: {}", e))?;
76+
let file = File::open(&path).map_err(|e| format!("Failed to open zip: {e}"))?;
7277

73-
let mut archive =
74-
zip::ZipArchive::new(file).map_err(|e| format!("Failed to read zip: {}", e))?;
78+
let mut archive = zip::ZipArchive::new(file).map_err(|e| format!("Failed to read zip: {e}"))?;
7579

7680
let mut entries = Vec::new();
7781
for i in 0..archive.len() {
7882
let file = archive
7983
.by_index(i)
80-
.map_err(|e| format!("Failed to read zip entry: {}", e))?;
84+
.map_err(|e| format!("Failed to read zip entry: {e}"))?;
8185

8286
entries.push(ZipEntry {
8387
name: file.name().to_string(),
@@ -91,42 +95,42 @@ pub fn zip_list(path: String) -> Result<Vec<ZipEntry>, String> {
9195
}
9296

9397
/// Extract single file from zip
98+
#[allow(clippy::needless_pass_by_value)]
9499
#[tauri::command]
95100
pub fn zip_extract_file(zip_path: String, entry_name: String) -> Result<Vec<u8>, String> {
96101
if entry_name.contains("..") || entry_name.starts_with('/') || entry_name.starts_with('\\') {
97102
return Err("Invalid zip entry name: must not contain '..' or start with '/'".to_string());
98103
}
99104

100-
let file = File::open(&zip_path).map_err(|e| format!("Failed to open zip: {}", e))?;
105+
let file = File::open(&zip_path).map_err(|e| format!("Failed to open zip: {e}"))?;
101106

102-
let mut archive =
103-
zip::ZipArchive::new(file).map_err(|e| format!("Failed to read zip: {}", e))?;
107+
let mut archive = zip::ZipArchive::new(file).map_err(|e| format!("Failed to read zip: {e}"))?;
104108

105109
let entry = archive
106110
.by_name(&entry_name)
107-
.map_err(|e| format!("Entry not found: {}", e))?;
111+
.map_err(|e| format!("Entry not found: {e}"))?;
108112

109113
let entry_size = entry.size();
110114
if entry_size > MAX_DECOMPRESSED_SIZE {
111115
return Err(format!(
112-
"Zip entry too large: {} bytes (limit: {} bytes)",
113-
entry_size, MAX_DECOMPRESSED_SIZE
116+
"Zip entry too large: {entry_size} bytes (limit: {MAX_DECOMPRESSED_SIZE} bytes)"
114117
));
115118
}
116119

117120
let mut limited = entry.take(MAX_DECOMPRESSED_SIZE + 1);
118121
let mut result = Vec::with_capacity(entry_size.min(MAX_DECOMPRESSED_SIZE) as usize);
119122
limited
120123
.read_to_end(&mut result)
121-
.map_err(|e| format!("Failed to read entry: {}", e))?;
124+
.map_err(|e| format!("Failed to read entry: {e}"))?;
122125

123126
Ok(result)
124127
}
125128

126129
/// Compress directory to zip
130+
#[allow(clippy::needless_pass_by_value)]
127131
#[tauri::command]
128132
pub fn zip_create(source_dir: String, zip_path: String) -> Result<(), String> {
129-
let file = File::create(&zip_path).map_err(|e| format!("Failed to create zip: {}", e))?;
133+
let file = File::create(&zip_path).map_err(|e| format!("Failed to create zip: {e}"))?;
130134

131135
let mut zip = zip::ZipWriter::new(file);
132136
let options = zip::write::SimpleFileOptions::default()
@@ -135,28 +139,27 @@ pub fn zip_create(source_dir: String, zip_path: String) -> Result<(), String> {
135139
let walkdir = walkdir::WalkDir::new(&source_dir);
136140
let source_path = std::path::Path::new(&source_dir);
137141

138-
for entry in walkdir.into_iter().filter_map(|e| e.ok()) {
142+
for entry in walkdir.into_iter().filter_map(std::result::Result::ok) {
139143
let path = entry.path();
140144
let name = path
141145
.strip_prefix(source_path)
142-
.map_err(|e| format!("Path error: {}", e))?
146+
.map_err(|e| format!("Path error: {e}"))?
143147
.to_string_lossy();
144148

145149
if path.is_file() {
146150
zip.start_file(name, options)
147-
.map_err(|e| format!("Failed to start file: {}", e))?;
151+
.map_err(|e| format!("Failed to start file: {e}"))?;
148152

149-
let mut f = File::open(path).map_err(|e| format!("Failed to open file: {}", e))?;
150-
std::io::copy(&mut f, &mut zip)
151-
.map_err(|e| format!("Failed to write to zip: {}", e))?;
153+
let mut f = File::open(path).map_err(|e| format!("Failed to open file: {e}"))?;
154+
std::io::copy(&mut f, &mut zip).map_err(|e| format!("Failed to write to zip: {e}"))?;
152155
} else if !name.is_empty() {
153156
zip.add_directory(name, options)
154-
.map_err(|e| format!("Failed to add directory: {}", e))?;
157+
.map_err(|e| format!("Failed to add directory: {e}"))?;
155158
}
156159
}
157160

158161
zip.finish()
159-
.map_err(|e| format!("Failed to finish zip: {}", e))?;
162+
.map_err(|e| format!("Failed to finish zip: {e}"))?;
160163

161164
Ok(())
162165
}

src-tauri/src/commands/crypto.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ use std::io::Read;
55

66
const HASH_BUF_SIZE: usize = 64 * 1024;
77

8+
#[allow(clippy::needless_pass_by_value)]
89
#[tauri::command]
910
pub fn sha256_hash(data: Vec<u8>) -> String {
1011
let mut hasher = Sha256::new();
1112
hasher.update(&data);
1213
format!("{:x}", hasher.finalize())
1314
}
1415

16+
#[allow(clippy::needless_pass_by_value, clippy::large_stack_arrays)]
1517
#[tauri::command]
1618
pub fn sha256_file(path: String) -> Result<String, String> {
17-
let file = std::fs::File::open(&path).map_err(|e| format!("Failed to open file: {}", e))?;
19+
let file = std::fs::File::open(&path).map_err(|e| format!("Failed to open file: {e}"))?;
1820
let mut reader = std::io::BufReader::with_capacity(HASH_BUF_SIZE, file);
1921
let mut hasher = Sha256::new();
2022
let mut buf = [0u8; HASH_BUF_SIZE];
2123

2224
loop {
2325
let n = reader
2426
.read(&mut buf)
25-
.map_err(|e| format!("Failed to read file: {}", e))?;
27+
.map_err(|e| format!("Failed to read file: {e}"))?;
2628
if n == 0 {
2729
break;
2830
}
@@ -32,22 +34,24 @@ pub fn sha256_file(path: String) -> Result<String, String> {
3234
Ok(format!("{:x}", hasher.finalize()))
3335
}
3436

37+
#[allow(clippy::needless_pass_by_value)]
3538
#[tauri::command]
3639
pub fn md5_hash(data: Vec<u8>) -> String {
3740
format!("{:x}", md5::compute(&data))
3841
}
3942

43+
#[allow(clippy::needless_pass_by_value, clippy::large_stack_arrays)]
4044
#[tauri::command]
4145
pub fn md5_file(path: String) -> Result<String, String> {
42-
let file = std::fs::File::open(&path).map_err(|e| format!("Failed to open file: {}", e))?;
46+
let file = std::fs::File::open(&path).map_err(|e| format!("Failed to open file: {e}"))?;
4347
let mut reader = std::io::BufReader::with_capacity(HASH_BUF_SIZE, file);
4448
let mut context = md5::Context::new();
4549
let mut buf = [0u8; HASH_BUF_SIZE];
4650

4751
loop {
4852
let n = reader
4953
.read(&mut buf)
50-
.map_err(|e| format!("Failed to read file: {}", e))?;
54+
.map_err(|e| format!("Failed to read file: {e}"))?;
5155
if n == 0 {
5256
break;
5357
}
@@ -70,32 +74,36 @@ pub fn uuid_v4() -> String {
7074
uuid::Uuid::new_v4().to_string()
7175
}
7276

77+
#[allow(clippy::needless_pass_by_value)]
7378
#[tauri::command]
7479
pub fn base64_encode(data: Vec<u8>) -> String {
7580
use base64::{engine::general_purpose::STANDARD, Engine as _};
7681
STANDARD.encode(&data)
7782
}
7883

84+
#[allow(clippy::needless_pass_by_value)]
7985
#[tauri::command]
8086
pub fn base64_decode(text: String) -> Result<Vec<u8>, String> {
8187
use base64::{engine::general_purpose::STANDARD, Engine as _};
8288
STANDARD
8389
.decode(&text)
84-
.map_err(|e| format!("Base64 decode error: {}", e))
90+
.map_err(|e| format!("Base64 decode error: {e}"))
8591
}
8692

93+
#[allow(clippy::needless_pass_by_value)]
8794
#[tauri::command]
8895
pub fn base64_encode_urlsafe(data: Vec<u8>) -> String {
8996
use base64::{engine::general_purpose::URL_SAFE, Engine as _};
9097
URL_SAFE.encode(&data)
9198
}
9299

100+
#[allow(clippy::needless_pass_by_value)]
93101
#[tauri::command]
94102
pub fn base64_decode_urlsafe(text: String) -> Result<Vec<u8>, String> {
95103
use base64::{engine::general_purpose::URL_SAFE, Engine as _};
96104
URL_SAFE
97105
.decode(&text)
98-
.map_err(|e| format!("Base64 decode error: {}", e))
106+
.map_err(|e| format!("Base64 decode error: {e}"))
99107
}
100108

101109
#[derive(Debug, Serialize)]
@@ -106,12 +114,13 @@ pub struct FileHashInfo {
106114
pub size: u64,
107115
}
108116

117+
#[allow(clippy::needless_pass_by_value, clippy::large_stack_arrays)]
109118
#[tauri::command]
110119
pub fn file_hashes(path: String) -> Result<FileHashInfo, String> {
111-
let file = std::fs::File::open(&path).map_err(|e| format!("Failed to open file: {}", e))?;
120+
let file = std::fs::File::open(&path).map_err(|e| format!("Failed to open file: {e}"))?;
112121
let file_size = file
113122
.metadata()
114-
.map_err(|e| format!("Failed to get metadata: {}", e))?
123+
.map_err(|e| format!("Failed to get metadata: {e}"))?
115124
.len();
116125

117126
let mut reader = std::io::BufReader::with_capacity(HASH_BUF_SIZE, file);
@@ -122,7 +131,7 @@ pub fn file_hashes(path: String) -> Result<FileHashInfo, String> {
122131
loop {
123132
let n = reader
124133
.read(&mut buf)
125-
.map_err(|e| format!("Failed to read file: {}", e))?;
134+
.map_err(|e| format!("Failed to read file: {e}"))?;
126135
if n == 0 {
127136
break;
128137
}

0 commit comments

Comments
 (0)