biblib 0.4.2

Parse, manage, and deduplicate academic citations
Documentation
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! EndNote XML parsing implementation.
//!
//! This module provides the core parsing logic for EndNote XML format.

use crate::error::{ParseError, SourceSpan, ValueError};
use crate::{Author, Citation, CitationFormat};
use quick_xml::Reader;
use quick_xml::events::Event;
use quick_xml::name::QName;
use std::io::BufRead;

/// Convert buffer position to approximate line number
fn buffer_position_to_line_number(content: &str, pos: usize) -> usize {
    if pos >= content.len() {
        return content.lines().count();
    }
    content[..pos].lines().count()
}

/// Enhanced extract_text function that tracks line numbers for better error reporting
fn extract_text_with_position<B: BufRead>(
    reader: &mut Reader<B>,
    buf: &mut Vec<u8>,
    closing_tag: &[u8],
    content: &str,
    start_pos: usize,
) -> Result<String, ParseError> {
    let mut text = String::new();
    let closing_tag_str = String::from_utf8_lossy(closing_tag);

    loop {
        let current_pos = reader.buffer_position() as usize;
        match reader.read_event_into(buf) {
            Ok(Event::Text(e)) => {
                text.push_str(&e.unescape().map_err(|e| {
                    let line_num = buffer_position_to_line_number(content, current_pos);
                    ParseError::at_line(
                        line_num,
                        CitationFormat::EndNoteXml,
                        ValueError::Syntax(format!("Invalid XML text content: {}", e)),
                    )
                })?);
            }
            Ok(Event::End(e)) if e.name() == QName(closing_tag) => break,
            Ok(Event::Eof) => {
                let line_num = buffer_position_to_line_number(content, current_pos);
                let end_pos = reader.buffer_position() as usize;
                return Err(ParseError::at_line(
                    line_num,
                    CitationFormat::EndNoteXml,
                    ValueError::Syntax(format!(
                        "Unexpected EOF while looking for closing tag '{}'",
                        closing_tag_str
                    )),
                )
                .with_span(SourceSpan::new(start_pos, end_pos)));
            }
            Err(e) => {
                let line_num = buffer_position_to_line_number(content, current_pos);
                let end_pos = reader.buffer_position() as usize;
                return Err(ParseError::at_line(
                    line_num,
                    CitationFormat::EndNoteXml,
                    ValueError::Syntax(format!("XML parsing error: {}", e)),
                )
                .with_span(SourceSpan::new(start_pos, end_pos)));
            }
            _ => continue,
        }
        buf.clear();
    }

    Ok(text.trim().to_string())
}

/// Parse EndNote XML content into citations.
///
/// This function parses EndNote XML format and returns a vector of citations.
/// It uses quick_xml for robust XML parsing and converts to our Citation structure.
///
/// # Arguments
///
/// * `content` - The EndNote XML content as a string
///
/// # Returns
///
/// A Result containing either a vector of citations or a parsing error.
///
/// ```
pub(crate) fn parse_endnote_xml(content: &str) -> Result<Vec<Citation>, ParseError> {
    if content.trim().is_empty() {
        return Ok(Vec::new());
    }

    let mut reader = Reader::from_str(content);
    reader.config_mut().trim_text(true);

    let mut citations = Vec::new();
    let mut buf = Vec::new();

    loop {
        let pos = reader.buffer_position() as usize;
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) if e.name() == QName(b"record") => {
                citations.push(parse_record(&mut reader, &mut buf, content, pos)?);
            }
            Ok(Event::Eof) => break,
            Err(e) => {
                let pos = reader.buffer_position() as usize;
                let line = buffer_position_to_line_number(content, pos);
                return Err(ParseError::at_line(
                    line,
                    CitationFormat::EndNoteXml,
                    ValueError::Syntax(format!("XML parsing error: {}", e)),
                ));
            }
            _ => (),
        }
        buf.clear();
    }

    // Return empty vector instead of error for empty but valid XML
    Ok(citations)
}

/// Extracts date components (year, month, day) from a year element
fn extract_date_from_year_element<B: BufRead>(
    reader: &mut Reader<B>,
    e: &quick_xml::events::BytesStart,
    content: &str,
) -> Result<(Option<i32>, Option<u8>, Option<u8>), ParseError> {
    let mut year_val = None;
    let mut month_val = None;
    let mut day_val = None;

    // First, extract attributes if present
    let attr_pos = reader.buffer_position() as usize;
    let attr_line = buffer_position_to_line_number(content, attr_pos);
    for attr in e.attributes() {
        let attr = attr.map_err(|e| {
            ParseError::at_line(
                attr_line,
                CitationFormat::EndNoteXml,
                ValueError::Syntax(format!("Invalid attribute: {}", e)),
            )
        })?;
        match attr.key.as_ref() {
            b"year" => {
                if let Ok(year_str) = std::str::from_utf8(&attr.value) {
                    year_val = year_str.parse::<i32>().ok();
                }
            }
            b"month" => {
                if let Ok(month_str) = std::str::from_utf8(&attr.value) {
                    month_val = month_str
                        .parse::<u8>()
                        .ok()
                        .filter(|&m| (1..=12).contains(&m));
                }
            }
            b"day" => {
                if let Ok(day_str) = std::str::from_utf8(&attr.value) {
                    day_val = day_str
                        .parse::<u8>()
                        .ok()
                        .filter(|&d| (1..=31).contains(&d));
                }
            }
            _ => {}
        }
    }

    // If no year attribute, try to get year from text content
    if year_val.is_none() {
        let mut local_buf = Vec::new();
        let start_pos = reader.buffer_position() as usize;
        if let Ok(year) =
            extract_text_with_position(reader, &mut local_buf, b"year", content, start_pos)?
                .parse::<i32>()
        {
            year_val = Some(year);
        }
    } else {
        // Still need to consume the text content
        let mut local_buf = Vec::new();
        let start_pos = reader.buffer_position() as usize;
        let _ = extract_text_with_position(reader, &mut local_buf, b"year", content, start_pos)?;
    }

    Ok((year_val, month_val, day_val))
}

/// Parse a single record element into a Citation
fn parse_record<B: BufRead>(
    reader: &mut Reader<B>,
    buf: &mut Vec<u8>,
    content: &str,
    start_pos: usize,
) -> Result<Citation, ParseError> {
    let mut citation = Citation::new();

    loop {
        match reader.read_event_into(buf) {
            Ok(Event::Start(ref e)) => match e.name().as_ref() {
                b"ref-type" => {
                    let attr_pos = reader.buffer_position() as usize;
                    let attr_line = buffer_position_to_line_number(content, attr_pos);
                    for attr in e.attributes() {
                        let attr = attr.map_err(|e| {
                            ParseError::at_line(
                                attr_line,
                                CitationFormat::EndNoteXml,
                                ValueError::Syntax(format!("Invalid attribute: {}", e)),
                            )
                        })?;
                        if attr.key.as_ref() == b"name" {
                            citation.citation_type.push(
                                attr.unescape_value()
                                    .map_err(|e| {
                                        ParseError::at_line(
                                            attr_line,
                                            CitationFormat::EndNoteXml,
                                            ValueError::Syntax(format!(
                                                "Invalid attribute value: {}",
                                                e
                                            )),
                                        )
                                    })?
                                    .into_owned(),
                            );
                        }
                    }
                }
                b"title" => {
                    let pos = reader.buffer_position() as usize;
                    citation.title =
                        extract_text_with_position(reader, buf, b"title", content, pos)?;
                }
                b"author" => {
                    let pos = reader.buffer_position() as usize;
                    let author_str =
                        extract_text_with_position(reader, buf, b"author", content, pos)?;
                    let (family, given) = crate::utils::parse_author_name(&author_str);
                    let (given_opt, middle_opt) = if given.is_empty() {
                        (None, None)
                    } else {
                        crate::utils::split_given_and_middle(&given)
                    };
                    citation.authors.push(Author {
                        name: family,
                        given_name: given_opt,
                        middle_name: middle_opt,
                        affiliations: Vec::new(),
                    });
                }
                b"secondary-title" => {
                    let pos = reader.buffer_position() as usize;
                    let sec_title =
                        extract_text_with_position(reader, buf, b"secondary-title", content, pos)?;
                    // If no primary title, use secondary-title as title
                    if citation.title.is_empty() {
                        citation.title = sec_title;
                    } else {
                        citation.journal = Some(sec_title);
                    }
                }
                b"alt-title" => {
                    let pos = reader.buffer_position() as usize;
                    let alt_title =
                        extract_text_with_position(reader, buf, b"alt-title", content, pos)?;
                    // If no primary title or journal is set, use alt-title as title
                    if citation.title.is_empty() && citation.journal.is_none() {
                        citation.title = alt_title;
                    } else if citation.journal.is_none() {
                        citation.journal = Some(alt_title);
                    } else {
                        citation.journal_abbr = Some(alt_title);
                    }
                }
                b"custom2" => {
                    let pos = reader.buffer_position() as usize;
                    let text = extract_text_with_position(reader, buf, b"custom2", content, pos)?;
                    // Check for PMC ID patterns
                    if text.to_lowercase().contains("pmc") || text.starts_with("PMC") {
                        citation.pmc_id = Some(text);
                    }
                }
                b"volume" => {
                    let pos = reader.buffer_position() as usize;
                    citation.volume = Some(extract_text_with_position(
                        reader, buf, b"volume", content, pos,
                    )?);
                }
                b"number" => {
                    let pos = reader.buffer_position() as usize;
                    citation.issue = Some(extract_text_with_position(
                        reader, buf, b"number", content, pos,
                    )?);
                }
                b"pages" => {
                    let pos = reader.buffer_position() as usize;
                    let pages = extract_text_with_position(reader, buf, b"pages", content, pos)?;
                    citation.pages = Some(crate::utils::format_page_numbers(&pages));
                }
                b"electronic-resource-num" => {
                    let pos = reader.buffer_position() as usize;
                    let doi = extract_text_with_position(
                        reader,
                        buf,
                        b"electronic-resource-num",
                        content,
                        pos,
                    )?;
                    citation.doi = crate::utils::format_doi(&doi);
                }
                b"url" => {
                    let pos = reader.buffer_position() as usize;
                    let url = extract_text_with_position(reader, buf, b"url", content, pos)?;
                    if citation.doi.is_none() && url.contains("doi.org") {
                        citation.doi = crate::utils::format_doi(&url);
                    }
                    citation.urls.push(url);
                }
                b"year" => {
                    let (year_val, month_val, day_val) =
                        extract_date_from_year_element(reader, e, content)?;
                    citation.date = crate::utils::parse_endnote_date(year_val, month_val, day_val);
                }
                b"dates" => {
                    // Handle the dates element - we'll look for year sub-element
                    // This is a more complex structure but we'll process it
                    loop {
                        match reader.read_event_into(buf) {
                            Ok(Event::Start(ref inner_e)) if inner_e.name() == QName(b"year") => {
                                // Parse year element within dates
                                let (year_val, month_val, day_val) =
                                    extract_date_from_year_element(reader, inner_e, content)?;
                                citation.date =
                                    crate::utils::parse_endnote_date(year_val, month_val, day_val);
                            }
                            Ok(Event::End(ref inner_e)) if inner_e.name() == QName(b"dates") => {
                                break;
                            }
                            Ok(Event::Eof) => break,
                            Err(e) => {
                                let pos = reader.buffer_position() as usize;
                                let line = buffer_position_to_line_number(content, pos);
                                return Err(ParseError::at_line(
                                    line,
                                    CitationFormat::EndNoteXml,
                                    ValueError::Syntax(format!("XML parsing error: {}", e)),
                                ));
                            }
                            _ => continue,
                        }
                        buf.clear();
                    }
                }
                b"abstract" => {
                    let pos = reader.buffer_position() as usize;
                    citation.abstract_text = Some(extract_text_with_position(
                        reader,
                        buf,
                        b"abstract",
                        content,
                        pos,
                    )?);
                }
                b"keyword" => {
                    let pos = reader.buffer_position() as usize;
                    citation.keywords.push(extract_text_with_position(
                        reader, buf, b"keyword", content, pos,
                    )?);
                }
                b"language" => {
                    let pos = reader.buffer_position() as usize;
                    citation.language = Some(extract_text_with_position(
                        reader,
                        buf,
                        b"language",
                        content,
                        pos,
                    )?);
                }
                b"publisher" => {
                    let pos = reader.buffer_position() as usize;
                    citation.publisher = Some(extract_text_with_position(
                        reader,
                        buf,
                        b"publisher",
                        content,
                        pos,
                    )?);
                }
                b"isbn" => {
                    let pos = reader.buffer_position() as usize;
                    let issns = extract_text_with_position(reader, buf, b"isbn", content, pos)?;
                    citation.issn.extend(crate::utils::split_issns(&issns));
                }
                _ => (),
            },
            Ok(Event::End(ref e)) if e.name() == QName(b"record") => break,
            Ok(Event::Eof) => break,
            Err(e) => {
                let pos = reader.buffer_position() as usize;
                let line = buffer_position_to_line_number(content, pos);
                return Err(ParseError::at_line(
                    line,
                    CitationFormat::EndNoteXml,
                    ValueError::Syntax(format!("XML parsing error: {}", e)),
                ));
            }
            _ => (),
        }
        buf.clear();
    }

    // Validate that we have at least a title or author
    if citation.title.is_empty() && citation.authors.is_empty() {
        let end_pos = reader.buffer_position() as usize;
        let line_num = buffer_position_to_line_number(content, start_pos);
        return Err(ParseError::at_line(
            line_num,
            CitationFormat::EndNoteXml,
            ValueError::MissingValue {
                field: "title or author",
                key: "title/author",
            },
        )
        .with_span(SourceSpan::new(start_pos, end_pos)));
    }

    Ok(citation)
}