Skip to content

Commit

Permalink
refactor: make API composable rather than configurable, make separate…
Browse files Browse the repository at this point in the history
… bin for configurable execution
  • Loading branch information
Firgrep committed Aug 29, 2024
1 parent 792428d commit 717350a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 45 deletions.
62 changes: 29 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,41 +85,37 @@ pub mod inserter;
pub mod utils;
pub mod validator;

use std::io::Error;

use biblatex::Entry;
use utils::{BiblatexUtils, CoreUtils};
use validator::ArticleFileData;

pub struct Prepyrus {}

/// Main API entry point for running Prepyrus.
///
/// ## Example
/// ```
/// let bib_src_file = "tests/mocks/test.bib";
/// let target_path = "tests/mocks/data";
/// let mode = "verify";
///
/// use prepyrus::run_prepyrus;
/// let result = run_prepyrus(bib_src_file, target_path, mode);
/// assert!(result.is_ok());
/// ```
pub fn run_prepyrus(
bib_file: &str,
target_path: &str,
mode: &str,
) -> Result<(), Box<dyn std::error::Error>> {
CoreUtils::verify_arguments(&vec![
bib_file.to_string(),
target_path.to_string(),
mode.to_string(),
]);

let all_entries = BiblatexUtils::retrieve_bibliography_entries(bib_file)?;
let mdx_paths = CoreUtils::extract_paths(target_path)?;

// Phase 1: Verify MDX files
let articles_file_data = validator::verify_mdx_files(mdx_paths.clone(), &all_entries)?;

// Phase 2: Process MDX files (requires mode to be set to "process")
if mode == "process" {
inserter::process_mdx_files(articles_file_data);
impl Prepyrus {
pub fn verify_config(args: &Vec<String>) -> utils::VerifiedConfig {
CoreUtils::verify_config(args)
}

Ok(())
pub fn get_all_bib_entries(
bib_file: &str,
) -> Result<Vec<biblatex::Entry>, Box<dyn std::error::Error>> {
Ok(BiblatexUtils::retrieve_bibliography_entries(bib_file)?)
}

pub fn get_mdx_paths(target_path: &str) -> Result<Vec<String>, Box<dyn std::error::Error>> {
Ok(CoreUtils::extract_paths(target_path)?)
}

pub fn verify(
mdx_paths: Vec<String>,
all_entries: &Vec<Entry>,
) -> Result<Vec<ArticleFileData>, Error> {
validator::verify_mdx_files(mdx_paths, &all_entries)
}

pub fn process(all_articles: Vec<ArticleFileData>) {
inserter::process_mdx_files(all_articles)
}
}
24 changes: 24 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use prepyrus::{utils::VerifiedConfig, Prepyrus};

fn main() {
let args: Vec<String> = std::env::args().collect();

let VerifiedConfig {
bib_file,
target_path,
mode,
} = Prepyrus::verify_config(&args);

let all_entries = Prepyrus::get_all_bib_entries(&bib_file).unwrap();
let mdx_paths = Prepyrus::get_mdx_paths(&target_path).unwrap();

// Phase 1: Verify MDX files
let articles_file_data = Prepyrus::verify(mdx_paths, &all_entries).unwrap();

// Phase 2: Process MDX files (requires mode to be set to "process")
if mode == "process" {
Prepyrus::process(articles_file_data);
}

println!("===Prepyrus completed successfully!");
}
16 changes: 15 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ impl BiblatexUtils {
}
}

pub struct VerifiedConfig {
pub bib_file: String,
pub target_path: String,
pub mode: String,
}

impl CoreUtils {
pub fn extract_paths(path: &str) -> io::Result<Vec<String>> {
let exceptions = vec![
Expand All @@ -93,7 +99,7 @@ impl CoreUtils {
Ok(mdx_paths)
}

pub fn verify_arguments(args: &Vec<String>) {
pub fn verify_config(args: &Vec<String>) -> VerifiedConfig {
if args.len() < 3 {
eprintln!("Arguments missing: <bibliography.bib> <target_dir_or_file> <mode>");
std::process::exit(1);
Expand All @@ -111,6 +117,14 @@ impl CoreUtils {
eprintln!("Invalid mode. Please provide either 'verify' or 'process'.");
std::process::exit(1);
}

let config = VerifiedConfig {
bib_file: args[0].clone(),
target_path: args[1].clone(),
mode: args[2].clone(),
};

config
}

/// Excavates all MDX files in a directory and its subdirectories
Expand Down
50 changes: 39 additions & 11 deletions tests/integration_test_verify.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
use prepyrus::run_prepyrus;
use prepyrus::{utils::VerifiedConfig, Prepyrus};

#[test]
fn run_verify_with_directory() {
let result = run_prepyrus("tests/mocks/test.bib", "tests/mocks/data", "verify");
println!("{:?}", result);
assert!(result.is_ok());
let args = vec![
"tests/mocks/test.bib".to_string(),
"tests/mocks/data".to_string(),
"verify".to_string(),
];
let VerifiedConfig {
bib_file,
target_path,
mode,
} = Prepyrus::verify_config(&args);

let all_entries = Prepyrus::get_all_bib_entries(&bib_file).unwrap();
let mdx_paths = Prepyrus::get_mdx_paths(&target_path).unwrap();
let articles_file_data = Prepyrus::verify(mdx_paths, &all_entries).unwrap();

println!("{:?}", articles_file_data);
assert!(mode == "verify");
assert!(articles_file_data.len() > 1);
assert!(!articles_file_data.is_empty());
}

#[test]
fn run_verify_with_single_file() {
let result = run_prepyrus(
"tests/mocks/test.bib",
"tests/mocks/data/science-of-logic-introduction.mdx",
"verify",
);
println!("{:?}", result);
assert!(result.is_ok());
let args = vec![
"tests/mocks/test.bib".to_string(),
"tests/mocks/data/science-of-logic-introduction.mdx".to_string(),
"verify".to_string(),
];
let VerifiedConfig {
bib_file,
target_path,
mode,
} = Prepyrus::verify_config(&args);

let all_entries = Prepyrus::get_all_bib_entries(&bib_file).unwrap();
let mdx_paths = Prepyrus::get_mdx_paths(&target_path).unwrap();
let articles_file_data = Prepyrus::verify(mdx_paths, &all_entries).unwrap();

println!("{:?}", articles_file_data);
assert!(mode == "verify");
assert!(articles_file_data.len() == 1);
assert!(!articles_file_data.is_empty());
}

0 comments on commit 717350a

Please sign in to comment.