Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src-tauri/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ struct OperationUpdate<'a> {
update_type: &'a str,
step_id: &'a str,
extra_details: Option<AppError>,
progress: Option<f64>,
uploaded_bytes: Option<u64>,
total_bytes: Option<u64>,
}

impl<'a> Operation<'a> {
Expand All @@ -34,6 +37,9 @@ impl<'a> Operation<'a> {
update_type: "started",
step_id: id,
extra_details: None,
progress: None,
uploaded_bytes: None,
total_bytes: None,
},
)
.map_err(|e| AppError::OperationUpdate(e.to_string()))
Expand All @@ -47,6 +53,9 @@ impl<'a> Operation<'a> {
update_type: "finished",
step_id: id,
extra_details: None,
progress: Some(1.0),
uploaded_bytes: None,
total_bytes: None,
},
)
.map_err(|e| AppError::OperationUpdate(e.to_string()))
Expand All @@ -60,12 +69,53 @@ impl<'a> Operation<'a> {
update_type: "failed",
step_id: id,
extra_details: Some(error.clone()),
progress: None,
uploaded_bytes: None,
total_bytes: None,
},
)
.map_err(|e| AppError::OperationUpdate(e.to_string()))?;
Err(error)
}

pub fn progress(&self, id: &str, progress: f64) -> Result<(), AppError> {
self.window
.emit(
&format!("operation_{}", self.id),
OperationUpdate {
update_type: "progress",
step_id: id,
extra_details: None,
progress: Some(progress.clamp(0.0, 1.0)),
uploaded_bytes: None,
total_bytes: None,
},
)
.map_err(|e| AppError::OperationUpdate(e.to_string()))
}

pub fn progress_bytes(&self, id: &str, uploaded_bytes: u64, total_bytes: u64) -> Result<(), AppError> {
let normalized = if total_bytes == 0 {
0.0
} else {
(uploaded_bytes as f64 / total_bytes as f64).clamp(0.0, 1.0)
};

self.window
.emit(
&format!("operation_{}", self.id),
OperationUpdate {
update_type: "progress",
step_id: id,
extra_details: None,
progress: Some(normalized),
uploaded_bytes: Some(uploaded_bytes),
total_bytes: Some(total_bytes),
},
)
.map_err(|e| AppError::OperationUpdate(e.to_string()))
}

pub fn fail_if_err<T>(&self, id: &str, res: Result<T, AppError>) -> Result<T, AppError> {
match res {
Ok(t) => Ok(t),
Expand Down
Loading