Use expect() instead of try

This commit is contained in:
Yash Karandikar 2022-07-03 00:03:57 +05:30
parent 4a3b1c169d
commit 2aac4731dd
2 changed files with 11 additions and 7 deletions

View file

@ -13,7 +13,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use crate::Result;
use serde::Deserialize;
use std::path::PathBuf;
@ -28,12 +27,17 @@ pub struct XcrabMsgConfig {
pub socket_path: PathBuf,
}
pub fn load_file() -> Result<XcrabConfig> {
let home_dir = std::env::var("HOME")?;
pub fn load_file() -> XcrabConfig {
let home_dir = std::env::var("HOME").expect("Error: $HOME variable was not set");
let contents = std::fs::read_to_string(format!("{}/.config/xcrab/config.toml", home_dir))?;
let contents = std::fs::read_to_string(format!("{}/.config/xcrab/config.toml", home_dir))
.expect(&format!(
"Error: file {}/.config/xcrab/config.toml was not found",
home_dir
));
let config: XcrabConfig = toml::from_str(&contents)?;
let config: XcrabConfig = toml::from_str(&contents)
.expect("Error: config file was not parseable. Is it properly formatted?");
Ok(config)
config
}

View file

@ -24,7 +24,7 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
async fn main() -> Result<()> {
let msg = std::env::args().skip(1).collect::<Vec<String>>().join(" ");
let conf = config::load_file()?;
let conf = config::load_file();
let path = conf.msg.expect("xcrab-msg not configured!").socket_path;