From f8e1c26309bcac6c9dea05e9625993def45826fd Mon Sep 17 00:00:00 2001 From: Antonio Yang Date: Sat, 4 Mar 2023 22:51:09 +0800 Subject: [PATCH] Fix dep struct patch (#1577) --- Cargo.lock | 53 ++++++++++ Cargo.toml | 1 + src/keys/key_list.rs | 32 ++++--- src/keys/key_list_file.rs | 197 -------------------------------------- src/keys/mod.rs | 1 - 5 files changed, 75 insertions(+), 209 deletions(-) delete mode 100644 src/keys/key_list_file.rs diff --git a/Cargo.lock b/Cargo.lock index e10a1b06..4f40675b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -785,6 +785,7 @@ dependencies = [ "scopetime", "serde", "simplelog", + "struct-patch", "syntect", "tempfile", "textwrap", @@ -1392,6 +1393,30 @@ dependencies = [ "yansi", ] +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro-hack" version = "0.5.20+deprecated" @@ -1704,6 +1729,34 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "struct-patch" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a40c3718360af439b4119c3110bac97336af63b6da95069fca7f5c93fa41d10" +dependencies = [ + "struct-patch-derive", + "struct-patch-trait", +] + +[[package]] +name = "struct-patch-derive" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f586d0641263362666059ec7dc0c037f1a9f9da1b87f660e64a74ea4c88ab583" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "struct-patch-trait" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaeaa9346335d361de71460a808a85436bb39dbaa69b82c8be2ad9d3f21ea24a" + [[package]] name = "symbolic-common" version = "10.2.1" diff --git a/Cargo.toml b/Cargo.toml index 7b4c8010..d1e0cc80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,7 @@ scopeguard = "1.1" scopetime = { path = "./scopetime", version = "0.1" } serde = "1.0" simplelog = { version = "0.12", default-features = false } +struct-patch = "0.2" syntect = { version = "5.0", default-features = false, features = ["parsing", "default-syntaxes", "default-themes", "html"] } textwrap = "0.16" tui = { version = "0.19", default-features = false, features = ['crossterm', 'serde'] } diff --git a/src/keys/key_list.rs b/src/keys/key_list.rs index bebc3e8f..7e1406e5 100644 --- a/src/keys/key_list.rs +++ b/src/keys/key_list.rs @@ -1,8 +1,8 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use serde::{Deserialize, Serialize}; -use std::path::PathBuf; - -use super::key_list_file::KeysListFile; +use std::{fs::File, path::PathBuf}; +use struct_patch::traits::Patch as PatchTrait; +use struct_patch::Patch; #[derive(Debug, PartialOrd, Clone, Copy, Serialize, Deserialize)] pub struct GituiKeyEvent { @@ -34,7 +34,8 @@ impl From<&GituiKeyEvent> for KeyEvent { } } -#[derive(Clone)] +#[derive(Clone, Patch)] +#[patch_derive(Deserialize)] pub struct KeysList { pub tab_status: GituiKeyEvent, pub tab_log: GituiKeyEvent, @@ -202,14 +203,13 @@ impl Default for KeysList { impl KeysList { pub fn init(file: PathBuf) -> Self { - if file.exists() { - let file = KeysListFile::read_file(file) - .map_err(|e| log::error!("key binding error: {e}",)) - .unwrap_or_default(); - file.get_list() - } else { - Self::default() + let mut keys_list = Self::default(); + if let Ok(f) = File::open(file) { + if let Ok(patch) = ron::de::from_reader(f) { + keys_list.apply(patch); + } } + keys_list } } @@ -220,6 +220,16 @@ mod tests { use std::io::Write; use tempfile::NamedTempFile; + #[test] + fn test_apply_vim_style_example() { + let mut keys_list = KeysList::default(); + let f = File::open("vim_style_key_config.ron") + .expect("vim style config should exist"); + let patch = ron::de::from_reader(f) + .expect("vim style config format incorrect"); + keys_list.apply(patch); + } + #[test] fn test_smoke() { let mut file = NamedTempFile::new().unwrap(); diff --git a/src/keys/key_list_file.rs b/src/keys/key_list_file.rs deleted file mode 100644 index 7409d03b..00000000 --- a/src/keys/key_list_file.rs +++ /dev/null @@ -1,197 +0,0 @@ -use anyhow::Result; -use ron::{self}; -use serde::{Deserialize, Serialize}; -use std::{fs::File, io::Read, path::PathBuf}; - -use super::key_list::{GituiKeyEvent, KeysList}; - -#[derive(Serialize, Deserialize, Default)] -pub struct KeysListFile { - pub tab_status: Option, - pub tab_log: Option, - pub tab_files: Option, - pub tab_stashing: Option, - pub tab_stashes: Option, - pub tab_toggle: Option, - pub tab_toggle_reverse: Option, - pub toggle_workarea: Option, - pub exit: Option, - pub quit: Option, - pub exit_popup: Option, - pub open_commit: Option, - pub open_commit_editor: Option, - pub open_help: Option, - pub open_options: Option, - pub move_left: Option, - pub move_right: Option, - pub tree_collapse_recursive: Option, - pub tree_expand_recursive: Option, - pub home: Option, - pub end: Option, - pub move_up: Option, - pub move_down: Option, - pub popup_up: Option, - pub popup_down: Option, - pub page_down: Option, - pub page_up: Option, - pub shift_up: Option, - pub shift_down: Option, - pub enter: Option, - pub blame: Option, - pub edit_file: Option, - pub file_history: Option, - pub status_stage_all: Option, - pub status_reset_item: Option, - pub status_ignore_file: Option, - pub diff_stage_lines: Option, - pub diff_reset_lines: Option, - pub stashing_save: Option, - pub stashing_toggle_untracked: Option, - pub stashing_toggle_index: Option, - pub stash_apply: Option, - pub stash_open: Option, - pub stash_drop: Option, - pub cmd_bar_toggle: Option, - pub log_tag_commit: Option, - pub log_mark_commit: Option, - pub log_checkout_commit: Option, - pub log_reset_commit: Option, - pub log_reword_commit: Option, - pub commit_amend: Option, - pub toggle_verify: Option, - pub copy: Option, - pub create_branch: Option, - pub rename_branch: Option, - pub select_branch: Option, - pub delete_branch: Option, - pub merge_branch: Option, - pub rebase_branch: Option, - pub compare_commits: Option, - pub tags: Option, - pub delete_tag: Option, - pub select_tag: Option, - pub push: Option, - pub open_file_tree: Option, - pub file_find: Option, - pub force_push: Option, - pub fetch: Option, - pub pull: Option, - pub abort_merge: Option, - pub undo_commit: Option, - pub stage_unstage_item: Option, - pub tag_annotate: Option, - pub view_submodules: Option, - pub view_submodule_parent: Option, - pub update_dubmodule: Option, - pub commit_history_next: Option, -} - -impl KeysListFile { - pub fn read_file(config_file: PathBuf) -> Result { - let mut f = File::open(config_file)?; - let mut buffer = Vec::new(); - f.read_to_end(&mut buffer)?; - Ok(ron::de::from_bytes(&buffer)?) - } - - #[rustfmt::skip] - pub fn get_list(self) -> KeysList { - let default = KeysList::default(); - - KeysList { - tab_status: self.tab_status.unwrap_or(default.tab_status), - tab_log: self.tab_log.unwrap_or(default.tab_log), - tab_files: self.tab_files.unwrap_or(default.tab_files), - tab_stashing: self.tab_stashing.unwrap_or(default.tab_stashing), - tab_stashes: self.tab_stashes.unwrap_or(default.tab_stashes), - tab_toggle: self.tab_toggle.unwrap_or(default.tab_toggle), - tab_toggle_reverse: self.tab_toggle_reverse.unwrap_or(default.tab_toggle_reverse), - toggle_workarea: self.toggle_workarea.unwrap_or(default.toggle_workarea), - exit: self.exit.unwrap_or(default.exit), - quit: self.quit.unwrap_or(default.quit), - exit_popup: self.exit_popup.unwrap_or(default.exit_popup), - open_commit: self.open_commit.unwrap_or(default.open_commit), - open_commit_editor: self.open_commit_editor.unwrap_or(default.open_commit_editor), - open_help: self.open_help.unwrap_or(default.open_help), - open_options: self.open_options.unwrap_or(default.open_options), - move_left: self.move_left.unwrap_or(default.move_left), - move_right: self.move_right.unwrap_or(default.move_right), - tree_collapse_recursive: self.tree_collapse_recursive.unwrap_or(default.tree_collapse_recursive), - tree_expand_recursive: self.tree_expand_recursive.unwrap_or(default.tree_expand_recursive), - home: self.home.unwrap_or(default.home), - end: self.end.unwrap_or(default.end), - move_up: self.move_up.unwrap_or(default.move_up), - move_down: self.move_down.unwrap_or(default.move_down), - popup_up: self.popup_up.unwrap_or(default.popup_up), - popup_down: self.popup_down.unwrap_or(default.popup_down), - page_down: self.page_down.unwrap_or(default.page_down), - page_up: self.page_up.unwrap_or(default.page_up), - shift_up: self.shift_up.unwrap_or(default.shift_up), - shift_down: self.shift_down.unwrap_or(default.shift_down), - enter: self.enter.unwrap_or(default.enter), - blame: self.blame.unwrap_or(default.blame), - edit_file: self.edit_file.unwrap_or(default.edit_file), - file_history: self.file_history.unwrap_or(default.file_history), - status_stage_all: self.status_stage_all.unwrap_or(default.status_stage_all), - status_reset_item: self.status_reset_item.unwrap_or(default.status_reset_item), - status_ignore_file: self.status_ignore_file.unwrap_or(default.status_ignore_file), - diff_stage_lines: self.diff_stage_lines.unwrap_or(default.diff_stage_lines), - diff_reset_lines: self.diff_reset_lines.unwrap_or(default.diff_reset_lines), - stashing_save: self.stashing_save.unwrap_or(default.stashing_save), - stashing_toggle_untracked: self.stashing_toggle_untracked.unwrap_or(default.stashing_toggle_untracked), - stashing_toggle_index: self.stashing_toggle_index.unwrap_or(default.stashing_toggle_index), - stash_apply: self.stash_apply.unwrap_or(default.stash_apply), - stash_open: self.stash_open.unwrap_or(default.stash_open), - stash_drop: self.stash_drop.unwrap_or(default.stash_drop), - cmd_bar_toggle: self.cmd_bar_toggle.unwrap_or(default.cmd_bar_toggle), - log_tag_commit: self.log_tag_commit.unwrap_or(default.log_tag_commit), - log_mark_commit: self.log_mark_commit.unwrap_or(default.log_mark_commit), - log_checkout_commit: self.log_checkout_commit.unwrap_or(default.log_checkout_commit), - log_reset_comit: self.log_reset_commit.unwrap_or(default.log_reset_comit), - log_reword_comit: self.log_reword_commit.unwrap_or(default.log_reword_comit), - commit_amend: self.commit_amend.unwrap_or(default.commit_amend), - toggle_verify: self.toggle_verify.unwrap_or(default.toggle_verify), - copy: self.copy.unwrap_or(default.copy), - create_branch: self.create_branch.unwrap_or(default.create_branch), - rename_branch: self.rename_branch.unwrap_or(default.rename_branch), - select_branch: self.select_branch.unwrap_or(default.select_branch), - delete_branch: self.delete_branch.unwrap_or(default.delete_branch), - merge_branch: self.merge_branch.unwrap_or(default.merge_branch), - rebase_branch: self.rebase_branch.unwrap_or(default.rebase_branch), - compare_commits: self.compare_commits.unwrap_or(default.compare_commits), - tags: self.tags.unwrap_or(default.tags), - delete_tag: self.delete_tag.unwrap_or(default.delete_tag), - select_tag: self.select_tag.unwrap_or(default.select_tag), - push: self.push.unwrap_or(default.push), - open_file_tree: self.open_file_tree.unwrap_or(default.open_file_tree), - file_find: self.file_find.unwrap_or(default.file_find), - force_push: self.force_push.unwrap_or(default.force_push), - fetch: self.fetch.unwrap_or(default.fetch), - pull: self.pull.unwrap_or(default.pull), - abort_merge: self.abort_merge.unwrap_or(default.abort_merge), - undo_commit: self.undo_commit.unwrap_or(default.undo_commit), - stage_unstage_item: self.stage_unstage_item.unwrap_or(default.stage_unstage_item), - tag_annotate: self.tag_annotate.unwrap_or(default.tag_annotate), - view_submodules: self.view_submodules.unwrap_or(default.view_submodules), - view_submodule_parent: self.view_submodule_parent.unwrap_or(default.view_submodule_parent), - update_submodule: self.update_dubmodule.unwrap_or(default.update_submodule), - commit_history_next: self.commit_history_next.unwrap_or(default.commit_history_next), - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_load_vim_style_example() { - assert_eq!( - KeysListFile::read_file( - "vim_style_key_config.ron".into() - ) - .is_ok(), - true - ); - } -} diff --git a/src/keys/mod.rs b/src/keys/mod.rs index 657c980e..a770087f 100644 --- a/src/keys/mod.rs +++ b/src/keys/mod.rs @@ -1,6 +1,5 @@ mod key_config; mod key_list; -mod key_list_file; mod symbols; pub use key_config::{KeyConfig, SharedKeyConfig};