From e2693242e5e8515471daaa897c1bf3b9e079810e Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 1 Sep 2023 12:21:20 +0200 Subject: [PATCH] Fix error if no config file exists --- src/config.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index 8fee796..04b472a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,8 @@ //! Configuration from a file. -use std::{collections::HashMap, fs, net::SocketAddr, path::PathBuf, time::Duration}; +use std::{ + collections::HashMap, fs, io::ErrorKind, net::SocketAddr, path::PathBuf, time::Duration, +}; use directories::ProjectDirs; use log::{info, trace}; @@ -259,11 +261,19 @@ impl Config { let path = Self::path(args); info!("Loading config from {}", path.display()); - let raw = fs::read_to_string(path)?; - let raw = toml::from_str::(&raw)?; + let raw = match fs::read_to_string(path) { + Ok(str) => toml::from_str::(&str)?, + Err(e) if e.kind() == ErrorKind::NotFound => { + info!("No config file found, using default config"); + RawConfig::default() + } + Err(e) => Err(e)?, + }; trace!("Raw config: {raw:#?}"); + let config = Self::from_raw_config(raw, args); trace!("Config: {config:#?}"); + Ok(config) } }