Skip to content

Commit

Permalink
feat(citation): read citation files
Browse files Browse the repository at this point in the history
Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
  • Loading branch information
bdarcus committed Nov 23, 2023
1 parent 13f10f8 commit a9887cb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
6 changes: 5 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use clap::Parser;
use csln::bibliography::HasFile;
use csln::bibliography::InputBibliography as Bibliography;
use csln::citation::Citation;
use csln::style::Style;
use csln::HasFile;
use processor::Processor;

#[derive(Parser, Default, Debug)]
Expand All @@ -15,6 +15,9 @@ pub struct Opts {
/// The path to the CSLN bibliography file
bibliography: String,
#[clap(short, long)]
/// The optional path to the CSLN citation file
citation: Option<String>,
#[clap(short, long)]
/// The path to the CSLN locale file
locale: String,
}
Expand All @@ -23,6 +26,7 @@ fn main() {
let opts = Opts::parse();
let style: Style = Style::from_file(&opts.style);
let bibliography: Bibliography = Bibliography::from_file(&opts.bibliography);
// TODO load citations from file
let citations: Vec<Citation> = Vec::new();
let locale = csln::style::locale::Locale::from_file(&opts.locale);
let processor: Processor = Processor::new(style, bibliography, citations, locale);
Expand Down
6 changes: 1 addition & 5 deletions csln/src/bibliography/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::HasFile;
use std::collections::HashMap;
use std::fs;

Expand All @@ -7,11 +8,6 @@ pub use reference::InputReference;
/// A bibliography is a collection of references.
pub type InputBibliography = HashMap<String, InputReference>;

// REVIEW move this to a core traits.rs module?
pub trait HasFile {
fn from_file(path: &str) -> Self;
}

impl HasFile for InputBibliography {
/// Load and parse a YAML or JSON bibliography file.
fn from_file(bib_path: &str) -> InputBibliography {
Expand Down
17 changes: 16 additions & 1 deletion csln/src/citation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
// originally converted from the typescript source with quicktype
use crate::HasFile;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fs;

impl HasFile for Vec<Citation> {
fn from_file(citations_path: &str) -> Vec<Citation> {
let contents =
fs::read_to_string(citations_path).expect("Failed to read citations file");
if citations_path.ends_with(".json") {
serde_json::from_str(&contents).expect("Failed to parse JSON")
} else if citations_path.ends_with(".yaml") || citations_path.ends_with(".yml") {
serde_yaml::from_str(&contents).expect("Failed to parse YAML")
} else {
panic!("Citations file must be in YAML or JSON format")
}
}
}

/// A vector of Citation objects.
#[derive(Debug, Default, Serialize, Deserialize, JsonSchema)]
Expand Down
4 changes: 4 additions & 0 deletions csln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ pub mod bibliography;
pub use bibliography::InputBibliography;

pub mod citation;

pub trait HasFile {
fn from_file(path: &str) -> Self;
}
2 changes: 1 addition & 1 deletion processor/benches/proc_bench.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use criterion::{criterion_group, criterion_main, Criterion};
use csln::bibliography::HasFile;
use csln::bibliography::InputBibliography as Bibliography;
use csln::citation::Citation;
use csln::style::Style;
use csln::HasFile;
use csln_processor::Processor;
use std::time::Duration;

Expand Down
2 changes: 1 addition & 1 deletion processor/tests/processor_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use csln::bibliography::HasFile;
use csln::citation::Citation;
use csln::HasFile;
// create tests for Processor::get_proc_references and Processor::sort_proc_references
#[test]
fn sorts_references() {
Expand Down

0 comments on commit a9887cb

Please sign in to comment.