diff --git a/TODO.md b/TODO.md index 76b6f52..fe289d6 100644 --- a/TODO.md +++ b/TODO.md @@ -15,4 +15,5 @@ [-] Have identical attributes with {0} {1} {0} (^^^^^) [ ] Update tests [ ] Benchmarks - [ ] Better modes \ No newline at end of file + [ ] Better modes + [ ] Test populate_actions \ No newline at end of file diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..b52ea84 --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,32 @@ +//! A general error module for the snp program + +/// 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 { + Unknown, + Parse(PlaceHolder), +} + +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"), + } + } +} + +impl error::Error for DoubleError { + 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), + } + } +} diff --git a/src/main.rs b/src/main.rs index 72365b6..059729d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,20 +26,66 @@ pub struct Cli { #[arg(short, long, default_value_t = false)] locate: bool, + /// Launch the editor and edit the snippet + #[arg(short, long, default_value_t = false)] + edit: bool, + + /// Launch the editor and edit the whole snippet + #[arg(short = 'E', long = "Edit", default_value_t = false)] + edit_all: bool, + /// The list of arguments to give to the snippet /// if the -c flag is used, it's used to create the default arguments #[arg(help = "List of args to give to the snipplet")] args: Vec, +} - /// Launch the editor and edit the snippet - #[arg(short, default_value_t = false)] - edit: bool, +/// List of all possible actions, some are mutually exclusive +#[derive(Debug)] +pub enum Actions { + /// Edit the content of the snippet only + Edit, + /// Edit the whole snippet + EditAll, + /// Locate the snippet and show the position + Locate, + /// Show the snippet (with replacement done) + Show, + /// Create the snippet with the given name and arguments + Create, } fn main() { let cli = Cli::parse(); - match cli.create { - true => create_snippet(cli), - false => print_snippet(&cli), + + let list_actions = populate_actions(&cli); + for action in list_actions.iter() { + match action { + Actions::Edit => snippet::edit_snippet(&cli), + Actions::EditAll => snippet::edit_whole_snippet(&cli), + 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), + // } +} + +/// Populate a logical vec of succesive actions to perform depending on arguments +/// 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) { + (true, _, _, _) => actions.push(Actions::Locate), + (_, true, _, true) => actions.append(&mut vec![Actions::Create, Actions::EditAll]), + (_, true, true, false) => actions.append(&mut vec![Actions::Create, Actions::Edit]), + (_, true, false, false) => actions.push(Actions::Create), + (_, false, true, _) => actions.push(Actions::EditAll), + (_, false, false, true) => actions.push(Actions::Edit), + (false, false, false, false) => (), + } + actions } diff --git a/src/snippet.rs b/src/snippet.rs index 4eed4b7..aaf65c5 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -10,6 +10,7 @@ use std::{ use crate::dynamic_formatter::{complete_vec, dynamic_formatter}; use crate::Cli; + /// The parsed snippet #[derive(Debug)] enum Token<'a> { @@ -37,6 +38,7 @@ impl CustFormatter { } } } + /// Assert where the read head is enum Status { /// Read head in inside a formatter @@ -53,6 +55,10 @@ macro_rules! push_if_some { }; }; } + +pub fn edit_whole_snippet(cli: &Cli) {} +pub fn locate_snippet(cli: &Cli) {} + /// Create a token list from the snippet given and return it pub fn create_tokens_from_string(snippet: &str) { let re = Regex::new(r"\{\d*\}").unwrap(); @@ -100,7 +106,7 @@ fn create_token_from_pos( /// * Find if the snippet exists /// * Create a snippet with the given data /// * Write the snippet -pub(crate) fn create_snippet(args: Cli) { +pub fn create_snippet(args: &Cli) { // if args.verbose println!("{}",save_path); let string = if args.edit { edit::edit("").unwrap() @@ -109,7 +115,7 @@ pub(crate) fn create_snippet(args: Cli) { }; // Create a snippet with the given data - let snp = Snippet::new(args.snipplet.clone(), args.args, string); + let snp = Snippet::new(args.snipplet.clone(), args.args.clone(), string); let (file, save_path) = open_path(&snp); if args.locate { println!("Saving snippet to {:?}", save_path); @@ -178,7 +184,7 @@ pub fn print_snippet(args: &Cli) { edit::edit(snp.content).unwrap(); } (true, false) => print_path(&path), - (false, true) => edit_snippet(&snp), + (false, true) => edit_snippet_content(&snp), (false, false) => (), } println!("{}", f_str); @@ -189,8 +195,16 @@ fn print_path(path: &Path) { println!("{}", path.to_string_lossy()); } +/// 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) { + // Load snippet + // Edit content + // Write snippet +} + /// Edit the snippet and write it again -fn edit_snippet(snp: &Snippet) { +fn edit_snippet_content(snp: &Snippet) { let test = edit::edit(&snp.content).expect("Couldn't edit the snippet with the editor"); let snp = Snippet::new(snp.name.clone(), snp.default_values.clone(), test); // TODO: Write snippet . Destroy original ? Return new ?