From 7dc9f45d1aa47b145b487e613e5a5518f74adfa4 Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Mon, 6 Nov 2023 10:32:12 +0100 Subject: [PATCH] chore: refactor Controller initialization to use `from` method instead of `try_from` for improved code readability and consistency --- packages/butler/src/app.rs | 2 +- packages/tauri/src/app.rs | 10 +++++----- packages/tauri/src/bin.rs | 6 ++---- packages/tauri/src/commands.rs | 4 +--- packages/tauri/src/git/index.rs | 4 ++-- .../tauri/src/watcher/handlers/fetch_gitbutler_data.rs | 2 +- .../tauri/src/watcher/handlers/fetch_project_data.rs | 4 ++-- packages/tauri/src/watcher/handlers/git_file_change.rs | 2 +- packages/tauri/src/watcher/handlers/index_handler.rs | 6 +++--- packages/tauri/src/watcher/handlers/mod.rs | 2 +- .../tauri/src/watcher/handlers/project_file_change.rs | 2 +- .../tauri/src/watcher/handlers/push_gitbutler_data.rs | 2 +- packages/tauri/src/watcher/handlers/tick_handler.rs | 2 +- 13 files changed, 22 insertions(+), 26 deletions(-) diff --git a/packages/butler/src/app.rs b/packages/butler/src/app.rs index 566e5fe8b..56183c289 100644 --- a/packages/butler/src/app.rs +++ b/packages/butler/src/app.rs @@ -22,7 +22,7 @@ impl App { let storage = storage::Storage::from(&local_data_dir); let users_storage = users::Controller::from(&storage); - let projects_storage = projects::Controller::try_from(&local_data_dir)?; + let projects_storage = projects::Controller::from(&local_data_dir); let projects = projects_storage.list().context("failed to list projects")?; let project = projects diff --git a/packages/tauri/src/app.rs b/packages/tauri/src/app.rs index 5c395acdf..8bb45a92e 100644 --- a/packages/tauri/src/app.rs +++ b/packages/tauri/src/app.rs @@ -46,13 +46,13 @@ impl TryFrom<&AppHandle> for App { fn try_from(value: &AppHandle) -> std::result::Result { Ok(Self { local_data_dir: DataDir::try_from(value)?, - keys: keys::Controller::try_from(value)?, + keys: keys::Controller::from(value), projects: projects::Controller::try_from(value)?, - users: users::Controller::try_from(value)?, + users: users::Controller::from(value), watchers: value.state::().inner().clone(), - sessions_database: sessions::Database::try_from(value)?, - deltas_database: deltas::Database::try_from(value)?, - bookmarks_database: bookmarks::Database::try_from(value)?, + sessions_database: sessions::Database::from(value), + deltas_database: deltas::Database::from(value), + bookmarks_database: bookmarks::Database::from(value), }) } } diff --git a/packages/tauri/src/bin.rs b/packages/tauri/src/bin.rs index 4ec7e2355..be026ba06 100644 --- a/packages/tauri/src/bin.rs +++ b/packages/tauri/src/bin.rs @@ -127,12 +127,10 @@ fn main() { .expect("failed to initialize virtual branches controller"); app_handle.manage(vbranch_contoller); - let keys_controller = keys::Controller::try_from(&app_handle) - .expect("failed to initialize keys controller"); + let keys_controller = keys::Controller::from(&app_handle); app_handle.manage(keys_controller); - let users_controller = users::Controller::try_from(&app_handle) - .expect("failed to initialize users controller"); + let users_controller = users::Controller::from(&app_handle); if let Some(user) = users_controller.get_user().context("failed to get user")? { sentry::configure_scope(|scope| scope.set_user(Some(user.clone().into()))); } diff --git a/packages/tauri/src/commands.rs b/packages/tauri/src/commands.rs index b36ef8d31..1eb9dc022 100644 --- a/packages/tauri/src/commands.rs +++ b/packages/tauri/src/commands.rs @@ -174,9 +174,7 @@ pub async fn upsert_bookmark( })?; let bookmark = bookmarks::Bookmark { project_id, - timestamp_ms: timestamp_ms - .try_into() - .context("failed to convert timestamp")?, + timestamp_ms: timestamp_ms.into(), created_timestamp_ms: now, updated_timestamp_ms: now, note, diff --git a/packages/tauri/src/git/index.rs b/packages/tauri/src/git/index.rs index 125655da8..7716b4db2 100644 --- a/packages/tauri/src/git/index.rs +++ b/packages/tauri/src/git/index.rs @@ -114,11 +114,11 @@ impl From for IndexEntry { fn from(value: git2::IndexEntry) -> Self { Self { ctime: FileTime::from_unix_time( - i64::try_from(value.ctime.seconds()).unwrap(), + i64::from(value.ctime.seconds()), value.ctime.nanoseconds(), ), mtime: FileTime::from_unix_time( - i64::try_from(value.mtime.seconds()).unwrap(), + i64::from(value.mtime.seconds()), value.mtime.nanoseconds(), ), dev: value.dev, diff --git a/packages/tauri/src/watcher/handlers/fetch_gitbutler_data.rs b/packages/tauri/src/watcher/handlers/fetch_gitbutler_data.rs index ef629b03f..a78364e48 100644 --- a/packages/tauri/src/watcher/handlers/fetch_gitbutler_data.rs +++ b/packages/tauri/src/watcher/handlers/fetch_gitbutler_data.rs @@ -55,7 +55,7 @@ impl TryFrom<&AppHandle> for HandlerInner { Ok(Self { local_data_dir, projects: projects::Controller::try_from(value)?, - users: users::Controller::try_from(value)?, + users: users::Controller::from(value), }) } } diff --git a/packages/tauri/src/watcher/handlers/fetch_project_data.rs b/packages/tauri/src/watcher/handlers/fetch_project_data.rs index 790f34362..4702e9b08 100644 --- a/packages/tauri/src/watcher/handlers/fetch_project_data.rs +++ b/packages/tauri/src/watcher/handlers/fetch_project_data.rs @@ -59,9 +59,9 @@ impl TryFrom<&AppHandle> for HandlerInner { fn try_from(value: &AppHandle) -> std::result::Result { Ok(Self { local_data_dir: DataDir::try_from(value)?, - keys: keys::Controller::try_from(value)?, + keys: keys::Controller::from(value), projects: projects::Controller::try_from(value)?, - users: users::Controller::try_from(value)?, + users: users::Controller::from(value), }) } } diff --git a/packages/tauri/src/watcher/handlers/git_file_change.rs b/packages/tauri/src/watcher/handlers/git_file_change.rs index a60fe0545..03b4c9304 100644 --- a/packages/tauri/src/watcher/handlers/git_file_change.rs +++ b/packages/tauri/src/watcher/handlers/git_file_change.rs @@ -25,7 +25,7 @@ impl TryFrom<&AppHandle> for Handler { Ok(Self { local_data_dir: DataDir::try_from(value)?, projects: projects::Controller::try_from(value)?, - users: users::Controller::try_from(value)?, + users: users::Controller::from(value), }) } } diff --git a/packages/tauri/src/watcher/handlers/index_handler.rs b/packages/tauri/src/watcher/handlers/index_handler.rs index bce2adcfd..461308473 100644 --- a/packages/tauri/src/watcher/handlers/index_handler.rs +++ b/packages/tauri/src/watcher/handlers/index_handler.rs @@ -32,9 +32,9 @@ impl TryFrom<&AppHandle> for Handler { local_data_dir: DataDir::try_from(value)?, projects: projects::Controller::try_from(value)?, users: users::Controller::from(value), - sessions_database: sessions::Database::try_from(value)?, - deltas_database: deltas::Database::try_from(value)?, - bookmarks_database: bookmarks::Database::try_from(value)?, + sessions_database: sessions::Database::from(value), + deltas_database: deltas::Database::from(value), + bookmarks_database: bookmarks::Database::from(value), }) } } diff --git a/packages/tauri/src/watcher/handlers/mod.rs b/packages/tauri/src/watcher/handlers/mod.rs index 1966db882..38399174e 100644 --- a/packages/tauri/src/watcher/handlers/mod.rs +++ b/packages/tauri/src/watcher/handlers/mod.rs @@ -47,7 +47,7 @@ impl TryFrom<&AppHandle> for Handler { push_gitbutler_handler: push_gitbutler_data::Handler::try_from(value)?, fetch_project_handler: fetch_project_data::Handler::try_from(value)?, fetch_gitbutler_handler: fetch_gitbutler_data::Handler::try_from(value)?, - analytics_handler: analytics_handler::Handler::try_from(value)?, + analytics_handler: analytics_handler::Handler::from(value), push_project_to_gitbutler: push_project_to_gitbutler::Handler::try_from(value)?, }) } diff --git a/packages/tauri/src/watcher/handlers/project_file_change.rs b/packages/tauri/src/watcher/handlers/project_file_change.rs index e09cecc18..ad88ed2b2 100644 --- a/packages/tauri/src/watcher/handlers/project_file_change.rs +++ b/packages/tauri/src/watcher/handlers/project_file_change.rs @@ -38,7 +38,7 @@ impl TryFrom<&AppHandle> for Handler { Ok(Self { local_data_dir: DataDir::try_from(value)?, projects: projects::Controller::try_from(value)?, - users: users::Controller::try_from(value)?, + users: users::Controller::from(value), }) } } diff --git a/packages/tauri/src/watcher/handlers/push_gitbutler_data.rs b/packages/tauri/src/watcher/handlers/push_gitbutler_data.rs index 128bd1d0a..00754d7c4 100644 --- a/packages/tauri/src/watcher/handlers/push_gitbutler_data.rs +++ b/packages/tauri/src/watcher/handlers/push_gitbutler_data.rs @@ -47,7 +47,7 @@ impl TryFrom<&AppHandle> for HandlerInner { Ok(Self::new( DataDir::try_from(value)?, projects::Controller::try_from(value)?, - users::Controller::try_from(value)?, + users::Controller::from(value), )) } } diff --git a/packages/tauri/src/watcher/handlers/tick_handler.rs b/packages/tauri/src/watcher/handlers/tick_handler.rs index fe5853044..92346de8d 100644 --- a/packages/tauri/src/watcher/handlers/tick_handler.rs +++ b/packages/tauri/src/watcher/handlers/tick_handler.rs @@ -27,7 +27,7 @@ impl TryFrom<&AppHandle> for Handler { Ok(Self { local_data_dir: DataDir::try_from(value)?, projects: projects::Controller::try_from(value)?, - users: users::Controller::try_from(value)?, + users: users::Controller::from(value), }) } }