Adds refactor and most function call
This commit is contained in:
parent
700ea029fe
commit
e8d0119e94
12
TODO.md
12
TODO.md
|
|
@ -9,23 +9,23 @@
|
|||
|
||||
## TODO
|
||||
[ ] Make a lib for dynamic_format and snippets (impl make a trait for have .id() and other future t)
|
||||
[ ] Make functions into methods
|
||||
[-] Make functions into methods
|
||||
[-] Have identical attributes with {0} {1} {0} (^^^^^)
|
||||
[ ] Update tests
|
||||
[ ] Benchmarks
|
||||
[ ] Better modes
|
||||
[ ] Test populate_actions
|
||||
[ ] Coverage
|
||||
[ ] Remake all methods
|
||||
[ ] Remake all Snippet methods
|
||||
[ ]
|
||||
|
||||
### Currently working on
|
||||
[ ] Remake all helper functions
|
||||
[ ] edit_snippet
|
||||
[ ] edit_whole_snippet
|
||||
[ ] locate_snippet
|
||||
[*] locate_snippet
|
||||
[*] create_snippet
|
||||
[*] edit_snippet
|
||||
[*] edit_whole_snippet
|
||||
[ ] print_snippet
|
||||
[ ] create_snippet
|
||||
|
||||
|
||||
## Finished
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ 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)]
|
||||
#[allow(dead_code)]
|
||||
pub enum SnippetError {
|
||||
/// A supposedly impossible to trigger error
|
||||
#[allow(dead_code)]
|
||||
|
|
|
|||
10
src/main.rs
10
src/main.rs
|
|
@ -38,6 +38,10 @@ pub struct Cli {
|
|||
/// 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>,
|
||||
|
||||
/// Wether to force the creation or save and overwrite existing data
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
force: bool,
|
||||
}
|
||||
|
||||
/// List of all possible actions, some are mutually exclusive
|
||||
|
|
@ -61,11 +65,11 @@ fn main() -> Result<(), errors::SnippetError> {
|
|||
let list_actions = populate_actions(&cli);
|
||||
for action in list_actions.iter() {
|
||||
match action {
|
||||
Actions::Locate => wrapper_functions::locate_snippet(&cli),
|
||||
Actions::Create => wrapper_functions::create_snippet(&cli),
|
||||
Actions::Show => wrapper_functions::print_snippet(&cli),
|
||||
Actions::Edit => wrapper_functions::edit_snippet(&cli),
|
||||
Actions::EditAll => wrapper_functions::edit_whole_snippet(&cli),
|
||||
Actions::Locate => wrapper_functions::locate_snippet(&cli),
|
||||
Actions::Show => wrapper_functions::print_snippet(&cli),
|
||||
Actions::Create => wrapper_functions::create_snippet(&cli),
|
||||
}?;
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -82,15 +82,15 @@ impl Snippet {
|
|||
|
||||
/// Write to stdout the snipped with either default arguments or given arguments
|
||||
pub fn print(&self) -> Result<(), SnippetError> {
|
||||
Ok(())
|
||||
todo!();
|
||||
}
|
||||
|
||||
/// Delete a snippet
|
||||
pub fn delete(&self) -> Result<(), SnippetError> {
|
||||
Ok(())
|
||||
todo!();
|
||||
}
|
||||
|
||||
pub fn from_str(taw_content: &str) -> Result<Self, SnippetError> {
|
||||
pub fn from_str(raw_content: &str) -> Result<Self, SnippetError> {
|
||||
// Parse the string
|
||||
|
||||
// Try to create
|
||||
|
|
@ -98,4 +98,10 @@ impl Snippet {
|
|||
// Return the snippet
|
||||
todo!();
|
||||
}
|
||||
|
||||
/// Mutate the contents in place
|
||||
pub fn edit(&mut self) -> Result<(), SnippetError> {
|
||||
self.content = edit::edit(self.content.clone())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
use std::{
|
||||
env,
|
||||
fs::{create_dir_all, File},
|
||||
io::{self, Read},
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
|
|
@ -15,6 +15,49 @@ use edit::edit;
|
|||
use crate::dynamic_formatter;
|
||||
use crate::{dynamic_formatter::complete_vec, errors::SnippetError, snippet::Snippet, Cli};
|
||||
|
||||
/// Locate and print a snippet location
|
||||
///
|
||||
/// Will error on snippet not existing
|
||||
pub fn locate_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
||||
println!("{}", snippet_path(&cli.snippet)?.to_string_lossy());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Print the path of the current snippet
|
||||
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) -> Result<(), SnippetError> {
|
||||
// Try loading, if not existant check if create or error
|
||||
let path = snippet_path(&cli.snippet)?;
|
||||
let f = match File::open(&path) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
if cli.force {
|
||||
File::create(&path)?
|
||||
} else {
|
||||
return Err(e.into());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let mut snp: Snippet = match serde_yaml::from_reader(f) {
|
||||
Ok(yaml) => yaml,
|
||||
Err(e) => {
|
||||
if cli.create {
|
||||
Snippet::new(cli.snippet.clone(), cli.args.clone(), String::from(""))
|
||||
} else {
|
||||
return Err(e.into());
|
||||
}
|
||||
}
|
||||
};
|
||||
snp.edit()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Edit the whole snippet and save it where neededi
|
||||
/// Note that the snippet can be saved with another name and might trigger
|
||||
/// a creation or an override
|
||||
|
|
@ -22,7 +65,6 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
// Find location
|
||||
let path = snippet_path(&cli.snippet)?;
|
||||
// Edit the whole file as str
|
||||
|
||||
let mut f = std::fs::File::open(&path)?; //.expect("Could not open snippet file.");
|
||||
let mut raw_snippet = String::from("");
|
||||
f.read_to_string(&mut raw_snippet)?;
|
||||
|
|
@ -30,18 +72,44 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
// Parse the result
|
||||
let snp = Snippet::from_str(&edited_str)?;
|
||||
// If no error save with snp.save()
|
||||
snp.save(cli);
|
||||
snp.save(cli)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Locate and print a snippet location
|
||||
pub fn locate_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
||||
/// Edit the snippet and write it again
|
||||
fn edit_snippet_content(snp: &Snippet) -> Result<(), SnippetError> {
|
||||
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 ?
|
||||
let file = open_path(&snp)?;
|
||||
snp.save_as(file)?;
|
||||
// .unwrap_or_else(|e| println!("Coudln't save to file {:?} {}", path, e));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Print the path of the current snippet
|
||||
fn print_path(path: &Path) {
|
||||
println!("{}", path.to_string_lossy());
|
||||
/// Create a new snippet with the given cli args
|
||||
/// * Find if the snippet exists
|
||||
/// * Create a snippet with the given data
|
||||
/// * Write the snippet
|
||||
pub fn create_snippet(args: &Cli) -> Result<(), SnippetError> {
|
||||
// Create a snippet with the given data
|
||||
// TODO: Remove clone ?
|
||||
let snp = Snippet::new(args.snippet.clone(), args.args.clone(), String::from(""));
|
||||
let path = snippet_path(&args.snippet)?;
|
||||
let f = match File::open(&path) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
if args.force {
|
||||
File::create(&path)?
|
||||
} else {
|
||||
return Err(e.into());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Write the snippet
|
||||
snp.save_as(f)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Print the snippet to stdout
|
||||
|
|
@ -66,68 +134,10 @@ pub fn print_snippet(args: &Cli) -> Result<(), SnippetError> {
|
|||
// };
|
||||
let f_str = dynamic_formatter::dynamic_format(&snp.content, final_args)?;
|
||||
|
||||
match (args.locate, args.edit) {
|
||||
(true, true) => {
|
||||
print_path(&path);
|
||||
edit::edit(snp.content)?;
|
||||
}
|
||||
(true, false) => print_path(&path),
|
||||
(false, true) => edit_snippet_content(&snp)?,
|
||||
(false, false) => (),
|
||||
}
|
||||
println!("{}", f_str);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 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) -> Result<(), SnippetError> {
|
||||
// Try loading, if not existant check if create or error
|
||||
|
||||
// let file = File::open(snippet_path(&cli.snippet)?)?;
|
||||
// Edit content
|
||||
// let content =
|
||||
// Write snippet
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Edit the snippet and write it again
|
||||
fn edit_snippet_content(snp: &Snippet) -> Result<(), SnippetError> {
|
||||
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 ?
|
||||
let file = open_path(&snp)?;
|
||||
snp.save_as(file)?;
|
||||
// .unwrap_or_else(|e| println!("Coudln't save to file {:?} {}", path, e));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Create a new snippet with the given cli args
|
||||
/// * Find if the snippet exists
|
||||
/// * Create a snippet with the given data
|
||||
/// * Write the snippet
|
||||
pub fn create_snippet(args: &Cli) -> Result<(), SnippetError> {
|
||||
// if args.verbose println!("{}",save_path);
|
||||
let string = if args.edit {
|
||||
edit::edit("")?
|
||||
} else {
|
||||
String::from("")
|
||||
};
|
||||
|
||||
// Create a snippet with the given data
|
||||
let snp = Snippet::new(args.snippet.clone(), args.args.clone(), string);
|
||||
let file = open_path(&snp)?;
|
||||
// TODO: LOCATE
|
||||
// if args.locate {
|
||||
// println!("Saving snippet to {:?}", save_path);
|
||||
// }
|
||||
|
||||
// Write the snippet
|
||||
snp.save_as(file)?;
|
||||
// .unwrap_or_else(|_| panic!("Couldn't write yaml to file {:?}", save_path));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Open the path linked to a snippet
|
||||
fn open_path(snp: &Snippet) -> Result<File, SnippetError> {
|
||||
// Find if the snippet exists
|
||||
|
|
|
|||
Reference in New Issue