Skip to content

Commit af74ac8

Browse files
committed
refactor: Simplify config reading
1 parent 173a685 commit af74ac8

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

rust/stackablectl/src/cli/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,10 @@ impl Cli {
188188
}
189189

190190
let xdg_directories = Cli::xdg_directories()?;
191+
// TODO (@Techassi): Move this file name to a constant
191192
let user_config_path = xdg_directories.config_dir().join("config.toml");
192193

193-
let _user_config = UserConfig::from_file(user_config_path)
194-
.unwrap()
195-
.unwrap_or_default();
194+
let _user_config = UserConfig::from_file_or_default(user_config_path).unwrap();
196195
dbg!(_user_config);
197196

198197
let cache = self

rust/stackablectl/src/config.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,16 @@ pub enum Error {
2929
}
3030

3131
impl UserConfig {
32-
pub fn from_file<P>(path: P) -> Result<Option<Self>, Error>
32+
/// Reads [`UserConfig`] from `path` or if not found, falls back to the default config.
33+
pub fn from_file_or_default<P>(path: P) -> Result<Self, Error>
3334
where
3435
P: AsRef<Path>,
3536
{
3637
let path = path.as_ref();
3738

3839
match std::fs::read_to_string(path) {
39-
Ok(contents) => {
40-
let config = toml::from_str(&contents).context(DeserializeSnafu { path })?;
41-
Ok(Some(config))
42-
}
43-
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None),
40+
Ok(contents) => toml::from_str(&contents).context(DeserializeSnafu { path }),
41+
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(Self::default()),
4442
Err(err) => Err(Error::Read {
4543
path: path.to_path_buf(),
4644
source: err,

0 commit comments

Comments
 (0)