From f32becf5f5b9d512f3abeba0643e84e172384720 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Tue, 26 Oct 2021 16:35:02 +0200 Subject: [PATCH] Added a check to return an empty string when attempting to read a file that doesn't exist Signed-off-by: Louis Vallat --- src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 3b78d43..e0c0fda 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::{Body, Client, body, client::HttpConnector}; @@ -32,6 +32,7 @@ fn write_to_file(d: &str, p: &str) { } fn read_from_file(p: &str) -> String { + if !Path::new(p).exists() { return "".to_string(); } let mut d = String::new(); let mut ifile = File::open(p).expect("Cannot open file."); ifile.read_to_string(&mut d).expect("Cannot read file.");