Skip to content

Commit

Permalink
feat(proc): add ProcReferences
Browse files Browse the repository at this point in the history
Add a struct to handle intermediately rendered output.

The intention is something similar to the haskell citeproc server json.

Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
  • Loading branch information
bdarcus committed Nov 21, 2023
1 parent f59c675 commit d502610
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
6 changes: 3 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use csln::bibliography::HasFile;
use csln::bibliography::InputBibliography as Bibliography;
use csln::citation::Citation;
use csln::style::Style;
use processor::{refs_to_string, Processor};
use processor::Processor;

#[derive(Parser, Default, Debug)]
#[clap(author = "Bruce D'Arcus", version, about = "A CLI for CSLN")]
Expand All @@ -27,6 +27,6 @@ fn main() {
let locale = csln::style::locale::Locale::from_file(&opts.locale);
let processor: Processor = Processor::new(style, bibliography, citations, locale);
let rendered_refs = processor.process_references();
println!("{}", refs_to_string(rendered_refs));
//println!("{}", serde_json::to_string_pretty(&rendered_refs).unwrap());
//println!("{}", refs_to_string(rendered_refs));
println!("{}", serde_json::to_string_pretty(&rendered_refs).unwrap());
}
54 changes: 49 additions & 5 deletions processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SPDX-FileCopyrightText: © 2023 Bruce D'Arcus
use csln::bibliography::reference::InputReference;
use csln::bibliography::reference::{EdtfString, RefID};
use csln::bibliography::InputBibliography as Bibliography;
use csln::citation::Citation;
use csln::citation::{Citation, CitationItem};
use csln::style::locale::Locale;
use csln::style::options::{Config, MonthFormat, SortKey, SubstituteKey};
use csln::style::template::{
Expand Down Expand Up @@ -619,24 +619,68 @@ impl ComponentValues for TemplateDate {
// assert_eq!(rendered_date, "2020");
// }

/// The intermediate representation of renderered citations and bibliography..
#[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)]
pub struct ProcReferences {
pub bibliography: Vec<ProcTemplate>,
pub citations: Option<Vec<ProcTemplate>>,
}

impl Processor {
/// Render references to AST.
#[inline]
pub fn process_references(&self) -> Vec<ProcTemplate> {
pub fn process_references(&self) -> ProcReferences {
let sorted_references = self.sort_references(self.get_references());
sorted_references
let bibliography = sorted_references
.par_iter()
.map(|reference| self.process_reference(reference))

Check failure on line 636 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

mismatched types

Check failure on line 636 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Lints

mismatched types

Check failure on line 636 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Lints

mismatched types

Check failure on line 636 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

mismatched types

Check failure on line 636 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

mismatched types

Check failure on line 636 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

mismatched types
.collect();
ProcReferences { bibliography, citations: None }
}

fn process_citations(
&self,
citations: &[Citation],
) -> Vec<Vec<ProcTemplateComponent>> {
citations
.iter()
.map(|citation| vec![self.process_citation(citation)])
.collect()

Check failure on line 648 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

a value of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>` cannot be built from an iterator over elements of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>`

Check failure on line 648 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Lints

a value of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>` cannot be built from an iterator over elements of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>`

Check failure on line 648 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Lints

a value of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>` cannot be built from an iterator over elements of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>`

Check failure on line 648 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

a value of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>` cannot be built from an iterator over elements of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>`

Check failure on line 648 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

a value of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>` cannot be built from an iterator over elements of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>`

Check failure on line 648 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

a value of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>` cannot be built from an iterator over elements of type `std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>`
}

fn process_citation(&self, citation: &Citation) -> Vec<ProcTemplateComponent> {
let style = self.style.clone();
// map the citation items to a vector of ProcTemplateComponents
let proc_template: Vec<Vec<Vec<ProcTemplateComponent>>> = citation
.citation_items
.iter()
.filter_map(|citation_item| self.process_citation_item(citation_item, style))
.collect();

Check failure on line 658 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

a value of type `std::vec::Vec<std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>>` cannot be built from an iterator over elements of type `std::vec::Vec<ProcTemplateComponent>`

Check failure on line 658 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Lints

a value of type `std::vec::Vec<std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>>` cannot be built from an iterator over elements of type `std::vec::Vec<ProcTemplateComponent>`

Check failure on line 658 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Lints

a value of type `std::vec::Vec<std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>>` cannot be built from an iterator over elements of type `std::vec::Vec<ProcTemplateComponent>`

Check failure on line 658 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

a value of type `std::vec::Vec<std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>>` cannot be built from an iterator over elements of type `std::vec::Vec<ProcTemplateComponent>`

Check failure on line 658 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

a value of type `std::vec::Vec<std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>>` cannot be built from an iterator over elements of type `std::vec::Vec<ProcTemplateComponent>`

Check failure on line 658 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

a value of type `std::vec::Vec<std::vec::Vec<std::vec::Vec<ProcTemplateComponent>>>` cannot be built from an iterator over elements of type `std::vec::Vec<ProcTemplateComponent>`
proc_template

Check failure on line 659 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

mismatched types

Check failure on line 659 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Lints

mismatched types

Check failure on line 659 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Lints

mismatched types

Check failure on line 659 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check

mismatched types

Check failure on line 659 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

mismatched types

Check failure on line 659 in processor/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

mismatched types
}

fn process_citation_item(
&self,
citation_item: &CitationItem,
style: Style,
) -> Option<Vec<ProcTemplateComponent>> {
let citation_style = self.style.citation.clone();
let reference = self.get_reference(&citation_item.ref_id)?;
let template = citation_style?.template.as_slice();
let proc_template = self.process_template(&reference, template);
Some(proc_template)
}

/// Render a reference to AST.
fn process_reference(
&self,
reference: &InputReference,
reference: &Option<InputReference>,
) -> Vec<ProcTemplateComponent> {
let bibliography_style = self.style.bibliography.clone();
self.process_template(reference, bibliography_style.unwrap().template.as_slice())
self.process_template(
reference.as_ref().unwrap(),
bibliography_style.unwrap().template.as_slice(),
)
}

fn get_render_options(&self, style: Style, locale: Locale) -> RenderOptions {
Expand Down

0 comments on commit d502610

Please sign in to comment.