From c16adefa562724b6504bb4e0acedde215b17248a Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 23 May 2023 04:19:49 +0200 Subject: [PATCH] Adds a rename option --- Cargo.toml | 6 ++- TODO.md | 2 + src/actions.rs | 46 +++++++++++++++----- src/main.rs | 4 ++ src/tokenizer.rs | 6 +++ src/wrapper_functions.rs | 93 ++++++++++++++++++++++++++++++++++------ 6 files changed, 133 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9479535..f46fc2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,18 @@ [package] name = "snp" -author = "Dandaedre" +authors = ["Dandaedre"] version = "1.0.1" edition = "2021" [features] test = [] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + [profile.test] opt-level = 0 + +[profile.release] +opt-level = "z" [dependencies] clap = { version = "4.2.7", features = ["derive"] } diff --git a/TODO.md b/TODO.md index 03757a7..d398bf0 100644 --- a/TODO.md +++ b/TODO.md @@ -17,6 +17,7 @@ [*] edit_snippet [*] edit_whole_snippet [*] print_snippet + [*] rename_snippet [*] Finish tests on [*] locate_snippet [*] create_snippet @@ -24,6 +25,7 @@ [*] edit_whole_snippet [*] print_snippet [*] delete snippet + [*] rename snippet [*] Have identical attributes with {0} {1} {0} (^^^^^) [*] Add an option to delete a snippet [*] Coverage diff --git a/src/actions.rs b/src/actions.rs index 0a734b8..4066e97 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -21,6 +21,8 @@ pub enum Actions { Create, /// Deletes the snippet Delete, + /// Renames the snippet + Rename, } /// The main worker, execute all commands given as Args @@ -34,6 +36,7 @@ pub fn execute_parsed_cli(cli: Cli) -> Result<(), errors::SnippetError> { Actions::Edit => wrapper_functions::edit_snippet(&cli), Actions::EditAll => wrapper_functions::edit_whole_snippet(&cli), 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() { @@ -49,15 +52,23 @@ pub fn execute_parsed_cli(cli: Cli) -> Result<(), errors::SnippetError> { /// even when the arguments don't make sense fn populate_actions(cli: &Cli) -> Vec { let mut actions = Vec::new(); - match (cli.locate, cli.create, cli.edit, cli.edit_all, cli.delete) { - (true, _, _, _, false) => actions.push(Actions::Locate), - (_, false, false, true, false) => actions.push(Actions::EditAll), - (_, true, _, true, false) => actions.push(Actions::EditAll), - (_, true, false, false, false) => actions.push(Actions::Create), - (_, false, true, _, false) => actions.push(Actions::Edit), - (_, true, true, false, false) => actions.push(Actions::Edit), - (false, false, false, false, false) => actions.push(Actions::Show), - (_, _, _, _, true) => actions.push(Actions::Delete), + match ( + cli.locate, + cli.create, + cli.edit, + cli.edit_all, + cli.delete, + &cli.rename, + ) { + (true, _, _, _, false, _) => actions.push(Actions::Locate), + (_, false, false, true, false, _) => actions.push(Actions::EditAll), + (_, true, _, true, false, _) => actions.push(Actions::EditAll), + (_, true, false, false, false, _) => actions.push(Actions::Create), + (_, false, true, _, false, _) => actions.push(Actions::Edit), + (_, true, true, false, false, _) => actions.push(Actions::Edit), + (false, false, false, false, false, None) => actions.push(Actions::Show), + (_, _, _, _, true, _) => actions.push(Actions::Delete), + (_, _, _, _, _, Some(_)) => actions.push(Actions::Rename), } actions } @@ -109,6 +120,18 @@ mod test { false_cli!("snp", "name", "-d", "-c", "-E", "-e", "-l"), vec![Actions::Delete] ), + ( + false_cli!("snp", "name", "-r", "new_name"), + vec![Actions::Rename] + ), + ( + false_cli!("snp", "name", "-r", "new_name", "-f"), + vec![Actions::Rename] + ), + ( + false_cli!("snp", "name", "-c", "-r", "new_name", "-f"), + vec![Actions::Create] + ), ); for (cli, res) in group.iter() { println!("Testing {:?}..actions expected {:?}", cli, res); @@ -124,7 +147,10 @@ mod test { false_cli!("snp", "parsed_cli", "-c", "-f"), false_cli!("snp", "parsed_cli"), false_cli!("snp", "parsed_cli", "-E", "-f"), - false_cli!("snp", "parsed_cli", "-d"), + false_cli!("snp", "parsed_cli", "-r", "new_name", "-f"), + false_cli!("snp", "parsed_cli", "-c"), + false_cli!("snp", "parsed_cli", "-r", "new_name", "-f"), + false_cli!("snp", "new_name", "-d"), ]; let bad_cli = vec![ diff --git a/src/main.rs b/src/main.rs index 63a7e9b..b07f945 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,10 @@ pub struct Cli { /// Wether to delete a snippet, takes precedence over everything #[arg(short, long, default_value_t = false)] delete: bool, + + /// Wether to rename the snippet + #[arg(short, long)] + rename: Option, } /// Main entry point, everything is defered for testing and coverage diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 598738a..e0ccf22 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -109,6 +109,12 @@ fn create_token_from_pos( mod test { use super::*; + #[test] + fn test_derive() { + println!("{:?}", CustFormatter::new("45")); + println!("{:?}", SnippetToken::Text("Bar")); + } + #[test] fn test_create_token_from_pos() { // All calls diff --git a/src/wrapper_functions.rs b/src/wrapper_functions.rs index f42480d..9749e28 100644 --- a/src/wrapper_functions.rs +++ b/src/wrapper_functions.rs @@ -14,17 +14,6 @@ use crate::errors::SnippetError; use crate::snippet::Snippet; use crate::{dynamic_formatter, Cli}; -// Potential way to chain all actions without re-opening -// /// The status of the snippet in memory -// pub enum PotentialSnippet { -// /// The snippet wasn't loaded -// FileDest(File), -// /// The snippet was loaded -// LoadedSnippet(Snippet), -// /// No snippet currently exists -// None, -// } - /// Locate and print a snippet location /// /// Will error on snippet not existing @@ -61,6 +50,44 @@ pub fn create_snippet(cli: &Cli) -> Result<(), SnippetError> { Ok(()) } +/// Rename a snippet with the new given name +pub fn rename_snippet(cli: &Cli) -> Result<(), SnippetError> { + // Check wether the snippet exitsts + let new_name = cli.rename.as_ref().unwrap(); + if new_name == &cli.snippet { + return Ok(()); // The noop is a success ? + } + let (f, oldpath) = match check_dir_and_open(&cli.snippet)? { + FileStatus::Opened((p, f)) => (f, p), + FileStatus::NotFound(e) => return Err(SnippetError::NotFound(e)), + }; + let mut snp = Snippet::from_file(&f)?; + // Check whether the destination is writable + let (f, _) = match check_dir_and_open(new_name)? { + FileStatus::Opened((p, _)) => { + if cli.force { + snp.name = new_name.clone(); + (File::create(&p)?, p) + } else { + return Err(SnippetError::FoundNoEraseError(p)); + } + } + FileStatus::NotFound(p) => { + snp.name = new_name.clone(); + (File::create(&p)?, p) + } + }; + // Save and delete + snp.save_in(&f)?; + std::fs::remove_file(oldpath)?; + // Save snippet + println!( + "The snippet {} was sucessfuly renamed as {}", + cli.snippet, new_name + ); + Ok(()) +} + /// Open and load snippet and edit it pub fn edit_snippet(cli: &Cli) -> Result<(), SnippetError> { // Try loading, if not existant check if create or error @@ -256,8 +283,8 @@ pub mod test { #[test] fn test_delete_snippet() { - assert!(create_snippet(false_cli!("-c", "delete_snippet")).is_ok()); - assert!(delete_snippet(false_cli!("-d", "delete_snippet")).is_ok()); + assert!(create_snippet(false_cli!("snp", "-c", "-f", "delete_snippet")).is_ok()); + assert!(delete_snippet(false_cli!("snp", "-d", "delete_snippet")).is_ok()); } #[test] @@ -343,4 +370,44 @@ pub mod test { )); }; } + + #[test] + fn test_rename_snippet() { + let oldpath = snippet_path("rename_snippet").unwrap(); + let newpath = snippet_path("renamed_snippet").unwrap(); + let filledpath = snippet_path("filled_destination").unwrap(); + _ = std::fs::remove_file(&oldpath); + _ = std::fs::remove_file(&newpath); + _ = std::fs::remove_file(filledpath); + assert!(create_snippet(false_cli!("-c", "rename_snippet", "-f")).is_ok()); + assert!( + rename_snippet(false_cli!("snp", "-r", "renamed_snippet", "rename_snippet")).is_ok() + ); + + assert!(std::fs::remove_file(oldpath).is_err()); + assert!(std::fs::remove_file(newpath).is_ok()); + + assert!(rename_snippet(false_cli!("snp", "-r", "non_ex", "non_ex")).is_ok()); + assert!(rename_snippet(false_cli!("snp", "-r", "non_ex", "renamed_snippet")).is_err()); + assert!(create_snippet(false_cli!("-c", "filled_destination", "-f")).is_ok()); + assert!(create_snippet(false_cli!("-c", "rename_snippet", "-f")).is_ok()); + assert!(matches!( + rename_snippet(false_cli!( + "snp", + "rename_snippet", + "-r", + "filled_destination" + )), + Err(SnippetError::FoundNoEraseError(_)) + )); + let res = rename_snippet(false_cli!( + "snp", + "rename_snippet", + "-r", + "filled_destination", + "-f" + )); + println!("{:?}", res); + assert!(res.is_ok()); + } }