Skip to content

Commit fd5b487

Browse files
committed
Display config file formatting errors
1 parent cfca1e6 commit fd5b487

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

src/context/args.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,9 @@ fn load_rc_config_args() -> Option<ArgMatches> {
121121
/// Loads an [`ArgMatches`] from `.erdtree.toml`.
122122
#[inline]
123123
fn load_toml_config_args(named_table: Option<&str>) -> Result<Option<ArgMatches>, Error> {
124-
config::toml::load().map_or(Ok(None), |toml_config| {
125-
let parsed_args = config::toml::parse(toml_config, named_table)?;
126-
let config_args = Context::command().get_matches_from(parsed_args);
124+
let toml_config = config::toml::load()?;
125+
let parsed_args = config::toml::parse(toml_config, named_table)?;
126+
let config_args = Context::command().get_matches_from(parsed_args);
127127

128-
Ok(Some(config_args))
129-
})
128+
Ok(Some(config_args))
130129
}

src/context/config/toml/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub enum Error {
55
#[error("Failed to load .erdtree.toml")]
66
LoadConfig,
77

8-
#[error("The configuration file is improperly formatted")]
8+
#[error("The configuration file is improperly formatted: {0}")]
99
InvalidFormat(#[from] ConfigError),
1010

1111
#[error("Named table '{0}' was not found in '.erdtree.toml'")]

src/context/config/toml/mod.rs

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,29 @@ pub fn parse(config: Config, named_table: Option<&str>) -> Result<Vec<OsString>,
6868
/// Reads in `.erdtree.toml` file.
6969
pub fn load() -> Result<Config, Error> {
7070
#[cfg(windows)]
71-
return windows::load_toml().ok_or(Error::LoadConfig);
71+
return windows::load_toml();
7272

7373
#[cfg(unix)]
74-
unix::load_toml().ok_or(Error::LoadConfig)
74+
unix::load_toml()
7575
}
7676

77-
/// Attempts to load in `.erdtree.toml` from `$ERDTREE_TOML_PATH`. Will return `None` for whatever
78-
/// reason.
79-
fn toml_from_env() -> Option<Config> {
77+
/// Attempts to load in `.erdtree.toml` from `$ERDTREE_TOML_PATH`.
78+
fn toml_from_env() -> Result<Config, Error> {
8079
let config = env::var_os(super::ERDTREE_TOML_PATH)
8180
.map(OsString::into_string)
82-
.and_then(Result::ok)?;
83-
84-
let file = config.strip_suffix(".toml").map(File::with_name)?;
85-
86-
Config::builder().add_source(file).build().ok()
81+
.transpose()
82+
.map_err(|_| Error::LoadConfig)?
83+
.ok_or(Error::LoadConfig)?;
84+
85+
let file = config
86+
.strip_suffix(".toml")
87+
.map(File::with_name)
88+
.ok_or(Error::LoadConfig)?;
89+
90+
Config::builder()
91+
.add_source(file)
92+
.build()
93+
.map_err(Error::from)
8794
}
8895

8996
/// Simple utility used to extract the underlying value from the [`Value`] enum that we get when
@@ -126,7 +133,8 @@ fn parse_argument(keyword: &str, arg: &Value) -> Result<ArgInstructions, Error>
126133
#[cfg(unix)]
127134
mod unix {
128135
use super::super::{CONFIG_DIR, ERDTREE_CONFIG_TOML, ERDTREE_DIR, HOME, XDG_CONFIG_HOME};
129-
use config::{Config, File};
136+
use super::Error;
137+
use config::{Config, ConfigError, File};
130138
use std::{env, path::PathBuf};
131139

132140
/// Looks for `.erdtree.toml` in the following locations in order:
@@ -136,18 +144,20 @@ mod unix {
136144
/// - `$XDG_CONFIG_HOME/.erdtree.toml`
137145
/// - `$HOME/.config/erdtree/.erdtree.toml`
138146
/// - `$HOME/.erdtree.toml`
139-
pub(super) fn load_toml() -> Option<Config> {
147+
pub(super) fn load_toml() -> Result<Config, Error> {
140148
super::toml_from_env()
141-
.or_else(toml_from_xdg_path)
142-
.or_else(toml_from_home)
149+
.or_else(|_| toml_from_xdg_path())
150+
.or_else(|_| toml_from_home())
143151
}
144152

145153
/// Looks for `.erdtree.toml` in the following locations in order:
146154
///
147155
/// - `$XDG_CONFIG_HOME/erdtree/.erdtree.toml`
148156
/// - `$XDG_CONFIG_HOME/.erdtree.toml`
149-
fn toml_from_xdg_path() -> Option<Config> {
150-
let config = env::var_os(XDG_CONFIG_HOME).map(PathBuf::from)?;
157+
fn toml_from_xdg_path() -> Result<Config, Error> {
158+
let config = env::var_os(XDG_CONFIG_HOME)
159+
.map(PathBuf::from)
160+
.ok_or(Error::LoadConfig)?;
151161

152162
let mut file = config
153163
.join(ERDTREE_DIR)
@@ -164,15 +174,20 @@ mod unix {
164174
.map(File::with_name);
165175
}
166176

167-
Config::builder().add_source(file?).build().ok()
177+
file.map_or_else(
178+
|| Err(Error::LoadConfig),
179+
|f| Config::builder().add_source(f).build().map_err(Error::from),
180+
)
168181
}
169182

170183
/// Looks for `.erdtree.toml` in the following locations in order:
171184
///
172185
/// - `$HOME/.config/erdtree/.erdtree.toml`
173186
/// - `$HOME/.erdtree.toml`
174-
fn toml_from_home() -> Option<Config> {
175-
let home = env::var_os(HOME).map(PathBuf::from)?;
187+
fn toml_from_home() -> Result<Config, Error> {
188+
let home = env::var_os(HOME)
189+
.map(PathBuf::from)
190+
.ok_or(Error::LoadConfig)?;
176191

177192
let mut file = home
178193
.join(CONFIG_DIR)
@@ -190,25 +205,35 @@ mod unix {
190205
.map(File::with_name);
191206
}
192207

193-
Config::builder().add_source(file?).build().ok()
208+
file.map_or_else(
209+
|| Err(Error::LoadConfig),
210+
|f| Config::builder()
211+
.add_source(f)
212+
.build()
213+
.map_err(|err| match err {
214+
ConfigError::FileParse { .. } | ConfigError::Type { .. } => Error::from(err),
215+
_ => Error::LoadConfig,
216+
}),
217+
)
194218
}
195219
}
196220

197221
/// Concerned with how to load `.erdtree.toml` on Windows.
198222
#[cfg(windows)]
199223
mod windows {
200224
use super::super::{ERDTREE_CONFIG_TOML, ERDTREE_DIR};
225+
use super::Error;
201226
use config::{Config, File};
202227

203228
/// Try to read in config from the following location:
204229
/// - `%APPDATA%\erdtree\.erdtree.toml`
205-
pub(super) fn load_toml() -> Option<Config> {
230+
pub(super) fn load_toml() -> Result<Config, Error> {
206231
super::toml_from_env().or_else(toml_from_appdata)
207232
}
208233

209234
/// Try to read in config from the following location:
210235
/// - `%APPDATA%\erdtree\.erdtree.toml`
211-
fn toml_from_appdata() -> Option<Config> {
236+
fn toml_from_appdata() -> Result<Config, Error> {
212237
let app_data = dirs::config_dir()?;
213238

214239
let file = app_data
@@ -218,6 +243,9 @@ mod windows {
218243
.and_then(|s| s.strip_suffix(".toml"))
219244
.map(File::with_name)?;
220245

221-
Config::builder().add_source(file).build().ok()
246+
Config::builder()
247+
.add_source(file)
248+
.build()
249+
.map_err(Error::from)
222250
}
223251
}

0 commit comments

Comments
 (0)