Added a check to return an empty string when attempting to read a file that doesn't exist

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-26 16:35:02 +02:00
parent 901ef7818d
commit f32becf5f5
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283

View File

@ -1,4 +1,4 @@
use std::{fs::{File, create_dir_all}, io::{Read, Write}, path::PathBuf}; use std::{fs::{File, create_dir_all}, io::{Read, Write}, path::{Path, PathBuf}};
use hyper_tls::HttpsConnector; use hyper_tls::HttpsConnector;
use hyper::{Body, Client, body, client::HttpConnector}; use hyper::{Body, Client, body, client::HttpConnector};
@ -32,6 +32,7 @@ fn write_to_file(d: &str, p: &str) {
} }
fn read_from_file(p: &str) -> String { fn read_from_file(p: &str) -> String {
if !Path::new(p).exists() { return "".to_string(); }
let mut d = String::new(); let mut d = String::new();
let mut ifile = File::open(p).expect("Cannot open file."); let mut ifile = File::open(p).expect("Cannot open file.");
ifile.read_to_string(&mut d).expect("Cannot read file."); ifile.read_to_string(&mut d).expect("Cannot read file.");