adds a part of the error Implementation
This commit is contained in:
parent
f16e6e4ffd
commit
8b5b1719e6
|
|
@ -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<clap::error::Error> for SnippetError {
|
||||
fn from(err: clap::error::Error) -> SnippetError {
|
||||
SnippetError::ClapError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for SnippetError {
|
||||
fn from(err: std::io::Error) -> SnippetError {
|
||||
SnippetError::IOError(err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
10
src/main.rs
10
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
|
||||
|
|
|
|||
|
|
@ -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<PathBuf> {
|
|||
}
|
||||
|
||||
/// 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 <name>
|
||||
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
|
||||
|
|
|
|||
Reference in New Issue