~garritfra/taurus

f6155006c5e53b5ce5e3779844495b7d5ed86b3d — Josh Leeb-du Toit 2 years ago 6fe4a2d master
Simplify Config::load

Simplify Config::load by using the fs::read_to_string function provided
in std. Additionally, we update the type of the config_path argument to
be generic over AsRef<Path> which is generally preferred over &str for
file path arguments.
1 files changed, 4 insertions(+), 9 deletions(-)

M src/config.rs
M src/config.rs => src/config.rs +4 -9
@@ 1,6 1,5 @@
use serde::Deserialize;
use std::fs;
use std::io::prelude::*;
use std::{fs, path::Path};

#[derive(Deserialize)]
pub struct Config {


@@ 13,12 12,8 @@ pub struct Config {
}

impl Config {
    pub fn load(config_path: &str) -> anyhow::Result<Self> {
        let mut file = fs::File::open(config_path)?;
        let mut contents = String::new();

        file.read_to_string(&mut contents)?;

        Ok(toml::from_str(&contents)?)
    pub fn load<P: AsRef<Path>>(config_path: P) -> anyhow::Result<Self> {
        let buf = fs::read_to_string(config_path)?;
        toml::from_str(&buf).map_err(|e| e.into())
    }
}