Updated settings::utils::write_file’s signature for more flexibility

`write_file` now accepts as its second argument any type that
implements the `ToString` trait so that not only Strings will be
accepted but any type that can be turned into one.
This commit is contained in:
Lucien Cartier-Tilet 2020-03-27 19:09:34 +01:00
parent 488ceba1bb
commit 8642067eb3
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
2 changed files with 6 additions and 3 deletions

View File

@ -188,7 +188,7 @@ fn write_settings() {
version: "1" version: "1"
categories: [] categories: []
rules: []"#; rules: []"#;
utils::write_file(&path, serde_yaml::to_string(&s).unwrap()).unwrap(); utils::write_file(&path, &serde_yaml::to_string(&s).unwrap()).unwrap();
assert_eq!(settings, utils::read_file(&path).unwrap()); assert_eq!(settings, utils::read_file(&path).unwrap());
} }

View File

@ -44,7 +44,6 @@ pub fn read_file(path: &Path) -> Result<String> {
} }
} }
pub fn write_file(path: &Path, content: String) -> Result<()> {
/// Write a `String` into a file /// Write a `String` into a file
/// ///
/// # Example /// # Example
@ -54,6 +53,10 @@ pub fn write_file(path: &Path, content: String) -> Result<()> {
/// let path = std::path::Path::new("./path/to/my/file.txt"); /// let path = std::path::Path::new("./path/to/my/file.txt");
/// lang_evolve_core::settings::utils::write_file(&path, &content).unwrap(); /// lang_evolve_core::settings::utils::write_file(&path, &content).unwrap();
/// ``` /// ```
pub fn write_file<S>(path: &Path, content: &S) -> Result<()>
where
S: std::string::ToString,
{
use std::io::prelude::*; use std::io::prelude::*;
let mut file = match File::create(&path) { let mut file = match File::create(&path) {
Err(e) => { Err(e) => {
@ -62,7 +65,7 @@ pub fn write_file(path: &Path, content: String) -> Result<()> {
} }
Ok(file) => file, Ok(file) => file,
}; };
match file.write_all(content.as_bytes()) { match file.write_all(content.to_string().as_bytes()) {
Err(e) => { Err(e) => {
warn!( warn!(
"Could not write settings to file {}: {}", "Could not write settings to file {}: {}",