-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.rs
142 lines (129 loc) · 3.7 KB
/
output.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use std::error::Error;
use crate::{get_creators, get_date_parts, get_field};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct CSLItem {
pub id: String,
#[serde(rename = "abstract")]
pub abstract_note: String,
pub title: String,
pub author: Vec<CSLAuthor>,
#[serde(rename = "citation-key")]
pub citation_key: String,
pub issued: CSLDate,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct CSLAuthor {
pub family: String,
pub given: String,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct CSLDate {
#[serde(rename = "date-parts")]
pub date_parts: Vec<Vec<i32>>,
}
impl CSLDate {
fn new(date: (i32, Option<i32>, Option<i32>)) -> Self {
let mut parts = vec![date.0];
if let Some(m) = date.1 {
parts.push(m);
if let Some(d) = date.2 {
parts.push(d);
}
}
Self {
date_parts: vec![parts],
}
}
}
fn make_csl(
conn: &Connection,
citation_key: &str,
item_id: i64,
) -> Result<CSLItem, Box<dyn Error>> {
Ok(CSLItem {
id: citation_key.into(),
abstract_note: get_field(&conn, item_id, "abstractNote")?,
title: get_field(&conn, item_id, "title")?,
author: get_creators(&conn, item_id)?
.iter()
.map(|(first, last)| CSLAuthor {
family: last.into(),
given: first.into(),
})
.collect(),
citation_key: citation_key.into(),
issued: CSLDate::new(get_date_parts(&conn, item_id)?),
})
}
pub fn csl_json(
conn: &Connection,
citation_key: &str,
item_id: i64,
) -> Result<String, Box<dyn Error>> {
let serialized = serde_json::to_string(&vec![make_csl(&conn, citation_key, item_id)?])?;
Ok(serialized)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::*;
fn mock_item() -> CSLItem {
CSLItem {
id: "kowalskiSampleTitle2022".into(),
abstract_note: "Abstract for a sample article".into(),
title: "A sample title".into(),
author: vec![CSLAuthor {
family: "Kowalski".into(),
given: "Jan".into(),
}],
citation_key: "kowalskiSampleTitle2022".into(),
issued: CSLDate::new((2022, Some(5), Some(15))),
}
}
#[test]
fn mock_struct() {
let conn = setup_database().unwrap();
let citation_key = "kowalskiSampleTitle2022";
let item_id = 1;
assert_eq!(mock_item(), make_csl(&conn, citation_key, item_id).unwrap());
}
#[test]
fn example_json() {
let conn = setup_database().unwrap();
let citation_key = "kowalskiSampleTitle2022";
let item_id = 1;
let csl_example = r#"
{
"id": "kowalskiSampleTitle2022",
"abstract": "Abstract for a sample article",
"container-title": "",
"DOI": "",
"event": "",
"page": "",
"source": "",
"title": "A sample title",
"author": [
{
"family": "Kowalski",
"given": "Jan"
}
],
"issued": {
"date-parts": [
[
2022,
5,
15
]
]
},
"citation-key": "kowalskiSampleTitle2022"
}"#;
assert_eq!(
serde_json::from_str::<CSLItem>(csl_example).unwrap(),
make_csl(&conn, citation_key, item_id).unwrap()
)
}
}