From f614f739ae055d03790d619a535053e4d51cca89 Mon Sep 17 00:00:00 2001 From: Mattias Granlund Date: Sat, 30 Mar 2024 20:47:23 +0100 Subject: [PATCH] Make more type conversion implicit in `commands.rs` --- gitbutler-app/src/commands.rs | 66 +++++++++-------------------------- 1 file changed, 17 insertions(+), 49 deletions(-) diff --git a/gitbutler-app/src/commands.rs b/gitbutler-app/src/commands.rs index 7cd362470..4aa15b71f 100644 --- a/gitbutler-app/src/commands.rs +++ b/gitbutler-app/src/commands.rs @@ -3,7 +3,9 @@ use std::{collections::HashMap, path}; use anyhow::Context; use gitbutler_core::{ error::{Code, Error}, - gb_repository, git, project_repository, projects, reader, + gb_repository, git, project_repository, + projects::{self, ProjectId}, + reader, sessions::SessionId, users, }; @@ -30,19 +32,11 @@ impl From for Error { #[instrument(skip(handle))] pub async fn list_session_files( handle: tauri::AppHandle, - project_id: &str, - session_id: &str, + project_id: ProjectId, + session_id: SessionId, paths: Option>, ) -> Result, Error> { let app = handle.state::(); - let session_id: SessionId = session_id.parse().map_err(|_| Error::UserError { - message: "Malformed session id".to_string(), - code: Code::Validation, - })?; - let project_id = project_id.parse().map_err(|_| Error::UserError { - code: Code::Validation, - message: "Malformed project id".to_string(), - })?; let files = app.list_session_files(&project_id, &session_id, paths.as_deref())?; Ok(files) } @@ -51,13 +45,9 @@ pub async fn list_session_files( #[instrument(skip(handle))] pub async fn git_remote_branches( handle: tauri::AppHandle, - project_id: &str, + project_id: ProjectId, ) -> Result, Error> { let app = handle.state::(); - let project_id = project_id.parse().map_err(|_| Error::UserError { - code: Code::Validation, - message: "Malformed project id".to_string(), - })?; let branches = app.git_remote_branches(&project_id)?; Ok(branches) } @@ -66,16 +56,12 @@ pub async fn git_remote_branches( #[instrument(skip(handle))] pub async fn git_test_push( handle: tauri::AppHandle, - project_id: &str, + project_id: ProjectId, remote_name: &str, branch_name: &str, ) -> Result<(), Error> { let app = handle.state::(); let helper = handle.state::(); - let project_id = project_id.parse().map_err(|_| Error::UserError { - code: Code::Validation, - message: "Malformed project id".to_string(), - })?; let askpass_broker = handle .state::() .inner() @@ -97,16 +83,12 @@ pub async fn git_test_push( #[instrument(skip(handle))] pub async fn git_test_fetch( handle: tauri::AppHandle, - project_id: &str, + project_id: ProjectId, remote_name: &str, action: Option, ) -> Result<(), Error> { let app = handle.state::(); let helper = handle.state::(); - let project_id = project_id.parse().map_err(|_| Error::UserError { - code: Code::Validation, - message: "Malformed project id".to_string(), - })?; let askpass_broker = handle .state::() .inner() @@ -125,23 +107,18 @@ pub async fn git_test_fetch( #[tauri::command(async)] #[instrument(skip(handle))] -pub async fn git_index_size(handle: tauri::AppHandle, project_id: &str) -> Result { +pub async fn git_index_size( + handle: tauri::AppHandle, + project_id: ProjectId, +) -> Result { let app = handle.state::(); - let project_id = project_id.parse().map_err(|_| Error::UserError { - code: Code::Validation, - message: "Malformed project id".to_string(), - })?; Ok(app.git_index_size(&project_id).expect("git index size")) } #[tauri::command(async)] #[instrument(skip(handle))] -pub async fn git_head(handle: tauri::AppHandle, project_id: &str) -> Result { +pub async fn git_head(handle: tauri::AppHandle, project_id: ProjectId) -> Result { let app = handle.state::(); - let project_id = project_id.parse().map_err(|_| Error::UserError { - code: Code::Validation, - message: "Malformed project id".to_string(), - })?; let head = app.git_head(&project_id)?; Ok(head) } @@ -158,14 +135,10 @@ pub async fn delete_all_data(handle: tauri::AppHandle) -> Result<(), Error> { #[instrument(skip(handle))] pub async fn mark_resolved( handle: tauri::AppHandle, - project_id: &str, + project_id: ProjectId, path: &str, ) -> Result<(), Error> { let app = handle.state::(); - let project_id = project_id.parse().map_err(|_| Error::UserError { - code: Code::Validation, - message: "Malformed project id".to_string(), - })?; app.mark_resolved(&project_id, path)?; Ok(()) } @@ -193,12 +166,7 @@ pub async fn git_get_global_config( #[tauri::command(async)] #[instrument(skip(handle))] -pub async fn project_flush_and_push(handle: tauri::AppHandle, id: &str) -> Result<(), Error> { - let project_id = id.parse().map_err(|_| Error::UserError { - code: Code::Validation, - message: "Malformed project id".into(), - })?; - +pub async fn project_flush_and_push(handle: tauri::AppHandle, id: ProjectId) -> Result<(), Error> { let users = handle.state::().inner().clone(); let projects = handle.state::().inner().clone(); let local_data_dir = handle @@ -206,7 +174,7 @@ pub async fn project_flush_and_push(handle: tauri::AppHandle, id: &str) -> Resul .app_data_dir() .context("failed to get app data dir")?; - let project = projects.get(&project_id).context("failed to get project")?; + let project = projects.get(&id).context("failed to get project")?; let user = users.get_user()?; let project_repository = project_repository::Repository::open(&project)?; let gb_repo = @@ -219,7 +187,7 @@ pub async fn project_flush_and_push(handle: tauri::AppHandle, id: &str) -> Resul { let watcher = handle.state::(); watcher - .post(watcher::Event::Flush(project_id, current_session)) + .post(watcher::Event::Flush(id, current_session)) .await .context("failed to post flush event")?; }