Skip to content

Commit 2826db0

Browse files
chore: Rust linting
1 parent f2d47d5 commit 2826db0

File tree

6 files changed

+45
-41
lines changed

6 files changed

+45
-41
lines changed

Diff for: build.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const COMMANDS: &[&str] = &["create", "list", "get", "start", "cancel", "pause", "resume"];
1+
const COMMANDS: &[&str] = &[
2+
"create", "list", "get", "start", "cancel", "pause", "resume",
3+
];
24

35
fn main() {
4-
tauri_plugin::Builder::new(COMMANDS)
5-
.build();
6+
tauri_plugin::Builder::new(COMMANDS).build();
67
}

Diff for: package.json

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
"eslint": "eslint .",
2626
"markdownlint": "markdownlint-cli2",
2727
"standards": "npm run commitlint && npm run eslint && npm run markdownlint",
28+
"rust:lint": "cargo clippy --all-targets --all-features -- -D warnings",
29+
"rust:fix": "cargo fmt --all",
2830
"prepare": "pnpm build"
2931
},
3032
"dependencies": {

Diff for: src/error.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ pub enum Error {
88
InvalidState,
99

1010
#[error("Store Error: {0}")]
11-
StoreError(String),
11+
Store(String),
1212

1313
#[error("File Error: {0}")]
14-
FileError(String),
14+
File(String),
1515

1616
#[error("HTTP Error: {0}")]
17-
HttpError(String),
18-
19-
#[error("Event Error: {0}")]
20-
EventError(String),
17+
Http(String),
2118

2219
#[error(transparent)]
2320
Io(#[from] std::io::Error),

Diff for: src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use tauri::{
2-
plugin::{Builder, TauriPlugin}, Manager, RunEvent, Runtime
2+
plugin::{Builder, TauriPlugin},
3+
Manager, RunEvent, Runtime,
34
};
45

56
pub use models::*;
@@ -54,11 +55,12 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
5455
});
5556

5657
Ok(())
57-
}).on_event(|app_handle, event| {
58+
})
59+
.on_event(|app_handle, event| {
5860
if let RunEvent::Ready = event {
5961
// Initialize the download plugin.
6062
app_handle.state::<Download<R>>().init();
6163
}
62-
})
64+
})
6365
.build()
6466
}

Diff for: src/shared.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use tauri::{plugin::PluginApi, Emitter, Runtime};
88
use tauri_plugin_http::reqwest;
99
use tauri_plugin_http::reqwest::header::{HeaderMap, RANGE};
1010

11-
use crate::{models::*, store, utils};
1211
use crate::Error;
12+
use crate::{models::*, store, utils};
1313

1414
static DOWNLOAD_SUFFIX: &str = ".download";
1515

@@ -29,8 +29,7 @@ impl<R: Runtime> Download<R> {
2929
/// Updates the state of any download operations which are still marked as "In Progress". This can occur if the
3030
/// application was suspended or terminated before a download was completed.
3131
///
32-
pub fn init(&self)
33-
{
32+
pub fn init(&self) {
3433
let records: Vec<_> = store::get_records(&self.0)
3534
.unwrap()
3635
.iter()
@@ -39,13 +38,13 @@ impl<R: Runtime> Download<R> {
3938
.collect();
4039

4140
records.into_iter().for_each(|record| {
42-
let new_state = if record.progress == 0.0 { DownloadState::Created } else { DownloadState::Paused };
41+
let new_state = if record.progress == 0.0 {
42+
DownloadState::Created
43+
} else {
44+
DownloadState::Paused
45+
};
4346
store::update_record(&self.0, record.with_state(new_state.clone())).unwrap();
44-
println!(
45-
"[{}] Found download operation - {}",
46-
&record.key,
47-
new_state
48-
);
47+
println!("[{}] Found download operation - {}", &record.key, new_state);
4948
});
5049
}
5150

@@ -239,13 +238,13 @@ impl<R: Runtime> Download<R> {
239238
let response = match client.get(&record.url).headers(headers).send().await {
240239
Ok(res) => res,
241240
Err(e) => {
242-
return Err(Error::HttpError(format!("Failed to send request: {}", e)));
241+
return Err(Error::Http(format!("Failed to send request: {}", e)));
243242
}
244243
};
245244

246245
// Ensure the server supports partial downloads.
247246
if downloaded_size > 0 && response.status() != reqwest::StatusCode::PARTIAL_CONTENT {
248-
return Err(Error::HttpError(
247+
return Err(Error::Http(
249248
"Server does not support partial downloads".to_string(),
250249
));
251250
}
@@ -270,7 +269,7 @@ impl<R: Runtime> Download<R> {
270269
.create(true)
271270
.append(true)
272271
.open(&record.path)
273-
.map_err(|e| Error::FileError(format!("Failed to open file: {}", e)))?;
272+
.map_err(|e| Error::File(format!("Failed to open file: {}", e)))?;
274273

275274
// Write the response body to the file in chunks.
276275
let mut downloaded = downloaded_size;
@@ -288,7 +287,7 @@ impl<R: Runtime> Download<R> {
288287
Ok(data) => {
289288
file
290289
.write_all(&data)
291-
.map_err(|e| Error::FileError(format!("Failed to write file: {}", e)))?;
290+
.map_err(|e| Error::File(format!("Failed to write file: {}", e)))?;
292291

293292
downloaded += data.len() as u64;
294293
let progress = (downloaded as f64 / total_size as f64) * 100.0;
@@ -314,9 +313,12 @@ impl<R: Runtime> Download<R> {
314313

315314
let output_path = utils::remove_suffix(&record.path, DOWNLOAD_SUFFIX);
316315
fs::rename(&record.path, output_path)?;
317-
Download::emit_changed(app, record
318-
.with_path(output_path.into())
319-
.with_state(DownloadState::Completed));
316+
Download::emit_changed(
317+
app,
318+
record
319+
.with_path(output_path.into())
320+
.with_state(DownloadState::Completed),
321+
);
320322
}
321323
}
322324
// Download was paused.
@@ -338,8 +340,8 @@ impl<R: Runtime> Download<R> {
338340
fs::remove_file(&record.path)?;
339341
}
340342

341-
return Err(Error::HttpError(format!("Failed to download: {}", e)))
342-
},
343+
return Err(Error::Http(format!("Failed to download: {}", e)));
344+
}
343345
}
344346
}
345347

Diff for: src/store.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ static DOWNLOAD_STORE_PATH: &str = "downloads.json";
88
pub fn get_records<R: Runtime>(app: &AppHandle<R>) -> crate::Result<Vec<DownloadRecord>> {
99
let store = app
1010
.store(DOWNLOAD_STORE_PATH)
11-
.map_err(|e| Error::StoreError(format!("Failed to load store: {}", e)))?;
11+
.map_err(|e| Error::Store(format!("Failed to load store: {}", e)))?;
1212

1313
let mut records = Vec::new();
1414
for key in store.keys() {
1515
if let Some(value) = store.get(&key) {
1616
let record: DownloadRecord = serde_json::from_value(value)
17-
.map_err(|e| Error::StoreError(format!("Failed to parse record: {}", e)))?;
17+
.map_err(|e| Error::Store(format!("Failed to parse record: {}", e)))?;
1818
records.push(record);
1919
}
2020
}
@@ -25,11 +25,11 @@ pub fn get_records<R: Runtime>(app: &AppHandle<R>) -> crate::Result<Vec<Download
2525
pub fn get_record<R: Runtime>(app: &AppHandle<R>, key: String) -> crate::Result<DownloadRecord> {
2626
let store = app
2727
.store(DOWNLOAD_STORE_PATH)
28-
.map_err(|e| Error::StoreError(format!("Failed to load store: {}", e)))?;
28+
.map_err(|e| Error::Store(format!("Failed to load store: {}", e)))?;
2929

3030
match store.get(&key) {
3131
Some(value) => Ok(serde_json::from_value(value).unwrap()),
32-
None => Err(Error::StoreError(format!(
32+
None => Err(Error::Store(format!(
3333
"No download record found for key: {}",
3434
key
3535
))),
@@ -42,11 +42,11 @@ pub fn create_record<R: Runtime>(
4242
) -> crate::Result<DownloadRecord> {
4343
let store = app
4444
.store(DOWNLOAD_STORE_PATH)
45-
.map_err(|e| Error::StoreError(format!("Failed to load store: {}", e)))?;
45+
.map_err(|e| Error::Store(format!("Failed to load store: {}", e)))?;
4646

4747
match store.get(&record.key) {
4848
Some(_) => {
49-
return Err(Error::StoreError(format!(
49+
return Err(Error::Store(format!(
5050
"Record already exists for key: {}",
5151
&record.key
5252
)))
@@ -55,7 +55,7 @@ pub fn create_record<R: Runtime>(
5555
store.set(&record.key, serde_json::to_value(&record).unwrap());
5656
store
5757
.save()
58-
.map_err(|e| Error::StoreError(format!("Failed to save store: {}", e)))?;
58+
.map_err(|e| Error::Store(format!("Failed to save store: {}", e)))?;
5959
}
6060
}
6161

@@ -65,28 +65,28 @@ pub fn create_record<R: Runtime>(
6565
pub fn update_record<R: Runtime>(app: &AppHandle<R>, record: DownloadRecord) -> crate::Result<()> {
6666
let store = app
6767
.store(DOWNLOAD_STORE_PATH)
68-
.map_err(|e| Error::StoreError(format!("Failed to load store: {}", e)))?;
68+
.map_err(|e| Error::Store(format!("Failed to load store: {}", e)))?;
6969

7070
store.set(&record.key, serde_json::to_value(&record).unwrap());
7171
store
7272
.save()
73-
.map_err(|e| Error::StoreError(format!("Failed to save store: {}", e)))?;
73+
.map_err(|e| Error::Store(format!("Failed to save store: {}", e)))?;
7474

7575
Ok(())
7676
}
7777

7878
pub fn remove_record<R: Runtime>(app: &AppHandle<R>, key: String) -> crate::Result<()> {
7979
let store = app
8080
.store(DOWNLOAD_STORE_PATH)
81-
.map_err(|e| Error::StoreError(format!("Failed to load store: {}", e)))?;
81+
.map_err(|e| Error::Store(format!("Failed to load store: {}", e)))?;
8282

8383
if store.has(&key) {
8484
store.delete(&key);
8585
}
8686

8787
store
8888
.save()
89-
.map_err(|e| Error::StoreError(format!("Failed to save store: {}", e)))?;
89+
.map_err(|e| Error::Store(format!("Failed to save store: {}", e)))?;
9090

9191
Ok(())
9292
}

0 commit comments

Comments
 (0)