1
0
mirror of https://github.com/jht5945/finding.git synced 2025-12-28 13:40:04 +08:00
This commit is contained in:
2020-04-27 23:33:21 +08:00
parent c03ab9ff4c
commit be4a25f0c7
3 changed files with 32 additions and 52 deletions

View File

@@ -1,20 +1,15 @@
use std:: {
fs::File,
path::Path,
io::prelude::*,
};
use rust_util::{
XResult,
new_box_error,
};
use rust_util::{ XResult, new_box_error, };
pub fn read_file_content(file: &Path, large_file_len: u64) -> XResult<String> {
if ! file.exists() {
if !file.exists() {
return Err(new_box_error(&format!("File not exists: {:?}", file)));
}
if ! file.is_file() {
if !file.is_file() {
return Err(new_box_error(&format!("File is not file: {:?}", file)));
}
let file_len = file.metadata()?.len();
@@ -22,7 +17,7 @@ pub fn read_file_content(file: &Path, large_file_len: u64) -> XResult<String> {
return Err(new_box_error(&format!("File too large: {:?}, len: {}", file, file_len)));
}
let mut f = File::open(file)?;
let mut content = String::new();
let mut content = String::with_capacity(file_len as usize);
f.read_to_string(&mut content)?;
Ok(content)