From a850c488f9dead0bf7d35f46efe4915c907b9d86 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Wed, 21 Dec 2022 10:44:34 +0900 Subject: [PATCH] cli: use fs::read_to_string() to read edited description file Also removes panic on io::Error. It's unlikely, but could occur if the edited description contained non-UTF-8 character. --- src/commands.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index dc99bf4b0..c15d21b37 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -14,7 +14,6 @@ use std::collections::{BTreeSet, HashSet}; use std::fmt::Debug; -use std::fs::OpenOptions; use std::io::{Read, Seek, SeekFrom, Write}; use std::ops::Deref; use std::path::{Path, PathBuf}; @@ -1829,18 +1828,12 @@ fn edit_description( ))); } - let mut description_file = OpenOptions::new() - .read(true) - .open(&description_file_path) - .unwrap_or_else(|_| { - panic!( - "failed to open {} for read", - description_file_path.display() - ) - }); - let mut buf = vec![]; - description_file.read_to_end(&mut buf).unwrap(); - let description = String::from_utf8(buf).unwrap(); + let description = fs::read_to_string(&description_file_path).map_err(|e| { + user_error(format!( + r#"Failed to read description file "{path}": {e}"#, + path = description_file_path.display() + )) + })?; // Delete the file only if everything went well. // TODO: Tell the user the name of the file we left behind. std::fs::remove_file(description_file_path).ok();