summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorConrad Ludgate <conradludgate@gmail.com>2021-11-13 20:58:40 +0000
committerConrad Ludgate <conradludgate@gmail.com>2021-11-21 11:36:17 +0000
commit027d1583c47caf65daad04bc5f5a8ddae4ca2642 (patch)
tree24d8761f55ca0958ac937ca887c77c3268367e70
parentfd7ef7fbce135da904fcef678d5ca3089d794a8d (diff)
chore: some file read simplifications
-rw-r--r--src/file/source/file.rs6
-rw-r--r--tests/async_builder.rs25
2 files changed, 9 insertions, 22 deletions
diff --git a/src/file/source/file.rs b/src/file/source/file.rs
index 471b654..5a50d93 100644
--- a/src/file/source/file.rs
+++ b/src/file/source/file.rs
@@ -1,7 +1,7 @@
use std::env;
use std::error::Error;
use std::fs;
-use std::io::{self, Read};
+use std::io;
use std::iter::Iterator;
use std::path::{Path, PathBuf};
@@ -114,9 +114,7 @@ where
.unwrap_or_else(|| filename.clone());
// Read contents from file
- let mut file = fs::File::open(filename)?;
- let mut text = String::new();
- file.read_to_string(&mut text)?;
+ let text = fs::read_to_string(filename)?;
Ok(FileSourceResult {
uri: Some(uri.to_string_lossy().into_owned()),
diff --git a/tests/async_builder.rs b/tests/async_builder.rs
index b0aa0f4..5fcabeb 100644
--- a/tests/async_builder.rs
+++ b/tests/async_builder.rs
@@ -1,7 +1,7 @@
use async_trait::async_trait;
use config::*;
use std::{env, fs, path, str::FromStr};
-use tokio::{fs::File, io::AsyncReadExt};
+use tokio::fs::read_to_string;
#[derive(Debug)]
struct AsyncFile {
@@ -22,23 +22,12 @@ impl AsyncSource for AsyncFile {
let mut path = env::current_dir().unwrap();
let local = path::PathBuf::from_str(&self.path).unwrap();
- path.extend(local.into_iter());
-
- let path = match fs::canonicalize(path) {
- Ok(path) => path,
- Err(e) => return Err(ConfigError::Foreign(Box::new(e))),
- };
-
- let text = match File::open(path).await {
- Ok(mut file) => {
- let mut buffer = String::default();
- match file.read_to_string(&mut buffer).await {
- Ok(_read) => buffer,
- Err(e) => return Err(ConfigError::Foreign(Box::new(e))),
- }
- }
- Err(e) => return Err(ConfigError::Foreign(Box::new(e))),
- };
+ path.extend(local.iter());
+ let path = fs::canonicalize(path).map_err(|e| ConfigError::Foreign(Box::new(e)))?;
+
+ let text = read_to_string(path)
+ .await
+ .map_err(|e| ConfigError::Foreign(Box::new(e)))?;
self.format
.parse(Some(&self.path), &text)