Adds entry point for print

This commit is contained in:
Dandaedre 2023-05-22 03:30:49 +02:00
parent 92b3ccf730
commit c83dfd8f80
3 changed files with 17 additions and 23 deletions

View File

@ -16,8 +16,8 @@ pub enum SnippetError {
IOError(std::io::Error),
/// Yaml error, generated during serialisation of snippets
YAMLError(serde_yaml::Error),
/// The file already exists and no creation flag was set
ExistsError(std::io::Error),
/// The file doesn't exits and can't be used
NotFound(PathBuf),
/// The file doen't exist and no creation was asked
NotFoundNoRetryError(PathBuf),
/// The snippet was found but shouldn't be erased because of flags
@ -35,9 +35,9 @@ impl fmt::Display for SnippetError {
SnippetError::YAMLError(..) => {
write!(f, "Couldn't convert the given arguments into a yaml")
}
SnippetError::ExistsError(..) => write!(
SnippetError::NotFound(ref p) => write!(
f,
"The file already exists. Use -c or -f to create it or force overwrite."
"The file {:?} doesn't exists",p.file_name().unwrap(),
),
SnippetError::NotFoundNoRetryError(ref p) => {
write!(
@ -66,7 +66,7 @@ impl error::Error for SnippetError {
SnippetError::ClapError(ref e) => Some(e),
SnippetError::IOError(ref e) => Some(e),
SnippetError::YAMLError(ref e) => Some(e),
SnippetError::ExistsError(ref e) => Some(e),
SnippetError::NotFound(_) => None,
SnippetError::NotFoundNoRetryError(_) => None,
SnippetError::FoundNoEraseError(_) => None,
SnippetError::ReparsingError => None,

View File

@ -99,7 +99,7 @@ fn populate_actions(cli: &Cli) -> Vec<Actions> {
(_, true, false, false) => actions.push(Actions::Create),
(_, false, true, _) => actions.push(Actions::Edit),
(_, false, false, true) => actions.push(Actions::EditAll),
(false, false, false, false) => (),
(false, false, false, false) => actions.push(Actions::Show),
}
actions
}

View File

@ -3,12 +3,13 @@
//! All functions included should either take a Cli object and return a SnippetError
//! or be a helper function for such a function
#[allow(unused)] // Mandatory because of how test and rust-analyser work
use edit::edit;
use std::fs::create_dir_all;
use std::io::{read_to_string, Write};
use std::{env, fs::File, path::PathBuf};
use edit::edit;
use crate::dynamic_formatter;
use crate::{dynamic_formatter::complete_vec, errors::SnippetError, snippet::Snippet, Cli};
@ -160,25 +161,19 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> {
// }
/// Print the snippet to stdout
pub fn print_snippet(args: &Cli) -> Result<(), SnippetError> {
pub fn print_snippet(cli: &Cli) -> Result<(), SnippetError> {
// Try to load the snippet <name>
let mut path = config_folder()?; //.expect("Coudn't open config forder");
path.push(format!("{}.yaml", args.snippet));
let f = std::fs::File::open(&path)?; //.expect("Could not open snippet file.");
let snp: Snippet = serde_yaml::from_reader(f)?; //.expect("Couldn't parse values");
let (p, f) = match check_dir_and_open(&cli.snippet)? {
FileStatus::Opened((p, f)) => (p, f),
FileStatus::NotFound(p) => return Err(SnippetError::NotFound(p)),
};
let snp = Snippet::from_file(&f)?;
// Add the given arguments to the snippet
// let final_args = match complete_vec(&args.args, &snp.default_values) {
// Err(_) => panic!("Too many arguments given to the snippet, aborting"),
// Ok(argg) => argg,
// };
let final_args = complete_vec(&args.args, &snp.default_values)?;
let final_args = complete_vec(&cli.args, &snp.default_values)?;
// Print the snippet
// let f_str = match dynamic_formatter::dynamic_formatter(&snp.content, final_args) {
// Err(_) => panic!("Couldn't format the snippet !"),
// Ok(sn) => sn,
// };
let f_str = dynamic_formatter::dynamic_format(&snp.content, final_args)?;
println!("{}", f_str);
@ -215,7 +210,6 @@ pub fn config_folder() -> Result<PathBuf, SnippetError> {
let dir = exe.parent().expect("Executable must be in some directory");
let mut dir = dir.join(".config");
dir.push("snp_config");
// dir.push(format!("{}.yaml", name));
Ok(dir)
}