From 8b5b1719e60b2607add0e1aceb460223ca1ed484 Mon Sep 17 00:00:00 2001 From: beffroi Date: Sat, 20 May 2023 13:55:36 +0200 Subject: [PATCH] adds a part of the error Implementation --- src/errors.rs | 33 +++++++++++++++++++++++++++------ src/main.rs | 10 ++++------ src/snippet.rs | 29 ++++++++++++++++++++--------- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index b52ea84..e818013 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,32 +1,53 @@ //! A general error module for the snp program +use std::{error, fmt}; + /// Snippet error, general error wrapper for the Snippet module /// All error are wrapper with more information available with the source() method #[derive(Debug)] -enum SnippetError { +pub enum SnippetError { + /// A supposedly impossible to trigger error + #[allow(dead_code)] Unknown, - Parse(PlaceHolder), + /// Clap error, originating from the Cli parser + ClapError(clap::error::Error), + /// A generic IOError, the source is mandatory to get the specifics + IOError(std::io::Error), } +#[derive(Debug)] struct PlaceHolder {} impl fmt::Display for SnippetError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { SnippetError::Unknown => write!(f, "An unknonw error occured"), - // DoubleError::Parse(..) => - // write!(f, "the provided string could not be parsed as int"), + SnippetError::ClapError(..) => write!(f, "A known error occured"), + SnippetError::IOError(..) => write!(f, "An error occured while editing the file"), } } } -impl error::Error for DoubleError { +impl error::Error for SnippetError { fn source(&self) -> Option<&(dyn error::Error + 'static)> { match *self { SnippetError::Unknown => None, // The cause is the underlying implementation error type. Is implicitly // cast to the trait object `&error::Error`. This works because the // underlying type already implements the `Error` trait. - DoubleError::Parse(ref e) => Some(e), + SnippetError::ClapError(ref e) => Some(e), + SnippetError::IOError(ref e) => Some(e), } } } + +impl From for SnippetError { + fn from(err: clap::error::Error) -> SnippetError { + SnippetError::ClapError(err) + } +} + +impl From for SnippetError { + fn from(err: std::io::Error) -> SnippetError { + SnippetError::IOError(err) + } +} diff --git a/src/main.rs b/src/main.rs index 059729d..b11296b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use clap::Parser; mod dynamic_formatter; +mod errors; mod snippet; use crate::snippet::{create_snippet, print_snippet}; @@ -55,7 +56,7 @@ pub enum Actions { Create, } -fn main() { +fn main() -> Result<(), errors::SnippetError> { let cli = Cli::parse(); let list_actions = populate_actions(&cli); @@ -66,12 +67,9 @@ fn main() { Actions::Locate => snippet::locate_snippet(&cli), Actions::Show => snippet::print_snippet(&cli), Actions::Create => snippet::create_snippet(&cli), - } + }?; } - // match cli.create { - // true => create_snippet(cli), - // false => print_snippet(&cli), - // } + Ok(()) } /// Populate a logical vec of succesive actions to perform depending on arguments diff --git a/src/snippet.rs b/src/snippet.rs index aaf65c5..78c066f 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -1,3 +1,4 @@ +use clap::error::Result; use regex::Regex; use serde::{Deserialize, Serialize}; @@ -8,8 +9,11 @@ use std::{ path::{Path, PathBuf}, }; -use crate::dynamic_formatter::{complete_vec, dynamic_formatter}; use crate::Cli; +use crate::{ + dynamic_formatter::{complete_vec, dynamic_formatter}, + errors, +}; /// The parsed snippet #[derive(Debug)] @@ -56,8 +60,12 @@ macro_rules! push_if_some { }; } -pub fn edit_whole_snippet(cli: &Cli) {} -pub fn locate_snippet(cli: &Cli) {} +pub fn edit_whole_snippet(cli: &Cli) -> Result<(), errors::SnippetError> { + Ok(()) +} +pub fn locate_snippet(cli: &Cli) -> Result<(), errors::SnippetError> { + Ok(()) +} /// Create a token list from the snippet given and return it pub fn create_tokens_from_string(snippet: &str) { @@ -106,10 +114,10 @@ fn create_token_from_pos( /// * Find if the snippet exists /// * Create a snippet with the given data /// * Write the snippet -pub fn create_snippet(args: &Cli) { +pub fn create_snippet(args: &Cli) -> Result<(), errors::SnippetError> { // if args.verbose println!("{}",save_path); let string = if args.edit { - edit::edit("").unwrap() + edit::edit("")? } else { String::from("") }; @@ -122,8 +130,9 @@ pub fn create_snippet(args: &Cli) { } // Write the snippet - snp.save(file) - .unwrap_or_else(|_| panic!("Couldn't write yaml to file {:?}", save_path)); + snp.save(file)?; + // .unwrap_or_else(|_| panic!("Couldn't write yaml to file {:?}", save_path)); + Ok(()) } /// Open the path linked to a snippet @@ -160,7 +169,7 @@ fn config_folder() -> io::Result { } /// Print the snippet to stdout -pub fn print_snippet(args: &Cli) { +pub fn print_snippet(args: &Cli) -> Result<(), errors::SnippetError> { // Try to load the snippet let mut path = config_folder().expect("Coudln't open config folder"); path.push(format!("{}.yaml", args.snipplet)); @@ -188,6 +197,7 @@ pub fn print_snippet(args: &Cli) { (false, false) => (), } println!("{}", f_str); + Ok(()) } /// Print the path of the current snippet @@ -197,10 +207,11 @@ fn print_path(path: &Path) { /// Open and load snippet and edit it /// If chained with create, the file will be created, closed and opened again -pub fn edit_snippet(cli: &Cli) { +pub fn edit_snippet(cli: &Cli) -> Result<(), errors::SnippetError> { // Load snippet // Edit content // Write snippet + Ok(()) } /// Edit the snippet and write it again