Cleans up a lot of code

This commit is contained in:
beffroi 2023-05-20 23:00:25 +02:00
parent e8d0119e94
commit 3304ba34d8
3 changed files with 59 additions and 59 deletions

View File

@ -18,7 +18,9 @@
[ ] Coverage
[ ] Remake all Snippet methods
[ ]
[ ] Chain the methods when possible with result ?
[ ] Change all uncatchables errors to expect to be more helpful or change them to better errors
### Currently working on
[ ] Remake all helper functions
[*] locate_snippet

View File

@ -38,15 +38,17 @@ impl Snippet {
}
}
// TODO: rewrite as a helper function
/// Save the snippet at the default location under it's name
pub fn save(&self, options: &Cli) -> Result<(), errors::SnippetError> {
let mut file = self.open(&self.name, options)?;
// Try to open the feed
let mut file = self.open(options)?;
write!(file, "{}", serde_yaml::to_string(self)?)?;
Ok(())
}
/// Open the file, will create on empty
pub fn open(&self, name: &str, options: &Cli) -> Result<File, SnippetError> {
pub fn open(&self, options: &Cli) -> Result<File, SnippetError> {
// Find the config folder
let mut path = config_folder()?; //.expect("Coudn't open config forder");
// Add path
@ -62,7 +64,7 @@ impl Snippet {
}
}
}; //.expect("Could not open snippet file.");
let snp: Snippet = serde_yaml::from_reader(&f)?; //.expect("Couldn't parse values");
// let snp: Snippet = serde_yaml::from_reader(&f)?; //.expect("Couldn't parse values");
Ok(f)
}
@ -75,29 +77,29 @@ impl Snippet {
/// Force the write of a file
// pub fn force_save(&self)
/// Tries to load a path and return a snippet
pub fn load(path: String) -> Result<Self, SnippetError> {
todo!();
}
// /// Tries to load a path and return a snippet
// pub fn load(path: String) -> Result<Self, SnippetError> {
// todo!();
// }
/// Write to stdout the snipped with either default arguments or given arguments
pub fn print(&self) -> Result<(), SnippetError> {
todo!();
}
// /// Write to stdout the snipped with either default arguments or given arguments
// pub fn print(&self) -> Result<(), SnippetError> {
// todo!();
// }
/// Delete a snippet
pub fn delete(&self) -> Result<(), SnippetError> {
todo!();
}
// /// Delete a snippet
// pub fn delete(&self) -> Result<(), SnippetError> {
// todo!();
// }
pub fn from_str(raw_content: &str) -> Result<Self, SnippetError> {
// Parse the string
// pub fn from_str(raw_content: &str) -> Result<Self, SnippetError> {
// // Parse the string
// Try to create
// // Try to create
// Return the snippet
todo!();
}
// // Return the snippet
// todo!();
// }
/// Mutate the contents in place
pub fn edit(&mut self) -> Result<(), SnippetError> {

View File

@ -3,12 +3,7 @@
//! All functions included should either take a Cli object and return a SnippetError
//! or be a helper function for such a function
use std::{
env,
fs::{create_dir_all, File},
io::Read,
path::{Path, PathBuf},
};
use std::{env, fs::File, io::Read, path::PathBuf};
use edit::edit;
@ -23,10 +18,10 @@ pub fn locate_snippet(cli: &Cli) -> Result<(), SnippetError> {
Ok(())
}
/// Print the path of the current snippet
fn print_path(path: &Path) {
println!("{}", path.to_string_lossy());
}
// /// 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
@ -70,22 +65,22 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> {
f.read_to_string(&mut raw_snippet)?;
let edited_str = edit(raw_snippet)?;
// Parse the result
let snp = Snippet::from_str(&edited_str)?;
let snp: Snippet = serde_yaml::from_str(&edited_str)?;
// If no error save with snp.save()
snp.save(cli)?;
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(())
}
// /// 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
@ -138,23 +133,24 @@ pub fn print_snippet(args: &Cli) -> Result<(), SnippetError> {
Ok(())
}
/// Open the path linked to a snippet
fn open_path(snp: &Snippet) -> Result<File, SnippetError> {
// Find if the snippet exists
let mut save_path = config_folder()?;
// Err(e) => panic!("The OS couldn't locate a place to save the snippets.\n {}"),
// TODO: Remove as the opening can't be generalized
// /// Open the path linked to a snippet
// fn open_path(snp: &Snippet) -> Result<File, SnippetError> {
// // Find if the snippet exists
// let mut save_path = config_folder()?;
// // Err(e) => panic!("The OS couldn't locate a place to save the snippets.\n {}"),
let file = match create_dir_all(&save_path) {
Ok(()) => {
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),
};
Ok(file)
}
// let file = match create_dir_all(&save_path) {
// Ok(()) => {
// 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),
// };
// Ok(file)
// }
/// Find a configuration folder/name for the given snippet name
pub fn config_folder() -> Result<PathBuf, SnippetError> {