From 1678e4ad6aed527994ea5a0c37d780822ee70602 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 23 May 2023 05:20:03 +0200 Subject: [PATCH] Adds full support for rename and corrects a few bugs --- .gitignore | 1 + Cargo.toml | 2 +- Makefile.toml | 22 +++++++++++++- TODO.md | 2 +- src/errors.rs | 2 +- src/wrapper_functions.rs | 65 +++++++++++++++++++++++++++++++++++----- 6 files changed, 83 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 5e4e5bd..7d8b60e 100644 --- a/.gitignore +++ b/.gitignore @@ -21,5 +21,6 @@ Cargo.lock /target /coverage /llvm +/releases tarpaulin-report.html !coverage/html/badges/flat.svg \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index f46fc2c..5b5d1e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ test = [] opt-level = 0 [profile.release] -opt-level = "z" +opt-level = "s" [dependencies] clap = { version = "4.2.7", features = ["derive"] } diff --git a/Makefile.toml b/Makefile.toml index ae00fbc..273806f 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -20,7 +20,15 @@ script = [ script_runner = "@shell" ignore_errors = true script = [ - "CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-pc-windows-gnu" + "CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-pc-windows-gnu --release" +] + +[tasks.linux_build] +script_runner = "@shell" +ignore_errors = true +script = [ + "cargo build --release", + # "CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-pc-linux-gnu --release" ] [tasks.win_test] @@ -30,3 +38,15 @@ script = [ "CROSS_CONTAINER_ENGINE=podman cross test --features test --target x86_64-pc-windows-gnu" ] +[tasks.release] +dependencies = ["win_build","linux_build"] +script_runner = "@shell" +ignore_errors = true +script = [ +"mkdir -p releases", +"rm -f releases/*", +"cp target/x86_64-pc-windows-gnu/release/snp.exe releases/", +"cp target/release/snp releases/", +"strip releases/snp", +"strip releases/snp.exe", +] diff --git a/TODO.md b/TODO.md index d398bf0..0583431 100644 --- a/TODO.md +++ b/TODO.md @@ -2,7 +2,6 @@ [ ] Benchmarks [ ] Better use of file modes [ ] Document non coverage - [ ] Have the the EditWhole save under a different path if name was changed (and edit too) [ ] Find a solution for the .badpocalypse ### Currently working on @@ -30,6 +29,7 @@ [*] Add an option to delete a snippet [*] Coverage [*] Test populate_actions + [*] Have the the EditWhole save under a different path if name was changed (and edit too) ## Posponed or rendered useless [x] Chain the methods when possible with result ? diff --git a/src/errors.rs b/src/errors.rs index ca4ee10..72d62da 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -54,7 +54,7 @@ impl fmt::Display for SnippetError { p.file_name().unwrap(), ), SnippetError::ReparsingError => write!(f,"The snipped couldn't be re-parsed, it might be caused by a bad value in your edit. \nThe snipped was saved as a raw file instead but cant be used as is."), - SnippetError::IndexError(ref i) => write!(f,"An error occured while trying to access the index {} of the given arguments.\nThe default arguments for this command likely do not support {0} arguments",i), + SnippetError::IndexError(ref i) => write!(f,"An error occured while trying to access the index {} of the given arguments.\nThe default arguments for this command likely do not support {} arguments",i,i+1), } } } diff --git a/src/wrapper_functions.rs b/src/wrapper_functions.rs index 9749e28..ac3de9a 100644 --- a/src/wrapper_functions.rs +++ b/src/wrapper_functions.rs @@ -136,7 +136,6 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> { } } }; - // During tests, the editor cannot be tested #[cfg(not(feature = "test"))] let buf = edit::edit(buf)?; @@ -144,12 +143,35 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> { // Try reparsing, on error no modifications will be made match Snippet::from_string(&buf) { Ok(snp) => { - snp.write_over(&p)?; // TODO: Consider not giving a path, as name can be changed in config - println!( - "The snippet {:?} was succesfully saved", - p.file_name().unwrap() - ); - Ok(()) + if cli.snippet == snp.name { + // No renaming has occured, the file can safely be overriten + snp.write_over(&p)?; + println!( + "The snippet {:?} was succesfully saved", + p.file_name().unwrap() + ); + Ok(()) + } else { + let (f, p) = match check_dir_and_open(&snp.name)? { + FileStatus::Opened((p, f)) => { + if cli.force { + // Erasing will take place + (File::create(&p)?, 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 + ); + (f, p) + } + } + FileStatus::NotFound(p) => (File::create(&p)?, p), + }; + snp.save_in(&f)?; + println!("The snippet was successfuly saved in {:?}", p); + Ok(()) + } } Err(_) => { @@ -410,4 +432,33 @@ pub mod test { println!("{:?}", res); assert!(res.is_ok()); } + + #[test] + fn test_resaving_new_name() { + // Try to resave under another name + _ = std::fs::remove_file(snippet_path("resave_snippet").unwrap()); + _ = std::fs::remove_file(snippet_path("new_snippet").unwrap()); + let p = snippet_path("resave_snippet").unwrap(); + let mut f = File::create(p).unwrap(); + + // Populate file with another name + let snp_str = Snippet::empty("new_snippet".to_string(), vec![]); + write!(f, "{}", snp_str).unwrap(); + + // Try to resave + let res = edit_whole_snippet(false_cli!("snp", "resave_snippet")); + println!("Trying to resave without destination {:?}", res); + assert!(res.is_ok()); + + assert!(create_snippet(false_cli!("-c", "new_snippet")).is_err()); + assert!(create_snippet(false_cli!("-c", "resave_snippet")).is_err()); + + let res = edit_whole_snippet(false_cli!("snp", "resave_snippet", "-f")); + println!("Sucessfully erasing {:?}", res); + assert!(res.is_ok()); + + let res = edit_whole_snippet(false_cli!("snp", "resave_snippet")); + println!("Failing to overwrite {:?}", res); + assert!(res.is_err()); + } }