Skip to content

refactor: remove uses of Option in new_with_config() #978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
5 changes: 2 additions & 3 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ mod tests {
test_helpers::{
DefaultTestModule,
TestModuleProvider,
create_config,
create_event_reader,
mocks,
with_git_directory,
Expand Down Expand Up @@ -289,7 +288,7 @@ mod tests {

#[test]
fn todo_file_options_without_command() {
let mut config = create_config();
let mut config = Config::default();
config.undo_limit = 10;
config.git.comment_char = String::from("#");
config.post_modified_line_exec_command = None;
Expand All @@ -303,7 +302,7 @@ mod tests {

#[test]
fn todo_file_options_with_command() {
let mut config = create_config();
let mut config = Config::default();
config.undo_limit = 10;
config.git.comment_char = String::from("#");
config.post_modified_line_exec_command = Some(String::from("command"));
Expand Down
14 changes: 7 additions & 7 deletions src/components/confirm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;
use crate::{
assert_rendered_output,
input::StandardEvent,
test_helpers::{assertions::assert_rendered_output::AssertRenderOptions, create_test_keybindings},
test_helpers::assertions::assert_rendered_output::AssertRenderOptions,
};

#[test]
Expand All @@ -23,47 +23,47 @@ fn render() {
#[test]
fn read_event_yes_uppercase() {
assert_eq!(
Confirm::read_event(Event::from('Y'), &create_test_keybindings()),
Confirm::read_event(Event::from('Y'), &KeyBindings::default()),
Event::from(StandardEvent::Yes)
);
}

#[test]
fn read_event_yes_lowercase() {
assert_eq!(
Confirm::read_event(Event::from('y'), &create_test_keybindings()),
Confirm::read_event(Event::from('y'), &KeyBindings::default()),
Event::from(StandardEvent::Yes)
);
}

#[test]
fn read_event_no_lowercase() {
assert_eq!(
Confirm::read_event(Event::from('n'), &create_test_keybindings()),
Confirm::read_event(Event::from('n'), &KeyBindings::default()),
Event::from(StandardEvent::No)
);
}

#[test]
fn read_event_no_uppercase() {
assert_eq!(
Confirm::read_event(Event::from('N'), &create_test_keybindings()),
Confirm::read_event(Event::from('N'), &KeyBindings::default()),
Event::from(StandardEvent::No)
);
}

#[test]
fn read_event_not_key_event() {
assert_eq!(
Confirm::read_event(Event::None, &create_test_keybindings()),
Confirm::read_event(Event::None, &KeyBindings::default()),
Event::None
);
}

#[test]
fn read_event_not_char_event() {
assert_eq!(
Confirm::read_event(Event::from(KeyCode::Backspace), &create_test_keybindings()),
Confirm::read_event(Event::from(KeyCode::Backspace), &KeyBindings::default()),
Event::from(KeyCode::Backspace)
);
}
Expand Down
31 changes: 25 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) struct Config {
}

impl Config {
pub(crate) fn new_with_config(git_config: Option<&crate::git::Config>) -> Result<Self, ConfigError> {
pub(crate) fn new_with_config(git_config: &crate::git::Config) -> Result<Self, ConfigError> {
Ok(Self {
auto_select_next: get_bool(git_config, "interactive-rebase-tool.autoSelectNext", false)?,
diff_ignore_whitespace: get_diff_ignore_whitespace(
Expand Down Expand Up @@ -94,6 +94,25 @@ impl Config {
}
}

impl Default for Config {
fn default() -> Self {
Self {
auto_select_next: false,
diff_ignore_whitespace: DiffIgnoreWhitespaceSetting::None,
diff_ignore_blank_lines: false,
diff_show_whitespace: DiffShowWhitespaceSetting::Both,
diff_space_symbol: DEFAULT_SPACE_SYMBOL.to_owned(),
diff_tab_symbol: DEFAULT_TAB_SYMBOL.to_owned(),
diff_tab_width: 4,
undo_limit: 5000,
post_modified_line_exec_command: None,
git: GitConfig::default(),
key_bindings: KeyBindings::default(),
theme: Theme::default(),
}
}
}

impl TryFrom<&ConfigLoader> for Config {
type Error = ConfigError;

Expand All @@ -106,15 +125,15 @@ impl TryFrom<&ConfigLoader> for Config {
let config = config_loader
.load_config()
.map_err(|e| ConfigError::new_read_error("", ConfigErrorCause::GitError(e)))?;
Self::new_with_config(Some(&config))
Self::new_with_config(&config)
}
}

impl TryFrom<&crate::git::Config> for Config {
type Error = ConfigError;

fn try_from(config: &crate::git::Config) -> Result<Self, Self::Error> {
Self::new_with_config(Some(config))
Self::new_with_config(config)
}
}

Expand Down Expand Up @@ -331,7 +350,7 @@ mod tests {
vec!["[interactive-rebase-tool]", value.as_str()]
};
with_git_config(&lines, |config| {
let config = Config::new_with_config(Some(&config)).unwrap();
let config = Config::new_with_config(&config).unwrap();
assert_eq!(access(config), expected);
});
}
Expand All @@ -353,7 +372,7 @@ mod tests {
],
|git_config| {
assert_err_eq!(
Config::new_with_config(Some(&git_config)),
Config::new_with_config(&git_config),
ConfigError::new(
format!("interactive-rebase-tool.{config_name}").as_str(),
config_value,
Expand All @@ -378,7 +397,7 @@ mod tests {
],
|git_config| {
assert_err_eq!(
Config::new_with_config(Some(&git_config)),
Config::new_with_config(&git_config),
ConfigError::new_read_error(
format!("interactive-rebase-tool.{config_name}").as_str(),
ConfigErrorCause::InvalidUtf
Expand Down
11 changes: 11 additions & 0 deletions src/config/diff_ignore_whitespace_setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ pub(crate) enum DiffIgnoreWhitespaceSetting {
/// ) flag.
Change,
}

impl DiffIgnoreWhitespaceSetting {
pub(crate) fn parse(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"true" | "on" | "all" => Some(DiffIgnoreWhitespaceSetting::All),
"change" => Some(DiffIgnoreWhitespaceSetting::Change),
"false" | "off" | "none" => Some(DiffIgnoreWhitespaceSetting::None),
_ => None,
}
}
}
12 changes: 12 additions & 0 deletions src/config/diff_show_whitespace_setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ pub(crate) enum DiffShowWhitespaceSetting {
/// Show both leading and trailing whitespace characters.
Both,
}

impl DiffShowWhitespaceSetting {
pub(crate) fn parse(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"true" | "on" | "both" => Some(DiffShowWhitespaceSetting::Both),
"trailing" => Some(DiffShowWhitespaceSetting::Trailing),
"leading" => Some(DiffShowWhitespaceSetting::Leading),
"false" | "off" | "none" => Some(DiffShowWhitespaceSetting::None),
_ => None,
}
}
}
Loading
Loading