diff --git a/src/errors.rs b/src/errors.rs index 2393200..33d6f89 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -16,8 +16,8 @@ pub enum SnippetError { IOError(std::io::Error), /// Yaml error, generated during serialisation of snippets YAMLError(serde_yaml::Error), - /// The file already exists and no creation flag was set - ExistsError(std::io::Error), + /// The file doesn't exits and can't be used + NotFound(PathBuf), /// The file doen't exist and no creation was asked NotFoundNoRetryError(PathBuf), /// The snippet was found but shouldn't be erased because of flags @@ -35,9 +35,9 @@ impl fmt::Display for SnippetError { SnippetError::YAMLError(..) => { write!(f, "Couldn't convert the given arguments into a yaml") } - SnippetError::ExistsError(..) => write!( + SnippetError::NotFound(ref p) => write!( f, - "The file already exists. Use -c or -f to create it or force overwrite." + "The file {:?} doesn't exists",p.file_name().unwrap(), ), SnippetError::NotFoundNoRetryError(ref p) => { write!( @@ -66,7 +66,7 @@ impl error::Error for SnippetError { SnippetError::ClapError(ref e) => Some(e), SnippetError::IOError(ref e) => Some(e), SnippetError::YAMLError(ref e) => Some(e), - SnippetError::ExistsError(ref e) => Some(e), + SnippetError::NotFound(_) => None, SnippetError::NotFoundNoRetryError(_) => None, SnippetError::FoundNoEraseError(_) => None, SnippetError::ReparsingError => None, diff --git a/src/main.rs b/src/main.rs index e6cf156..c15f6e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,7 +99,7 @@ fn populate_actions(cli: &Cli) -> Vec { (_, true, false, false) => actions.push(Actions::Create), (_, false, true, _) => actions.push(Actions::Edit), (_, false, false, true) => actions.push(Actions::EditAll), - (false, false, false, false) => (), + (false, false, false, false) => actions.push(Actions::Show), } actions } diff --git a/src/wrapper_functions.rs b/src/wrapper_functions.rs index 139f282..77e2f69 100644 --- a/src/wrapper_functions.rs +++ b/src/wrapper_functions.rs @@ -3,12 +3,13 @@ //! All functions included should either take a Cli object and return a SnippetError //! or be a helper function for such a function +#[allow(unused)] // Mandatory because of how test and rust-analyser work +use edit::edit; + use std::fs::create_dir_all; use std::io::{read_to_string, Write}; use std::{env, fs::File, path::PathBuf}; -use edit::edit; - use crate::dynamic_formatter; use crate::{dynamic_formatter::complete_vec, errors::SnippetError, snippet::Snippet, Cli}; @@ -160,25 +161,19 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> { // } /// Print the snippet to stdout -pub fn print_snippet(args: &Cli) -> Result<(), SnippetError> { +pub fn print_snippet(cli: &Cli) -> Result<(), SnippetError> { // Try to load the snippet - let mut path = config_folder()?; //.expect("Coudn't open config forder"); - path.push(format!("{}.yaml", args.snippet)); - let f = std::fs::File::open(&path)?; //.expect("Could not open snippet file."); - let snp: Snippet = serde_yaml::from_reader(f)?; //.expect("Couldn't parse values"); + let (p, f) = match check_dir_and_open(&cli.snippet)? { + FileStatus::Opened((p, f)) => (p, f), + FileStatus::NotFound(p) => return Err(SnippetError::NotFound(p)), + }; + + let snp = Snippet::from_file(&f)?; // Add the given arguments to the snippet - // let final_args = match complete_vec(&args.args, &snp.default_values) { - // Err(_) => panic!("Too many arguments given to the snippet, aborting"), - // Ok(argg) => argg, - // }; - let final_args = complete_vec(&args.args, &snp.default_values)?; + let final_args = complete_vec(&cli.args, &snp.default_values)?; // Print the snippet - // let f_str = match dynamic_formatter::dynamic_formatter(&snp.content, final_args) { - // Err(_) => panic!("Couldn't format the snippet !"), - // Ok(sn) => sn, - // }; let f_str = dynamic_formatter::dynamic_format(&snp.content, final_args)?; println!("{}", f_str); @@ -215,7 +210,6 @@ pub fn config_folder() -> Result { let dir = exe.parent().expect("Executable must be in some directory"); let mut dir = dir.join(".config"); dir.push("snp_config"); - // dir.push(format!("{}.yaml", name)); Ok(dir) }