Adds and tests snippet creation
This commit is contained in:
parent
8e5d6a0e81
commit
e1e46765a0
2
TODO.md
2
TODO.md
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
### Currently working on
|
||||
[ ] Proceed one by one and catch errors
|
||||
[ ] Remake all helper functions
|
||||
[-] Remake all helper functions
|
||||
[*] locate_snippet
|
||||
[*] create_snippet
|
||||
[-] edit_snippet
|
||||
|
|
|
|||
|
|
@ -74,36 +74,15 @@ impl Snippet {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// 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!();
|
||||
// }
|
||||
|
||||
// /// 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!();
|
||||
// }
|
||||
|
||||
// pub fn from_str(raw_content: &str) -> Result<Self, SnippetError> {
|
||||
// // Parse the string
|
||||
|
||||
// // Try to create
|
||||
|
||||
// // Return the snippet
|
||||
// todo!();
|
||||
// }
|
||||
|
||||
/// Mutate the contents in place
|
||||
pub fn edit(&mut self) -> Result<(), SnippetError> {
|
||||
self.content = edit::edit(self.content.clone())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Create snippet from file buffer (deserialize)
|
||||
pub fn from_file(f: File) -> Result<Self, SnippetError> {
|
||||
let snp: Snippet = serde_yaml::from_reader(f)?;
|
||||
Ok(snp)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,26 +60,14 @@ pub fn create_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
/// Open and load snippet and edit it
|
||||
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) => {
|
||||
let status = check_dir_and_open(&cli.snippet)?;
|
||||
let mut snp = match status {
|
||||
FileStatus::Opened((p, f)) => Snippet::from_file(f)?,
|
||||
FileStatus::NotFound(p) => {
|
||||
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());
|
||||
return Err(SnippetError::NotFoundNoRetryError(p));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -194,11 +182,9 @@ fn snippet_path(snp: &str) -> Result<PathBuf, SnippetError> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::io::Write;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
use super::*;
|
||||
use clap::Parser;
|
||||
use std::io::Write;
|
||||
|
||||
macro_rules! false_cli {
|
||||
($($arg:expr),+) => {
|
||||
|
|
@ -236,6 +222,7 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn test_create() {
|
||||
_ = std::fs::remove_file(snippet_path("create_snippet").unwrap());
|
||||
// Test to create empty
|
||||
assert!(create_snippet(false_cli!("-c", "create_snippet")).is_ok());
|
||||
|
||||
|
|
@ -245,4 +232,14 @@ mod test {
|
|||
// Test to create with force
|
||||
assert!(create_snippet(false_cli!("-c", "-f", "create_snippet")).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edit() {
|
||||
// Test edit non existant
|
||||
|
||||
// Test edit with create
|
||||
|
||||
// Test
|
||||
// todo!()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in New Issue