From e1e46765a0b695f3bec2431a99601237780be99e Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sun, 21 May 2023 18:52:18 +0200 Subject: [PATCH] Adds and tests snippet creation --- TODO.md | 2 +- src/snippet.rs | 33 ++++++--------------------------- src/wrapper_functions.rs | 39 ++++++++++++++++++--------------------- 3 files changed, 25 insertions(+), 49 deletions(-) diff --git a/TODO.md b/TODO.md index c50f5c8..e37d4ed 100644 --- a/TODO.md +++ b/TODO.md @@ -23,7 +23,7 @@ ### Currently working on [ ] Proceed one by one and catch errors - [ ] Remake all helper functions + [-] Remake all helper functions [*] locate_snippet [*] create_snippet [-] edit_snippet diff --git a/src/snippet.rs b/src/snippet.rs index 4818132..0106c99 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -74,36 +74,15 @@ impl Snippet { Ok(()) } - /// Force the write of a file - // pub fn force_save(&self) - - // /// Tries to load a path and return a snippet - // pub fn load(path: String) -> Result { - // todo!(); - // } - - // /// Write to stdout the snipped with either default arguments or given arguments - // pub fn print(&self) -> Result<(), SnippetError> { - // todo!(); - // } - - // /// Delete a snippet - // pub fn delete(&self) -> Result<(), SnippetError> { - // todo!(); - // } - - // pub fn from_str(raw_content: &str) -> Result { - // // Parse the string - - // // Try to create - - // // Return the snippet - // todo!(); - // } - /// Mutate the contents in place pub fn edit(&mut self) -> Result<(), SnippetError> { self.content = edit::edit(self.content.clone())?; Ok(()) } + + /// Create snippet from file buffer (deserialize) + pub fn from_file(f: File) -> Result { + let snp: Snippet = serde_yaml::from_reader(f)?; + Ok(snp) + } } diff --git a/src/wrapper_functions.rs b/src/wrapper_functions.rs index bc72253..f408739 100644 --- a/src/wrapper_functions.rs +++ b/src/wrapper_functions.rs @@ -60,26 +60,14 @@ pub fn create_snippet(cli: &Cli) -> Result<(), SnippetError> { /// 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 - - let path = snippet_path(&cli.snippet)?; - let f = match File::open(&path) { - Ok(f) => f, - Err(e) => { + let status = check_dir_and_open(&cli.snippet)?; + let mut snp = match status { + FileStatus::Opened((p, f)) => Snippet::from_file(f)?, + FileStatus::NotFound(p) => { if cli.force { - File::create(&path)? - } else { - return Err(e.into()); - } - } - }; - - let mut snp: Snippet = match serde_yaml::from_reader(f) { - Ok(yaml) => yaml, - Err(e) => { - if cli.create { Snippet::new(cli.snippet.clone(), cli.args.clone(), String::from("")) } else { - return Err(e.into()); + return Err(SnippetError::NotFoundNoRetryError(p)); } } }; @@ -194,11 +182,9 @@ fn snippet_path(snp: &str) -> Result { #[cfg(test)] mod test { - use std::io::Write; - - use clap::Parser; - use super::*; + use clap::Parser; + use std::io::Write; macro_rules! false_cli { ($($arg:expr),+) => { @@ -236,6 +222,7 @@ mod test { #[test] fn test_create() { + _ = std::fs::remove_file(snippet_path("create_snippet").unwrap()); // Test to create empty assert!(create_snippet(false_cli!("-c", "create_snippet")).is_ok()); @@ -245,4 +232,14 @@ mod test { // Test to create with force assert!(create_snippet(false_cli!("-c", "-f", "create_snippet")).is_ok()); } + + #[test] + fn test_edit() { + // Test edit non existant + + // Test edit with create + + // Test + // todo!() + } }