tenho seguinte código rust use reqwest::Client; use serde::Deserialize; use…
- User
tenho seguinte código rust
use reqwest::Client; use serde::Deserialize; use std::path::PathBuf; use std::sync::{Arc}; use std::sync::atomic::{AtomicBool, Ordering}; use tokio::sync::Notify; use futures_util::StreamExt; use tokio::io::AsyncWriteExt; use tokio::fs::OpenOptions; use tauri::{Window, Emitter}; use mlua::Lua; use std::env;
// Definição da struct
Update, que mantém informações sobre as atualizações #[derive(Debug, Deserialize)] struct Update { version: u32, file_name: String, merge_file: String, checksum: String, }// Definição de enum para mensagens enviadas ao frontend #[derive(Debug, Clone, serde::Serialize)] enum DownloadMessage { ConnectingToServer, ValidatingFileUrl, AuthenticatingWithServer, VerifyingFileIntegrity, ErrorNotFound, ErrorAccessDenied, ErrorServerUnavailable, RestartingDownload, FileAlreadyExists, PreparingDownload, DownloadProgress { current_file: usize, total_files: usize, file_name: String, downloaded_bytes: u64, total_bytes: u64, }, }
// Estrutura responsável por manter o estado do download (pausa/retomada) pub struct DownloadState { is_paused: Arc, notify: Arc, }
impl DownloadState { // Cria um novo
DownloadStatepub fn new() -> Self { Self { is_paused: Arc::new(AtomicBool::new(false)), notify: Arc::new(Notify::new()), } }code// Função que espera se o estado estiver pausado pub async fn wait_if_paused(&self) { while self.is_paused.load(Ordering::SeqCst) { self.notify.notified().await; } } // Pausa o download pub fn pause(&self) { self.is_paused.store(true, Ordering::SeqCst); } // Retoma o download pub fn resume(&self) { self.is_paused.store(false, Ordering::SeqCst); self.notify.notify_waiters(); }}
// Função para iniciar o download dos arquivos #[tauri::command] async fn download_files(window: Window) -> Result<(), String> { let download_state = Arc::new(DownloadState::new());
code - Assistant