Skip to content

Commit 7ad2c3a

Browse files
committed
refactor(proc): impl render traits
Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
1 parent 1677cc7 commit 7ad2c3a

File tree

2 files changed

+168
-140
lines changed

2 files changed

+168
-140
lines changed

bibliography/src/reference.rs

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
use edtf::level_1::Edtf;
21
use schemars::JsonSchema;
32
use serde::{Deserialize, Serialize};
4-
use style::template::{
5-
Contributors, DateForm, Dates, StyleTemplateContributor, StyleTemplateDate,
6-
StyleTemplateList, StyleTemplateTitle, Titles,
7-
};
8-
//use edtf::DateComplete;
93

104
#[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)]
115
pub struct InputReference {
@@ -20,110 +14,3 @@ pub struct InputReference {
2014
pub accessed: Option<String>,
2115
pub note: Option<String>,
2216
}
23-
24-
impl InputReference {
25-
pub fn render_names(names: Vec<String>) -> String {
26-
let mut name_string = String::new();
27-
if names.len() == 1 {
28-
name_string = names[0].to_string();
29-
} else if names.len() == 2 {
30-
name_string = names.join(" and ");
31-
} else if names.len() > 2 {
32-
let last_author = names.last().unwrap();
33-
let other_authors = &names[..names.len() - 1];
34-
name_string = other_authors.join(", ");
35-
name_string.push_str(", and ");
36-
name_string.push_str(last_author);
37-
}
38-
name_string
39-
}
40-
41-
pub fn render_contributors(
42-
&self,
43-
template_component: &StyleTemplateContributor,
44-
) -> String {
45-
match template_component.contributor {
46-
Contributors::Author => {
47-
let authors = self
48-
.author
49-
.as_ref()
50-
.unwrap_or(&Vec::new())
51-
.iter()
52-
.map(|name| name.to_string())
53-
.collect::<Vec<String>>();
54-
InputReference::render_names(authors)
55-
}
56-
Contributors::Editor => {
57-
let editors = self
58-
.editor
59-
.as_ref()
60-
.unwrap()
61-
.iter()
62-
.map(|editor| editor.to_string())
63-
.collect::<Vec<String>>();
64-
InputReference::render_names(editors)
65-
}
66-
Contributors::Translator => {
67-
let translators = self
68-
.translator
69-
.as_ref()
70-
.unwrap()
71-
.iter()
72-
.map(|translator| translator.to_string())
73-
.collect::<Vec<String>>();
74-
InputReference::render_names(translators)
75-
}
76-
Contributors::Director => todo!(),
77-
Contributors::Recipient => todo!(),
78-
Contributors::Interviewer => todo!(),
79-
Contributors::Interviewee => todo!(),
80-
Contributors::Inventor => todo!(),
81-
Contributors::Counsel => todo!(),
82-
Contributors::Composer => todo!(),
83-
Contributors::Publisher => todo!(),
84-
}
85-
}
86-
87-
pub fn render_edtf_date(&self, date_string: &str, _format_string: &str) -> String {
88-
let edtf_date: Edtf = Edtf::parse(date_string).unwrap();
89-
let formatted_date: String = match edtf_date {
90-
// TODO need localized date rendering, using format_string
91-
Edtf::Date(date) => date.to_string(),
92-
Edtf::DateTime { .. } => todo!(),
93-
Edtf::Interval { .. } => todo!(),
94-
Edtf::IntervalFrom { .. } => todo!(),
95-
Edtf::IntervalTo { .. } => todo!(),
96-
Edtf::YYear { .. } => todo!(),
97-
};
98-
formatted_date
99-
}
100-
101-
pub fn render_date(&self, template_component: &StyleTemplateDate) -> String {
102-
let date_string: &str = match template_component.date {
103-
Dates::Issued => self.issued.as_ref().unwrap(),
104-
Dates::Accessed => self.accessed.as_ref().unwrap(),
105-
Dates::OriginalPublished => todo!(),
106-
};
107-
108-
let format_string: &str = match template_component.form {
109-
DateForm::Year => "%Y",
110-
DateForm::YearMonth => "%Y-%m",
111-
DateForm::Full => "%Y-%m-%d",
112-
DateForm::MonthDay => "%m-%d",
113-
};
114-
115-
self.render_edtf_date(date_string, format_string)
116-
}
117-
118-
pub fn render_title(&self, template_component: &StyleTemplateTitle) -> String {
119-
let title: &str = match template_component.title {
120-
Titles::Title => self.title.as_ref().unwrap(),
121-
Titles::ContainerTitle => todo!(),
122-
};
123-
title.to_string()
124-
}
125-
126-
pub fn render_list(&self, _template_component: &StyleTemplateList) -> String {
127-
"".to_string() // TODO
128-
}
129-
}

processor/src/lib.rs

Lines changed: 168 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
use bibliography::InputBibliography as Bibliography;
2+
use bibliography::InputReference;
3+
use edtf::level_1::Edtf;
4+
use rayon::prelude::*;
15
use schemars::JsonSchema;
26
use serde::{Deserialize, Serialize};
3-
use rayon::prelude::*;
47
use std::collections::HashMap;
58
use std::fmt::Debug;
69
use std::fs;
710
use std::option::Option;
811
use std::sync::{Arc, Mutex};
9-
10-
use bibliography::InputBibliography as Bibliography;
11-
use bibliography::InputReference;
1212
use style::options::{SortOrder, StyleSortGroupKey, StyleSorting};
1313
#[allow(unused_imports)] // for now
1414
use style::template::{
1515
Contributors, DateForm, Dates, StyleTemplateComponent, StyleTemplateContributor,
16+
StyleTemplateDate, StyleTemplateList, StyleTemplateTitle, Titles,
1617
};
1718
use style::Style;
1819

@@ -116,16 +117,171 @@ impl Default for ProcHints {
116117
}
117118
}
118119

119-
impl Processor {
120+
pub trait Render<T> {
121+
fn render(&self, reference: &InputReference, component: &T) -> String;
122+
}
123+
124+
// WTD???
125+
126+
pub trait RenderComponent {
127+
fn render(
128+
&self,
129+
reference: &InputReference,
130+
// context: &RenderContext<T>,
131+
) -> String;
132+
}
133+
134+
pub trait RenderDate {
135+
fn render(
136+
&self,
137+
reference: &InputReference,
138+
// context: &RenderContext<T>,
139+
) -> String;
140+
141+
fn render_date(&self, date_string: &str, format_string: &str) -> String;
142+
}
143+
144+
pub trait RenderTitle {
145+
fn render(
146+
&self,
147+
reference: &InputReference,
148+
// context: &RenderContext<T>,
149+
) -> String;
150+
}
151+
152+
pub trait RenderContributor {
153+
fn render(
154+
&self,
155+
reference: &InputReference,
156+
) -> String;
157+
}
158+
159+
impl RenderComponent for StyleTemplateComponent {
160+
fn render(
161+
&self,
162+
reference: &InputReference,
163+
// context: &RenderContext<T>,
164+
) -> String {
165+
match self {
166+
StyleTemplateComponent::Title(title) => title.render(reference),
167+
StyleTemplateComponent::Contributor(contributor) => {
168+
contributor.render(reference)
169+
}
170+
StyleTemplateComponent::Date(date) => date.render(reference),
171+
StyleTemplateComponent::List(_list) => todo!(),
172+
}
173+
}
174+
}
175+
176+
impl<T: RenderContributor + ?Sized> dyn Render<T> {
177+
pub fn render(names: Vec<String>) -> String {
178+
names.join(", ")
179+
}
180+
}
120181

182+
impl RenderTitle for StyleTemplateTitle {
183+
fn render(
184+
&self,
185+
reference: &InputReference,
186+
) -> String {
187+
let title: &str = match &self.title {
188+
Titles::Title => reference.title.as_ref().unwrap(),
189+
Titles::ContainerTitle => todo!(),
190+
};
191+
title.to_string()
192+
}
193+
}
194+
195+
impl RenderContributor for StyleTemplateContributor {
196+
fn render(
197+
&self,
198+
reference: &InputReference,
199+
) -> String {
200+
match &self.contributor {
201+
Contributors::Author => {
202+
let authors = reference
203+
.author
204+
.as_ref()
205+
.unwrap_or(&Vec::new())
206+
.par_iter()
207+
.map(|name| name.to_string())
208+
.collect::<Vec<String>>();
209+
authors.join(", ")
210+
}
211+
Contributors::Editor => {
212+
let editors = reference
213+
.editor
214+
.as_ref()
215+
.unwrap()
216+
.par_iter()
217+
.map(|editor| editor.to_string())
218+
.collect::<Vec<String>>();
219+
editors.join(", ")
220+
}
221+
Contributors::Translator => {
222+
let translators = reference
223+
.translator
224+
.as_ref()
225+
.unwrap()
226+
.par_iter()
227+
.map(|translator| translator.to_string())
228+
.collect::<Vec<String>>();
229+
translators.join(", ")
230+
}
231+
Contributors::Director => todo!(),
232+
Contributors::Publisher => todo!(),
233+
Contributors::Recipient => todo!(),
234+
Contributors::Interviewer => todo!(),
235+
Contributors::Interviewee => todo!(),
236+
Contributors::Composer => todo!(),
237+
Contributors::Inventor => todo!(),
238+
Contributors::Counsel => todo!(),
239+
}
240+
}
241+
}
242+
243+
impl RenderDate for StyleTemplateDate {
244+
fn render(
245+
&self,
246+
reference: &InputReference,
247+
) -> String {
248+
let date_string: &str = match self.date {
249+
Dates::Issued => reference.issued.as_ref().unwrap(),
250+
Dates::Accessed => reference.accessed.as_ref().unwrap(),
251+
Dates::OriginalPublished => todo!(),
252+
};
253+
254+
let format_string: &str = match self.form {
255+
DateForm::Year => "%Y",
256+
DateForm::YearMonth => "%Y-%m",
257+
DateForm::Full => "%Y-%m-%d",
258+
DateForm::MonthDay => "%m-%d",
259+
};
260+
261+
self.render_date(date_string, format_string)
262+
}
263+
fn render_date(&self, date_string: &str, _format_string: &str) -> String {
264+
let edtf_date: Edtf = Edtf::parse(date_string).unwrap();
265+
let formatted_date: String = match edtf_date {
266+
// TODO need localized date rendering, using format_string
267+
Edtf::Date(date) => date.to_string(),
268+
Edtf::DateTime { .. } => todo!(),
269+
Edtf::Interval { .. } => todo!(),
270+
Edtf::IntervalFrom { .. } => todo!(),
271+
Edtf::IntervalTo { .. } => todo!(),
272+
Edtf::YYear { .. } => todo!(),
273+
};
274+
formatted_date
275+
}
276+
}
277+
278+
impl Processor {
121279
/// Render references to AST.
122280
pub fn render_references(&self) -> Vec<ProcTemplate> {
123281
let sorted_references = self.sort_references(self.get_references());
124282
sorted_references
125283
.par_iter()
126-
.map(|reference| {
127-
self.render_reference(reference)
128-
})
284+
.map(|reference| self.render_reference(reference))
129285
.collect()
130286
}
131287

@@ -134,11 +290,10 @@ impl Processor {
134290
let bibliography_style = self.style.bibliography.clone();
135291
bibliography_style
136292
.map(|style| {
137-
style.template
293+
style
294+
.template
138295
.par_iter()
139-
.map(|component| {
140-
self.render_template_component(component, reference)
141-
})
296+
.map(|component| self.render_template_component(component, reference))
142297
.collect()
143298
})
144299
.unwrap_or_else(std::vec::Vec::new)
@@ -149,23 +304,9 @@ impl Processor {
149304
component: &StyleTemplateComponent,
150305
reference: &InputReference,
151306
) -> ProcTemplateComponent {
152-
let rendered = match component {
153-
StyleTemplateComponent::Date(date) => {
154-
reference.render_date(date)
155-
}
156-
StyleTemplateComponent::Contributor(contributor) => {
157-
reference.render_contributors(contributor)
158-
}
159-
StyleTemplateComponent::Title(title) => {
160-
reference.render_title(title)
161-
}
162-
StyleTemplateComponent::List(list) => {
163-
reference.render_list(list)
164-
}
165-
};
166307
ProcTemplateComponent {
167308
template_component: component.clone(),
168-
value: rendered,
309+
value: component.render(reference),
169310
}
170311
}
171312

0 commit comments

Comments
 (0)