aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorlukeflo2024-09-20 22:31:33 +0200
committerlukeflo2024-09-20 22:31:33 +0200
commitdc45b960a4eda299058e597f6867e4d4be109b1b (patch)
tree30501aa9f1f26413f959d751302fa189508eacc1 /src/backend
downloadbibiman-dc45b960a4eda299058e597f6867e4d4be109b1b.tar.gz
bibiman-dc45b960a4eda299058e597f6867e4d4be109b1b.zip
initial commit
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/bib.rs51
-rw-r--r--src/backend/cliargs.rs89
2 files changed, 140 insertions, 0 deletions
diff --git a/src/backend/bib.rs b/src/backend/bib.rs
new file mode 100644
index 0000000..aa7272f
--- /dev/null
+++ b/src/backend/bib.rs
@@ -0,0 +1,51 @@
+use biblatex::Bibliography;
+use regex::Regex;
+use std::{
+ fs,
+ path::{Path, PathBuf},
+};
+
+use super::cliargs::{CLIArgs, PosArgs};
+
+// Set necessary fields
+// TODO: can surely be made more efficient/simpler
+pub struct Bibi {
+ pub citekeys: Vec<String>,
+ // pub bibliography: Bibliography,
+}
+
+pub fn get_bibfile(filename: impl AsRef<Path>) -> String {
+ let bibfile = fs::read_to_string(&filename).unwrap();
+ bibfile
+}
+
+pub fn get_citekeys(bibstring: &Bibliography) -> Vec<String> {
+ // let bib = Bibliography::parse(&get_bibfile(CLIArgs::parse_cli_args().bibfilearg)).unwrap();
+ // // Define Regex to match citekeys
+ // let re = Regex::new(r"(?m)^\@[a-zA-Z]*\{(.*)\,").unwrap();
+ // // Declare empty vector to fill with captured keys
+ // // Has to be Vec<&str> because of captures_iter method
+ // let mut keys = vec![];
+ // for (_, [key]) in re.captures_iter(&bibfilestring).map(|c| c.extract()) {
+ // keys.push(key);
+ // }
+ // // Transform Vec<&str> to Vec<String> which is needed by the struct Bibi
+ // let mut citekeys: Vec<String> = keys.into_iter().map(String::from).collect();
+ // // Sort vector items case-insensitive
+ // citekeys.sort_by_key(|name| name.to_lowercase());
+ // citekeys
+ let mut citekeys: Vec<String> = bibstring.iter().map(|entry| entry.to_owned().key).collect();
+ citekeys.sort_by_key(|name| name.to_lowercase());
+ citekeys
+}
+
+impl Bibi {
+ pub fn new() -> Self {
+ // TODO: Needs check for config file path as soon as config file is impl
+ let bib = Bibliography::parse(&get_bibfile(PosArgs::parse_pos_args().bibfilearg)).unwrap();
+ Self {
+ citekeys: get_citekeys(&bib),
+ // bibliography: biblatex::Bibliography::parse(&bibfilestring).unwrap(),
+ }
+ }
+}
diff --git a/src/backend/cliargs.rs b/src/backend/cliargs.rs
new file mode 100644
index 0000000..b820b6a
--- /dev/null
+++ b/src/backend/cliargs.rs
@@ -0,0 +1,89 @@
+use core::panic;
+use std::path::{Path, PathBuf};
+
+use sarge::prelude::*;
+
+sarge! {
+ // Name of the struct
+ ArgumentsCLI,
+
+ // Show help and exit.
+ 'h' help: bool,
+
+ // Show version and exit. TODO: Write version...
+ 'v' version: bool,
+
+ // Option for file: -b - short option; --bibfile - long option
+ // #ok makes it optional
+ #ok 'b' bibfile: String,
+}
+
+// struct for CLIArgs
+pub struct CLIArgs {
+ pub helparg: bool,
+ pub versionarg: bool,
+}
+
+impl CLIArgs {
+ pub fn parse_cli_args() -> Self {
+ let (cli_args, _) = ArgumentsCLI::parse().expect("Could not parse CLI arguments");
+ Self {
+ helparg: cli_args.help,
+ versionarg: cli_args.version,
+ }
+ }
+}
+
+// Struct for positional arguments
+// TODO: Can surely be improved!!
+pub struct PosArgs {
+ pub bibfilearg: PathBuf,
+}
+
+impl PosArgs {
+ pub fn parse_pos_args() -> Self {
+ let (_, pos_args) = ArgumentsCLI::parse().expect("Could not parse positional arguments");
+ Self {
+ bibfilearg: if pos_args.len() > 1 {
+ PathBuf::from(&pos_args[1])
+ // pos_args[1].to_string()
+ } else {
+ panic!("No path to bibfile provided as argument")
+ }, // bibfilearg: pos_args[1].to_string(),
+ }
+ }
+}
+
+pub fn help_func() -> String {
+ let help = format!(
+ "\
+{} {}
+
+USAGE:
+ bibiman [FLAGS] [file]
+
+POSITIONAL ARGS:
+ <file> Path to .bib file
+
+FLAGS:
+ -h, --help Show this help and exit
+ -v, --version Show the version and exit",
+ env!("CARGO_PKG_NAME"),
+ env!("CARGO_PKG_VERSION"),
+ );
+ help
+}
+
+pub fn version_func() -> String {
+ let version = format!(
+ "\
+{} {}
+{}
+{}",
+ env!("CARGO_PKG_NAME"),
+ env!("CARGO_PKG_VERSION"),
+ env!("CARGO_PKG_AUTHORS"),
+ env!("CARGO_PKG_LICENSE")
+ );
+ version
+}