Skip to content

Commit 3907137

Browse files
committed
decouple construction of Config from ConfigLoader
1 parent 3c4fa04 commit 3907137

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

src/application.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub(crate) use crate::application::app_data::AppData;
99
use crate::{
1010
Args,
1111
Exit,
12-
config::{Config, ConfigLoader, DiffIgnoreWhitespaceSetting},
12+
config::{Config, ConfigError, ConfigErrorCause, ConfigLoader, DiffIgnoreWhitespaceSetting},
1313
diff::{self, CommitDiffLoader, CommitDiffLoaderOptions},
1414
display::Display,
1515
git::open_repository_from_env,
@@ -42,7 +42,8 @@ where ModuleProvider: module::ModuleProvider + Send + 'static
4242
let filepath = Self::filepath_from_args(args)?;
4343
let repository = Self::open_repository()?;
4444
let config_loader = ConfigLoader::from(repository);
45-
let config = Self::load_config(&config_loader)?;
45+
let config = Self::load_config(&config_loader)
46+
.map_err(|err| Exit::new(ExitStatus::ConfigError, format!("{err:#}").as_str()))?;
4647
let todo_file = Arc::new(Mutex::new(Self::load_todo_file(filepath.as_str(), &config)?));
4748

4849
let display = Display::new(tui, &config.theme);
@@ -162,8 +163,11 @@ where ModuleProvider: module::ModuleProvider + Send + 'static
162163
})
163164
}
164165

165-
fn load_config(config_loader: &ConfigLoader) -> Result<Config, Exit> {
166-
Config::try_from(config_loader).map_err(|err| Exit::new(ExitStatus::ConfigError, format!("{err:#}").as_str()))
166+
fn load_config(config_loader: &ConfigLoader) -> Result<Config, ConfigError> {
167+
let config = config_loader
168+
.load_config()
169+
.map_err(|e| ConfigError::new_read_error("", ConfigErrorCause::GitError(e)))?;
170+
Config::new_with_config(Some(&config))
167171
}
168172

169173
fn todo_file_options(config: &Config) -> TodoFileOptions {

src/config.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ pub(crate) use self::{
2323
config_loader::ConfigLoader,
2424
diff_ignore_whitespace_setting::DiffIgnoreWhitespaceSetting,
2525
diff_show_whitespace_setting::DiffShowWhitespaceSetting,
26+
errors::{ConfigError, ConfigErrorCause, InvalidColorError},
2627
git_config::GitConfig,
2728
key_bindings::KeyBindings,
2829
theme::Theme,
2930
};
30-
use crate::config::{
31-
errors::{ConfigError, ConfigErrorCause, InvalidColorError},
32-
utils::get_optional_string,
33-
};
31+
use crate::config::utils::get_optional_string;
3432

3533
const DEFAULT_SPACE_SYMBOL: &str = "\u{b7}"; // ·
3634
const DEFAULT_TAB_SYMBOL: &str = "\u{2192}"; // →
@@ -94,22 +92,6 @@ impl Config {
9492
}
9593
}
9694

97-
impl TryFrom<&ConfigLoader> for Config {
98-
type Error = ConfigError;
99-
100-
/// Creates a new Config instance loading the Git Config.
101-
///
102-
/// # Errors
103-
///
104-
/// Will return an `Err` if there is a problem loading the configuration.
105-
fn try_from(config_loader: &ConfigLoader) -> Result<Self, Self::Error> {
106-
let config = config_loader
107-
.load_config()
108-
.map_err(|e| ConfigError::new_read_error("", ConfigErrorCause::GitError(e)))?;
109-
Self::new_with_config(Some(&config))
110-
}
111-
}
112-
11395
impl TryFrom<&crate::git::Config> for Config {
11496
type Error = ConfigError;
11597

@@ -132,7 +114,8 @@ mod tests {
132114
fn try_from_config_loader() {
133115
with_temp_bare_repository(|repository| {
134116
let loader = ConfigLoader::from(repository);
135-
assert_ok!(Config::try_from(&loader));
117+
let config = assert_ok!(loader.load_config());
118+
assert_ok!(Config::try_from(&config));
136119
});
137120
}
138121

0 commit comments

Comments
 (0)