From 1ed8e65de73fe4b2f2e1aa9b34767c0be93e31fa Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 23 May 2023 18:03:10 +0200 Subject: [PATCH] Corrects the save/replace error when dest was taken --- src/actions.rs | 14 +++++------ src/wrapper_functions.rs | 50 +++++++++++----------------------------- 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index 5531f13..f8b3165 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1,8 +1,6 @@ //! The action module, including all the logic to jump to a specific action //! depending on the CLI arguments -use std::error::Error; - use crate::{errors, wrapper_functions, Cli}; /// List of all possible actions, some are mutually exclusive @@ -38,12 +36,12 @@ pub fn execute_parsed_cli(cli: Cli) -> Result<(), errors::SnippetError> { Actions::Delete => wrapper_functions::delete_snippet(&cli), Actions::Rename => wrapper_functions::rename_snippet(&cli), } { - println!("An error occured during runtime:\n\t{}", e); - if let Some(e) = e.source() { - println!("This error was caused by: \n\t{}", e); - } - // return Err(e); - return Ok(()); // Very unconvinced by this + // println!("An error occured during runtime:\n\t{}", e); + // if let Some(e) = e.source() { + // println!("This error was caused by: \n\t{}", e); + // } + _ = ""; // No op to calm clippy while I decide wether or not delete this + return Err(e); }; } Ok(()) diff --git a/src/wrapper_functions.rs b/src/wrapper_functions.rs index ac3de9a..f9375c1 100644 --- a/src/wrapper_functions.rs +++ b/src/wrapper_functions.rs @@ -125,7 +125,7 @@ pub fn edit_snippet(cli: &Cli) -> Result<(), SnippetError> { pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> { // Try loading the snippet let status = check_dir_and_open(&cli.snippet); - let (buf, mut p) = match status? { + let (buf, mut origin_p) = match status? { FileStatus::Opened((p, f)) => (read_to_string(f)?, p), FileStatus::NotFound(p) => { if cli.create { @@ -145,72 +145,50 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> { Ok(snp) => { if cli.snippet == snp.name { // No renaming has occured, the file can safely be overriten - snp.write_over(&p)?; + snp.write_over(&origin_p)?; println!( "The snippet {:?} was succesfully saved", - p.file_name().unwrap() + origin_p.file_name().unwrap() ); Ok(()) } else { - let (f, p) = match check_dir_and_open(&snp.name)? { - FileStatus::Opened((p, f)) => { + let (dest_f, dest_p) = match check_dir_and_open(&snp.name)? { + FileStatus::Opened((dest_p, _dest_f)) => { if cli.force { // Erasing will take place - (File::create(&p)?, p) + (File::create(&dest_p)?, dest_p) } else { // No erasing/overriting println!( - "Impossible to save the snippet as {:}, as it already exists. The snippet will be saved under it's old name.", - snp.name + "The snippet could not be saved as {:}.yaml, as this Snippet already exists. The Snippet will be saved under it's original name name: {}", + snp.name,origin_p.file_name().unwrap().to_str().unwrap() ); - (f, p) + (File::create(&origin_p)?, origin_p) } } FileStatus::NotFound(p) => (File::create(&p)?, p), }; - snp.save_in(&f)?; - println!("The snippet was successfuly saved in {:?}", p); + snp.save_in(&dest_f)?; + println!("The snippet was successfuly saved in {:?}", dest_p); Ok(()) } } Err(_) => { // The snippet is incorrect, however it should still be saved, just under another name ? - p.set_extension("bad.yaml"); - let mut failover = File::create(&p)?; + origin_p.set_extension("bad.yaml"); + let mut failover = File::create(&origin_p)?; write!(failover, "{}", &buf)?; println!( "The snippet was saved under {:?} in your config", - p.file_name().unwrap() + origin_p.file_name().unwrap() ); Err(SnippetError::ReparsingError) } } } -// This code is used to check if the name changed but should be moved to a separate function -// let old_name = snp.name.clone(); -// // During tests, the editor cannot be tested -// #[cfg(not(feature = "test"))] -// snp.edit()?; - -// if snp.name == old_name { -// snp.write_over(&p) -// } else { -// match check_dir_and_open(&snp.name)? { -// FileStatus::Opened((p, _f)) => { -// if cli.force { -// std::fs::remove_file(&p)?; -// snp.write_over(&p) -// } else { -// Err(SnippetError::NotFoundNoRetryError(p)) -// } -// } -// FileStatus::NotFound(p) => snp.write_over(&p), -// } -// } - /// Print the snippet to stdout pub fn print_snippet(cli: &Cli) -> Result<(), SnippetError> { // Try to load the snippet