From 8937ce7fd40547c2a7a4afeaa1f4b26dd79ddf65 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 19 May 2023 21:59:06 +0200 Subject: [PATCH] Updates TODO and Adds a lot of progress on rework --- TODO.md | 5 +-- src/main.rs | 90 +++++++++++++++++++++++++++-------------------------- 2 files changed, 49 insertions(+), 46 deletions(-) diff --git a/TODO.md b/TODO.md index 8f43f5e..724b70e 100644 --- a/TODO.md +++ b/TODO.md @@ -8,8 +8,9 @@ [ ] ## TODO - [ ] Make a lib for dynamic_format + [ ] Make a lib for dynamic_format and snippets (impl make a trait for have .id() and other future t) [ ] Make custom errors [ ] Transform all panic in errors [ ] Make functions into methods - [ ] Have identical attributes with {0} {1} {0} (^^^^^) \ No newline at end of file + [-] Have identical attributes with {0} {1} {0} (^^^^^) + [ ] Update tests \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 74bf55f..291e790 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,12 +86,13 @@ macro_rules! push_if_some { } /// 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(); + let re = Regex::new(r"\{\d*\}").unwrap(); let matches = re.find_iter(snippet); let mut readhead = 0; // Starting to read at zero let mut token_list = Vec::new(); for r in matches { // Assume we matched text from read to here + println!("{:?}", r); push_if_some!( token_list, create_token_from_pos(snippet, Status::Text, readhead, Some(r.start())) @@ -128,9 +129,6 @@ fn create_token_from_pos( fn main() { let cli = Cli::parse(); - // let origin = "Fill the blank bébé"; - // let edited = edit::edit(origin).unwrap(); - // println!("{}", edited); match cli.create { true => create_snippet(cli), false => print_snippet(&cli), @@ -142,14 +140,6 @@ fn main() { /// * Create a snippet with the given data /// * Write the snippet fn create_snippet(args: Cli) { - // Find if the snippet exists - let mut save_path = match config_folder() { - Ok(path) => path, - Err(e) => panic!( - "The OS couldn't locate a place to save the snippets.\n {}", - e - ), - }; // if args.verbose println!("{}",save_path); let string = if args.edit { edit::edit("").unwrap() @@ -159,31 +149,39 @@ fn create_snippet(args: Cli) { // Create a snippet with the given data let snp = Snippet::new(args.snipplet.clone(), args.args, string); + let (file, save_path) = open_path(&snp); + if args.locate { + println!("Saving snippet to {:?}", save_path); + } - let mut file = match create_dir_all(&save_path) { + // Write the snippet + snp.save(file) + .unwrap_or_else(|_| panic!("Couldn't write yaml to file {:?}", save_path)); +} + +/// Open the path linked to a snippet +fn open_path(snp: &Snippet) -> (File, PathBuf) { + // Find if the snippet exists + let mut save_path = match config_folder() { + Ok(path) => path, + Err(e) => panic!( + "The OS couldn't locate a place to save the snippets.\n {}", + e + ), + }; + + let file = match create_dir_all(&save_path) { Ok(()) => { - save_path.push(format!("{}.yaml", args.snipplet)); - if args.locate { - println!("Saving snippet to {:?}", save_path); - } + save_path.push(format!("{}.yaml", snp.name)); File::create(&save_path).unwrap_or_else(|e| { panic!("Couldn't create snippet file at {:?} {:}", save_path, e) }) } Err(e) => panic!("Couldn't write to path {:?}. {}", save_path, e), }; - // snp.save() - // Write the snippet - write!( - file, - "{}", - serde_yaml::to_string(&snp).expect("Couldn't convert the given arguments into a yaml") - ) - .unwrap_or_else(|_| panic!("Couldn't write yaml to file {:?}", save_path)); + (file, save_path) } -// fn open_path() - /// Find a configuration folder/name for the given snippet name fn config_folder() -> io::Result { let exe = env::current_exe()?; @@ -203,7 +201,7 @@ fn print_snippet(args: &Cli) { let snp: Snippet = serde_yaml::from_reader(f).expect("Couldn't parse values"); // Add the given arguments to the snippet - let final_args = match complete_vec(&args.args, &snp.arguments) { + let final_args = match complete_vec(&args.args, &snp.default_values) { Err(_) => panic!("Too many arguments given to the snippet, aborting"), Ok(argg) => argg, }; @@ -222,7 +220,6 @@ fn print_snippet(args: &Cli) { (false, true) => edit_snippet(&snp), (false, false) => (), } - create_tokens_from_string(&f_str); println!("{}", f_str); } @@ -234,9 +231,11 @@ fn print_path(path: &Path) { /// Edit the snippet and write it again fn edit_snippet(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.arguments.clone(), test); + let snp = Snippet::new(snp.name.clone(), snp.default_values.clone(), test); // TODO: Write snippet . Destroy original ? Return new ? - // snp.save(); + let (file, path) = open_path(&snp); + snp.save(file) + .unwrap_or_else(|e| println!("Coudln't save to file {:?} {}", path, e)); } /// A snippet ready to be Serialized/Deserialized @@ -246,7 +245,7 @@ pub struct Snippet { name: String, /// The default value of all args - arguments: Vec, + default_values: Vec, /// The base string to format content: String, @@ -254,18 +253,22 @@ pub struct Snippet { impl Snippet { /// Create a new snippet - pub fn new(name: String, arguments: Vec, content: String) -> Self { + pub fn new(name: String, default_values: Vec, content: String) -> Self { Snippet { name, - arguments, + default_values, content, } } - // /// Save the snippet at the default location under it's name - // pub fn save(&self) -> Result<(), ()> { - // Ok(()) - // } + /// Save the snippet at the default location under it's name + pub fn save(&self, mut file: File) -> Result<(), std::io::Error> { + write!( + file, + "{}", + serde_yaml::to_string(self).expect("Couldn't convert the given arguments into a yaml") + ) + } /// Tries to load a path and return a snippet // pub fn load(path: String) -> Result { @@ -278,10 +281,6 @@ impl Snippet { } } -// pub struct AccessError{ - -// } - /// A dynamic formatter, formatting a string with any number of /// {} with element of a Vec, by default the search doen't need to /// be exhaustive and wont fail if either the array or format string don't @@ -290,8 +289,11 @@ pub fn dynamic_format(format_string: &str, vector: Vec) -> Result = format_string.split/*_inclusive*/("{}").collect(); - // println!("{:?}", collected_format_string); + // TODO: RESTART FROM HERE, do the permutation + // Implement a to_str or a collect for vec + create_tokens_from_string(format_string); + // println!("{}",); + let collected_format_string: Vec<&str> = format_string.split("{}").collect(); let size_str = collected_format_string.len(); let size_vec = vector.len();