From 8642067eb389aeb4d134a38b3344f1786687e8b6 Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Fri, 27 Mar 2020 19:09:34 +0100 Subject: [PATCH] =?UTF-8?q?Updated=20settings::utils::write=5Ffile?= =?UTF-8?q?=E2=80=99s=20signature=20for=20more=20flexibility?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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. --- src/settings/mod.rs | 2 +- src/settings/utils.rs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/settings/mod.rs b/src/settings/mod.rs index b94a164..95a405b 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -188,7 +188,7 @@ fn write_settings() { version: "1" categories: [] 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()); } diff --git a/src/settings/utils.rs b/src/settings/utils.rs index e0aeda0..557c449 100644 --- a/src/settings/utils.rs +++ b/src/settings/utils.rs @@ -44,7 +44,6 @@ pub fn read_file(path: &Path) -> Result { } } -pub fn write_file(path: &Path, content: String) -> Result<()> { /// Write a `String` into a file /// /// # 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"); /// lang_evolve_core::settings::utils::write_file(&path, &content).unwrap(); /// ``` +pub fn write_file(path: &Path, content: &S) -> Result<()> +where + S: std::string::ToString, +{ use std::io::prelude::*; let mut file = match File::create(&path) { Err(e) => { @@ -62,7 +65,7 @@ pub fn write_file(path: &Path, content: String) -> Result<()> { } Ok(file) => file, }; - match file.write_all(content.as_bytes()) { + match file.write_all(content.to_string().as_bytes()) { Err(e) => { warn!( "Could not write settings to file {}: {}",