Skip to content

Commit

Permalink
refactor: use AsRef<Path> instead of <Path>
Browse files Browse the repository at this point in the history
It enables the caller to pass `Path`, `PathBuf`, `&str` or `String.`
  • Loading branch information
azzamsa committed Jan 3, 2023
1 parent 8ded32d commit 21c878b
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::fs;
use std::path::Path;
use std::{
ffi::OsStr,
fs,
path::{Path, PathBuf},
};

use serde::Deserialize;

Expand All @@ -16,9 +19,12 @@ pub struct Config {
pub servers: Vec<Server>,
}

pub fn read(path: &Path) -> Result<Config, Error> {
let file_content = fs::read_to_string(path).map_err(|_| Error::ConfigNotFound {
path: path.to_path_buf(),
pub fn read<P>(filename: P) -> Result<Config, Error>
where
P: AsRef<Path> + AsRef<OsStr>,
{
let file_content = fs::read_to_string(&filename).map_err(|_| Error::ConfigNotFound {
path: PathBuf::from(&filename),
})?;
deserialize(&file_content)
}
Expand Down

0 comments on commit 21c878b

Please sign in to comment.