From 21c878bdec13e604812934b65f9ed42a6543aae3 Mon Sep 17 00:00:00 2001 From: "Azzam S.A" Date: Wed, 4 Jan 2023 04:53:55 +0700 Subject: [PATCH] refactor: use `AsRef` instead of `` It enables the caller to pass `Path`, `PathBuf`, `&str` or `String.` --- src/config.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index 6fba1a2..b525b10 100755 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,8 @@ -use std::fs; -use std::path::Path; +use std::{ + ffi::OsStr, + fs, + path::{Path, PathBuf}, +}; use serde::Deserialize; @@ -16,9 +19,12 @@ pub struct Config { pub servers: Vec, } -pub fn read(path: &Path) -> Result { - let file_content = fs::read_to_string(path).map_err(|_| Error::ConfigNotFound { - path: path.to_path_buf(), +pub fn read

(filename: P) -> Result +where + P: AsRef + AsRef, +{ + let file_content = fs::read_to_string(&filename).map_err(|_| Error::ConfigNotFound { + path: PathBuf::from(&filename), })?; deserialize(&file_content) }