From 4e81c3c76cd8c04aa2cf912c09817ca75dd27183 Mon Sep 17 00:00:00 2001 From: Jiayi Zhao Date: Mon, 22 Apr 2019 20:52:17 -0400 Subject: code cleanup and implement std::default::Default --- src/config/config.rs | 27 ++++++++------------------- src/config/keymap.rs | 22 ++++++++++------------ src/config/mimetype.rs | 24 ++++++++---------------- src/config/theme.rs | 14 ++++++++------ 4 files changed, 34 insertions(+), 53 deletions(-) (limited to 'src/config') diff --git a/src/config/config.rs b/src/config/config.rs index eecefb3..4de38a6 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -22,19 +22,6 @@ pub struct JoshutoRawConfig { column_ratio: Option<[usize; 3]>, } -impl JoshutoRawConfig { - #[allow(dead_code)] - pub fn new() -> Self { - JoshutoRawConfig { - scroll_offset: None, - tilde_in_titlebar: None, - sort_method: None, - sort_option: None, - column_ratio: None, - } - } -} - impl Flattenable for JoshutoRawConfig { fn flatten(self) -> JoshutoConfig { let column_ratio = match self.column_ratio { @@ -99,7 +86,14 @@ pub struct JoshutoConfig { } impl JoshutoConfig { - pub fn new() -> Self { + pub fn get_config() -> JoshutoConfig { + parse_config_file::(CONFIG_FILE) + .unwrap_or_else(JoshutoConfig::default) + } +} + +impl std::default::Default for JoshutoConfig { + fn default() -> Self { let sort_option = sort::SortOption { show_hidden: false, directories_first: true, @@ -115,9 +109,4 @@ impl JoshutoConfig { column_ratio: (1, 3, 4), } } - - pub fn get_config() -> JoshutoConfig { - parse_config_file::(CONFIG_FILE) - .unwrap_or_else(JoshutoConfig::new) - } } diff --git a/src/config/keymap.rs b/src/config/keymap.rs index 4b44b2d..9fb2e7d 100644 --- a/src/config/keymap.rs +++ b/src/config/keymap.rs @@ -32,12 +32,8 @@ impl Flattenable for JoshutoRawKeymap { if let Some(maps) = self.mapcommand { for mapcommand in maps { match commands::from_args(mapcommand.command.as_str(), mapcommand.args.as_ref()) { - Some(command) => { - insert_keycommand(&mut keymaps, command, &mapcommand.keys[..]); - } - None => { - eprintln!("Unknown command: {}", mapcommand.command); - } + Some(command) => insert_keycommand(&mut keymaps, command, &mapcommand.keys[..]), + None => eprintln!("Unknown command: {}", mapcommand.command), } } } @@ -51,14 +47,16 @@ pub struct JoshutoKeymap { } impl JoshutoKeymap { - pub fn new() -> Self { - let keymaps = HashMap::new(); - JoshutoKeymap { keymaps } - } - pub fn get_config() -> JoshutoKeymap { parse_config_file::(KEYMAP_FILE) - .unwrap_or_else(JoshutoKeymap::new) + .unwrap_or_else(JoshutoKeymap::default) + } +} + +impl std::default::Default for JoshutoKeymap { + fn default() -> Self { + let keymaps = HashMap::new(); + JoshutoKeymap { keymaps } } } diff --git a/src/config/mimetype.rs b/src/config/mimetype.rs index 8c730c0..bdcf765 100644 --- a/src/config/mimetype.rs +++ b/src/config/mimetype.rs @@ -42,16 +42,6 @@ pub struct JoshutoRawMimetype { extension: Option>>, } -impl JoshutoRawMimetype { - #[allow(dead_code)] - pub fn new() -> Self { - JoshutoRawMimetype { - mimetype: None, - extension: None, - } - } -} - impl Flattenable for JoshutoRawMimetype { fn flatten(self) -> JoshutoMimetype { let mimetype = self.mimetype.unwrap_or_default(); @@ -71,15 +61,17 @@ pub struct JoshutoMimetype { } impl JoshutoMimetype { - pub fn new() -> Self { + pub fn get_config() -> JoshutoMimetype { + parse_config_file::(MIMETYPE_FILE) + .unwrap_or_else(JoshutoMimetype::default) + } +} + +impl std::default::Default for JoshutoMimetype { + fn default() -> Self { JoshutoMimetype { mimetype: HashMap::new(), extension: HashMap::new(), } } - - pub fn get_config() -> JoshutoMimetype { - parse_config_file::(MIMETYPE_FILE) - .unwrap_or_else(JoshutoMimetype::new) - } } diff --git a/src/config/theme.rs b/src/config/theme.rs index 31ccbdc..5de537c 100644 --- a/src/config/theme.rs +++ b/src/config/theme.rs @@ -153,7 +153,14 @@ pub struct JoshutoTheme { } impl JoshutoTheme { - pub fn new() -> Self { + pub fn get_config() -> JoshutoTheme { + parse_config_file::(crate::THEME_FILE) + .unwrap_or_else(JoshutoTheme::default) + } +} + +impl std::default::Default for JoshutoTheme { + fn default() -> Self { let mut colorpair: Vec = Vec::with_capacity(10); colorpair.push(JoshutoColorPair::new(2, 2, -1)); colorpair.push(JoshutoColorPair::new(3, 3, -1)); @@ -220,9 +227,4 @@ impl JoshutoTheme { ext: HashMap::new(), } } - - pub fn get_config() -> JoshutoTheme { - parse_config_file::(crate::THEME_FILE) - .unwrap_or_else(JoshutoTheme::new) - } } -- cgit v1.2.3