Adds error module (as a stub) and implements a true action mutex-ish
This commit is contained in:
parent
ac4f58f929
commit
f16e6e4ffd
3
TODO.md
3
TODO.md
|
|
@ -15,4 +15,5 @@
|
|||
[-] Have identical attributes with {0} {1} {0} (^^^^^)
|
||||
[ ] Update tests
|
||||
[ ] Benchmarks
|
||||
[ ] Better modes
|
||||
[ ] Better modes
|
||||
[ ] Test populate_actions
|
||||
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
58
src/main.rs
58
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<String>,
|
||||
}
|
||||
|
||||
/// 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<Actions> {
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 ?
|
||||
|
|
|
|||
Reference in New Issue