From 69653497b1e61f1b7c8ea19de30b4217eeebe71a Mon Sep 17 00:00:00 2001 From: lukeflo Date: Fri, 8 Nov 2024 13:36:09 +0100 Subject: error handling if no file is passed as pos arg --- src/bibiman/bibisetup.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index b5dae8e..e17997d 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -17,9 +17,12 @@ use biblatex::{self, Bibliography}; use biblatex::{ChunksExt, Type}; +use color_eyre::owo_colors::OwoColorize; use itertools::Itertools; use std::{fs, path::PathBuf}; +use crate::cliargs; + #[derive(Debug)] pub enum FileFormat { BibLatex, @@ -75,14 +78,30 @@ impl BibiSetup { // Check which file format the passed file has fn check_file_format(main_bibfile: &PathBuf) -> FileFormat { - let extension = main_bibfile.extension().unwrap().to_str(); + if main_bibfile.exists() { + let extension = main_bibfile.extension().unwrap().to_str(); - match extension { - Some("yml") => FileFormat::Hayagriva, - Some("yaml") => FileFormat::Hayagriva, - Some("bib") => FileFormat::BibLatex, - Some(_) => panic!("The extension {:?} is no valid bibfile", extension.unwrap()), - None => panic!("The given path {:?} holds no valid file", main_bibfile), + match extension { + // Some("yml") => FileFormat::Hayagriva, + // Some("yaml") => FileFormat::Hayagriva, + Some("bib") => FileFormat::BibLatex, + Some(_) => panic!( + "The extension \".{:?}\" is no valid bibfile", + extension.unwrap() + ), + None => panic!("The given path {:?} holds no valid file", main_bibfile), + } + } else { + println!( + "{}", + "No bibfile passed as argument. Please select a valid file." + .red() + .bold() + ); + println!(""); + println!("{}", cliargs::help_func()); + // panic!("No file passed as argument. Please choose a .bib file.") + std::process::exit(1) } } -- cgit v1.2.3