grobid 0.6.0

Native Rust client for the GROBID REST API with a strongly typed TEI parser
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
//! Strongly typed structures for GROBID TEI documents.
//!
//! The types in this module mirror the content model of the TEI XML that
//! GROBID produces (see the GROBID documentation at
//! <https://grobid.readthedocs.io/>). All types are plain data containers
//! with `serde` support, so a parsed [`Document`] can be serialized to JSON
//! or any other `serde`-compatible format.

use serde::{Deserialize, Serialize};

/// A rectangular region on a PDF page.
///
/// GROBID uses the native PDF coordinate system: the origin is at the
/// *upper left* corner of a page, the x-axis extends to the right and the
/// y-axis extends *downward*. Values are in abstract PDF units, and the
/// first page has index 1.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct BoundingBox {
    /// Page number, starting at 1.
    pub page: u32,
    /// X coordinate of the upper-left corner.
    pub x: f64,
    /// Y coordinate of the upper-left corner.
    pub y: f64,
    /// Width of the box.
    pub width: f64,
    /// Height of the box.
    pub height: f64,
}

/// The coordinates of a TEI structure in the original PDF.
///
/// A structure may span several bounding boxes, e.g. when a reference is
/// broken over several lines. Coordinates are only present when the
/// `teiCoordinates` parameter was requested from GROBID.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct Coords {
    /// The bounding boxes that make up the structure's area.
    pub boxes: Vec<BoundingBox>,
}

impl Coords {
    /// Parse a GROBID `@coords` attribute value.
    ///
    /// The compact notation consists of bounding boxes separated by `;`,
    /// each box being `page,x,y,width,height`:
    ///
    /// ```
    /// use grobid::Coords;
    ///
    /// let coords = Coords::parse("1,53.80,194.57,58.71,9.29")?;
    /// assert_eq!(coords.boxes.len(), 1);
    /// assert_eq!(coords.boxes[0].page, 1);
    /// assert_eq!(coords.boxes[0].x, 53.80);
    ///
    /// // A structure can span several boxes, e.g. across multiple lines.
    /// let coords = Coords::parse("10,317.03,183.61,223.16,7.55;10,317.03,192.57,223.21,7.55")?;
    /// assert_eq!(coords.boxes.len(), 2);
    /// # Ok::<(), grobid::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`crate::Error::InvalidCoords`] when a box does not consist of
    /// exactly five comma-separated numbers.
    pub fn parse(value: &str) -> Result<Self, crate::Error> {
        let mut boxes = Vec::new();
        for part in value.split(';') {
            let fields: Vec<&str> = part.split(',').map(str::trim).collect();
            let [page, x, y, width, height] = fields[..] else {
                return Err(crate::Error::InvalidCoords {
                    value: value.to_string(),
                });
            };
            let parse = |s: &str| {
                s.parse::<f64>().map_err(|_| crate::Error::InvalidCoords {
                    value: value.to_string(),
                })
            };
            let page = page
                .parse::<u32>()
                .map_err(|_| crate::Error::InvalidCoords {
                    value: value.to_string(),
                })?;
            boxes.push(BoundingBox {
                page,
                x: parse(x)?,
                y: parse(y)?,
                width: parse(width)?,
                height: parse(height)?,
            });
        }
        Ok(Coords { boxes })
    }

    /// Returns true if there are no bounding boxes.
    pub fn is_empty(&self) -> bool {
        self.boxes.is_empty()
    }
}

/// The dimension of a page, taken from the `<facsimile>` section of a TEI
/// document. Page dimensions are included when coordinates were requested.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Surface {
    /// Page number, starting at 1.
    pub page: u32,
    /// Page width in PDF units.
    pub width: f64,
    /// Page height in PDF units.
    pub height: f64,
}

/// A person (author or editor), parsed from a TEI `<persName>` element.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Author {
    /// The full name, as it appears in the document.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub full_name: Option<String>,
    /// First name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub given_name: Option<String>,
    /// Middle name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub middle_name: Option<String>,
    /// Last name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub surname: Option<String>,
    /// Email address, when present in the header.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    /// ORCID identifier, when present in the header.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub orcid: Option<String>,
    /// Affiliation, when present in the header.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub affiliation: Option<Affiliation>,
    /// Coordinates in the original PDF, when requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coords: Option<Coords>,
}

impl Author {
    /// Returns true if nothing is known about this author.
    ///
    /// ```
    /// use grobid::Author;
    ///
    /// assert!(Author::default().is_empty());
    ///
    /// let author = Author {
    ///     given_name: Some("Brewster".to_string()),
    ///     surname: Some("Kahle".to_string()),
    ///     ..Author::default()
    /// };
    /// assert!(!author.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.full_name.is_none()
            && self.given_name.is_none()
            && self.middle_name.is_none()
            && self.surname.is_none()
            && self.email.is_none()
            && self.orcid.is_none()
            && self.affiliation.as_ref().is_none_or(Affiliation::is_empty)
    }
}

/// An author affiliation, parsed from a TEI `<affiliation>` element.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Affiliation {
    /// Institution name (`<orgName type="institution">`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub institution: Option<String>,
    /// Department name (`<orgName type="department">`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub department: Option<String>,
    /// Laboratory name (`<orgName type="laboratory">`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub laboratory: Option<String>,
    /// Postal address, when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub address: Option<Address>,
    /// Coordinates in the original PDF, when requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coords: Option<Coords>,
}

impl Affiliation {
    /// Returns true if nothing is known about this affiliation.
    pub fn is_empty(&self) -> bool {
        self.institution.is_none()
            && self.department.is_none()
            && self.laboratory.is_none()
            && self.address.is_none()
    }
}

/// A postal address, parsed from a TEI `<address>` element.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Address {
    /// First address line.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub addr_line: Option<String>,
    /// Postal code.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub post_code: Option<String>,
    /// City.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub settlement: Option<String>,
    /// Country.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub country: Option<String>,
}

/// A complete bibliographic record.
///
/// This is used both for the document header metadata ([`Header`]) and for
/// every parsed reference ([`Citation`]). Field semantics follow the GROBID
/// TEI encoding; see also the Go client (`github.com/schmettow/grobidclient`)
/// which this parser is modelled after.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Biblio {
    /// The authors of the work.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub authors: Vec<Author>,
    /// The editors of the work.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub editors: Vec<Author>,
    /// The `xml:id` of the `<biblStruct>` element (e.g. `b12`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// The original, unstructured reference string
    /// (`<note type="raw_reference">`), only present when
    /// `includeRawCitations` was requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub raw_reference: Option<String>,
    /// Publication date (`date/@when`), usually ISO-8601.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub date: Option<String>,
    /// Title of the work (article title).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Title of the containing book, when different from the article title.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub book_title: Option<String>,
    /// Title of the containing series.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub series_title: Option<String>,
    /// Journal title.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub journal: Option<String>,
    /// Journal abbreviation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub journal_abbrev: Option<String>,
    /// Publisher.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub publisher: Option<String>,
    /// Issuing institution (e.g. for reports and theses).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub institution: Option<String>,
    /// ISSN.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub issn: Option<String>,
    /// Electronic ISSN.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub eissn: Option<String>,
    /// Volume.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub volume: Option<String>,
    /// Issue.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub issue: Option<String>,
    /// Page range, e.g. `235-243`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pages: Option<String>,
    /// First page.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub first_page: Option<String>,
    /// Last page.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_page: Option<String>,
    /// A free-form note attached to the reference.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
    /// DOI.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub doi: Option<String>,
    /// PubMed ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pmid: Option<String>,
    /// PubMed Central ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pmcid: Option<String>,
    /// arXiv identifier, without the `arXiv:` prefix.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arxiv_id: Option<String>,
    /// PII identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pii: Option<String>,
    /// ARK identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ark: Option<String>,
    /// ISTEX identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub istex_id: Option<String>,
    /// URL, usually from a `<ptr target="...">` element.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    /// Coordinates in the original PDF, when requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coords: Option<Coords>,
}

impl Biblio {
    /// Returns true if the record contains no meaningful information.
    ///
    /// Mirrors `IsEmpty` of the Go GROBID client: a record is considered
    /// empty when it has neither authors nor editors and none of the main
    /// bibliographic fields are set. Note that a `raw_reference` alone does
    /// not make a record non-empty.
    ///
    /// ```
    /// use grobid::Biblio;
    ///
    /// assert!(Biblio::default().is_empty());
    ///
    /// // A single identifier, e.g. from an unparsed reference, is enough
    /// // to make the record non-empty.
    /// let biblio = Biblio {
    ///     doi: Some("10.1234/example".to_string()),
    ///     ..Biblio::default()
    /// };
    /// assert!(!biblio.is_empty());
    ///
    /// // A raw reference string alone does not.
    /// let biblio = Biblio {
    ///     raw_reference: Some("Smith 2001".to_string()),
    ///     ..Biblio::default()
    /// };
    /// assert!(biblio.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        if !self.authors.is_empty() || !self.editors.is_empty() {
            return false;
        }
        !any_nonempty(&[
            self.date.as_deref(),
            self.title.as_deref(),
            self.journal.as_deref(),
            self.publisher.as_deref(),
            self.volume.as_deref(),
            self.issue.as_deref(),
            self.pages.as_deref(),
            self.doi.as_deref(),
            self.pmid.as_deref(),
            self.pmcid.as_deref(),
            self.arxiv_id.as_deref(),
            self.url.as_deref(),
        ])
    }
}

/// Returns true if any of the given strings is non-empty.
fn any_nonempty(values: &[Option<&str>]) -> bool {
    values.iter().any(|v| v.is_some_and(|s| !s.is_empty()))
}

/// The bibliographic metadata of a processed document (its TEI header).
///
/// `Header` is a thin wrapper around [`Biblio`]; it dereferences to the
/// underlying record, so all bibliographic fields can be accessed directly,
/// e.g. `document.header.title`.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Header {
    /// The header's bibliographic metadata.
    #[serde(flatten)]
    pub biblio: Biblio,
}

impl std::ops::Deref for Header {
    type Target = Biblio;
    fn deref(&self) -> &Biblio {
        &self.biblio
    }
}

impl std::ops::DerefMut for Header {
    fn deref_mut(&mut self) -> &mut Biblio {
        &mut self.biblio
    }
}

/// A single bibliographic reference (one `<biblStruct>` element), e.g. from
/// the reference list of a processed document.
///
/// `Citation` dereferences to its [`Biblio`] record, so all bibliographic
/// fields can be accessed directly, e.g. `citation.title`.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Citation {
    /// Zero-based position of the reference within its list.
    pub index: usize,
    /// The bibliographic record.
    #[serde(flatten)]
    pub biblio: Biblio,
}

impl std::ops::Deref for Citation {
    type Target = Biblio;
    fn deref(&self) -> &Biblio {
        &self.biblio
    }
}

impl std::ops::DerefMut for Citation {
    fn deref_mut(&mut self) -> &mut Biblio {
        &mut self.biblio
    }
}

/// Kind of an in-text reference marker (`<ref type="...">`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RefKind {
    /// Reference to a bibliographic entry (`type="bibr"`).
    #[serde(rename = "bibr")]
    Biblio,
    /// Reference to a figure (`type="figure"`).
    #[serde(rename = "figure")]
    Figure,
    /// Reference to a table (`type="table"`).
    #[serde(rename = "table")]
    Table,
    /// Reference to a formula (`type="formula"`).
    #[serde(rename = "formula")]
    Formula,
    /// Any other reference kind.
    #[serde(untagged)]
    Other(String),
}

impl Default for RefKind {
    fn default() -> Self {
        RefKind::Other(String::new())
    }
}

impl RefKind {
    /// Map a TEI `ref/@type` value onto a [`RefKind`].
    pub(crate) fn from_tei(value: Option<&str>) -> Self {
        match value {
            Some("bibr") => RefKind::Biblio,
            Some("figure") => RefKind::Figure,
            Some("table") => RefKind::Table,
            Some("formula") => RefKind::Formula,
            other => RefKind::Other(other.unwrap_or_default().to_string()),
        }
    }
}

/// An in-text reference marker, e.g. `(Smith et al., 2019)` or `(Fig. 1)`,
/// parsed from a TEI `<ref>` element.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct MarkerRef {
    /// The kind of referenced object.
    pub kind: RefKind,
    /// The target of the reference, e.g. `#b0` or `#fig_1`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target: Option<String>,
    /// The visible label of the marker, e.g. `(1)`.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub label: String,
}

/// A sentence within a paragraph. Only present when GROBID was asked to
/// segment sentences (`segmentSentences=1`).
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Sentence {
    /// The sentence text.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub text: String,
    /// Coordinates in the original PDF, when requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coords: Option<Coords>,
}

/// A paragraph (`<p>` element) of a document section.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Paragraph {
    /// The `xml:id`, only present when `generateIDs=1` was requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// The paragraph text, including the labels of inline reference
    /// markers.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub text: String,
    /// Sentence segmentation, only present when `segmentSentences=1` was
    /// requested.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub sentences: Vec<Sentence>,
    /// In-text reference markers (bibliographic, figure, table, formula).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub markers: Vec<MarkerRef>,
    /// Coordinates in the original PDF, when requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coords: Option<Coords>,
}

/// A figure or table (`<figure>` element) of a document section.
///
/// GROBID encodes tables as `<figure type="table">` elements.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Figure {
    /// The `xml:id`, e.g. `fig_1`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// The figure type, `table` for tables.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub figure_type: Option<String>,
    /// The figure head, e.g. `Fig. 1 .`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub head: Option<String>,
    /// The figure label, e.g. `1`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// The figure caption (`<figDesc>`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub caption: Option<String>,
    /// Coordinates in the original PDF, when requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coords: Option<Coords>,
}

/// A mathematical formula (`<formula>` element) of a document section.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Formula {
    /// The `xml:id`, only present when `generateIDs=1` was requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// The textual content of the formula.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub text: String,
    /// Coordinates in the original PDF, when requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coords: Option<Coords>,
}

/// A footnote (`<note>` element) of a document section.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Note {
    /// The `place` attribute, e.g. `foot`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub place: Option<String>,
    /// The note text.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub text: String,
    /// Coordinates in the original PDF, when requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coords: Option<Coords>,
}

/// A list (`<list>` element) of a document section.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct List {
    /// The `type` attribute, when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub list_type: Option<String>,
    /// The list items.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub items: Vec<String>,
}

/// An ordered content block of a [`Div`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Block {
    /// A paragraph.
    Paragraph(Paragraph),
    /// A figure or table.
    Figure(Figure),
    /// A mathematical formula.
    Formula(Formula),
    /// A footnote.
    Note(Note),
    /// A list.
    List(List),
    /// A nested subsection.
    Div(Box<Div>),
}

/// A document section (`<div>` element), holding its content in document
/// order. Sections can be nested arbitrarily deep, which reflects the full
/// heading hierarchy of the source document.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Div {
    /// The `type` attribute, e.g. `acknowledgement` or `references` for
    /// back matter sections.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub div_type: Option<String>,
    /// The section heading (`<head>`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub head: Option<String>,
    /// The section number (`head/@n`), e.g. `2.1`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub number: Option<String>,
    /// The section content, in document order.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub blocks: Vec<Block>,
    /// The `xml:id`, only present when `generateIDs=1` was requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Coordinates in the original PDF, when requested.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coords: Option<Coords>,
}

impl Div {
    /// All paragraphs of this section, including paragraphs of nested
    /// subsections, in document order.
    ///
    /// ```
    /// use grobid::tei::parse_document;
    ///
    /// let xml = r#"<TEI xmlns="http://www.tei-c.org/ns/1.0">
    ///   <teiHeader><encodingDesc><appInfo>
    ///     <application version="0.9" when="2024-01-01T00:00+0000"/>
    ///   </appInfo></encodingDesc>
    ///   <fileDesc><titleStmt><title level="a" type="main">Example</title></titleStmt>
    ///     <publicationStmt><publisher/></publicationStmt>
    ///     <sourceDesc><biblStruct/></sourceDesc></fileDesc></teiHeader>
    ///   <text><body><div>
    ///     <head>Results</head>
    ///     <p>First paragraph.</p>
    ///     <div><head>Subsection</head><p>Nested paragraph.</p></div>
    ///   </div></body></text>
    /// </TEI>"#;
    ///
    /// let document = parse_document(xml)?;
    /// let section = &document.body[0];
    /// let paragraphs: Vec<&str> = section
    ///     .paragraphs()
    ///     .iter()
    ///     .map(|p| p.text.as_str())
    ///     .collect();
    /// assert_eq!(paragraphs, ["First paragraph.", "Nested paragraph."]);
    /// # Ok::<(), grobid::Error>(())
    /// ```
    pub fn paragraphs(&self) -> Vec<&Paragraph> {
        fn collect<'a>(div: &'a Div, out: &mut Vec<&'a Paragraph>) {
            for block in &div.blocks {
                match block {
                    Block::Paragraph(p) => out.push(p),
                    Block::Div(sub) => collect(sub, out),
                    _ => {}
                }
            }
        }
        let mut out = Vec::new();
        collect(self, &mut out);
        out
    }

    /// All figures and tables of this section, including nested
    /// subsections, in document order.
    ///
    /// ```
    /// use grobid::tei::parse_document;
    ///
    /// let xml = r#"<TEI xmlns="http://www.tei-c.org/ns/1.0">
    ///   <teiHeader><encodingDesc><appInfo>
    ///     <application version="0.9" when="2024-01-01T00:00+0000"/>
    ///   </appInfo></encodingDesc>
    ///   <fileDesc><titleStmt><title level="a" type="main">Example</title></titleStmt>
    ///     <publicationStmt><publisher/></publicationStmt>
    ///     <sourceDesc><biblStruct/></sourceDesc></fileDesc></teiHeader>
    ///   <text><body><div>
    ///     <head>Results</head>
    ///     <figure xml:id="fig_1"><head>Fig. 1</head><figDesc>A caption</figDesc></figure>
    ///     <figure type="table" xml:id="tab_1"><head>Table 1</head></figure>
    ///   </div></body></text>
    /// </TEI>"#;
    ///
    /// let document = parse_document(xml)?;
    /// let figures = document.body[0].figures();
    /// assert_eq!(figures.len(), 2);
    /// assert_eq!(figures[0].caption.as_deref(), Some("A caption"));
    /// // GROBID encodes tables as figures with type="table".
    /// assert_eq!(figures[1].figure_type.as_deref(), Some("table"));
    /// # Ok::<(), grobid::Error>(())
    /// ```
    pub fn figures(&self) -> Vec<&Figure> {
        fn collect<'a>(div: &'a Div, out: &mut Vec<&'a Figure>) {
            for block in &div.blocks {
                match block {
                    Block::Figure(f) => out.push(f),
                    Block::Div(sub) => collect(sub, out),
                    _ => {}
                }
            }
        }
        let mut out = Vec::new();
        collect(self, &mut out);
        out
    }

    /// All nested subsections of this section (one level deep).
    pub fn subsections(&self) -> impl Iterator<Item = &Div> {
        self.blocks.iter().filter_map(|block| match block {
            Block::Div(div) => Some(div.as_ref()),
            _ => None,
        })
    }
}

/// A parsed GROBID TEI document, e.g. the response of
/// `processFulltextDocument` or `processHeaderDocument`.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Document {
    /// The GROBID version that produced the document
    /// (`application/@version`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub grobid_version: Option<String>,
    /// The processing timestamp (`application/@when`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub grobid_timestamp: Option<String>,
    /// The MD5 checksum of the source PDF, when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pdf_md5: Option<String>,
    /// The language of the document (`text/@xml:lang`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    /// The document's bibliographic metadata.
    pub header: Header,
    /// The abstract, when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub abstract_text: Option<String>,
    /// The document body as a hierarchy of sections.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub body: Vec<Div>,
    /// Footnotes that GROBID emitted directly under `<body>` (outside any
    /// section).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub body_notes: Vec<Note>,
    /// The acknowledgement section, when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub acknowledgements: Option<String>,
    /// The annex section, when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub annexes: Option<String>,
    /// The parsed bibliographic references.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub citations: Vec<Citation>,
    /// Page dimensions of the source PDF, present when coordinates were
    /// requested.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub facsimile: Vec<Surface>,
}

impl Document {
    /// The full body text as a single string: the text of all body
    /// elements (headings, paragraphs, markers, captions, footnotes) joined
    /// with single spaces.
    ///
    /// ```
    /// use grobid::tei::parse_document;
    ///
    /// let xml = r##"<TEI xmlns="http://www.tei-c.org/ns/1.0">
    ///   <teiHeader><encodingDesc><appInfo>
    ///     <application version="0.9" when="2024-01-01T00:00+0000"/>
    ///   </appInfo></encodingDesc>
    ///   <fileDesc><titleStmt><title level="a" type="main">Example</title></titleStmt>
    ///     <publicationStmt><publisher/></publicationStmt>
    ///     <sourceDesc><biblStruct/></sourceDesc></fileDesc></teiHeader>
    ///   <text><body>
    ///     <div><head>Introduction</head><p>Hello <ref type="bibr" target="#b0">[1]</ref>.</p></div>
    ///   </body></text>
    /// </TEI>"##;
    ///
    /// let document = parse_document(xml)?;
    /// assert_eq!(document.body_text(), "Introduction Hello [1] .");
    /// # Ok::<(), grobid::Error>(())
    /// ```
    pub fn body_text(&self) -> String {
        let mut parts = Vec::new();
        for div in &self.body {
            collect_div_text(div, &mut parts);
        }
        for note in &self.body_notes {
            parts.push(note.text.clone());
        }
        parts.join(" ")
    }

    /// Look up a citation by its `xml:id` (e.g. `b12`, or `#b12` with the
    /// leading `#` of an in-text reference target).
    ///
    /// ```
    /// use grobid::tei::parse_document;
    ///
    /// let xml = r#"<TEI xmlns="http://www.tei-c.org/ns/1.0">
    ///   <teiHeader><encodingDesc><appInfo>
    ///     <application version="0.9" when="2024-01-01T00:00+0000"/>
    ///   </appInfo></encodingDesc>
    ///   <fileDesc><titleStmt><title level="a" type="main">Example</title></titleStmt>
    ///     <publicationStmt><publisher/></publicationStmt>
    ///     <sourceDesc><biblStruct/></sourceDesc></fileDesc></teiHeader>
    ///   <text><back><div type="references"><listBibl>
    ///     <biblStruct xml:id="b0">
    ///       <monogr><title level="j">A journal</title></monogr>
    ///     </biblStruct>
    ///   </listBibl></div></back></text>
    /// </TEI>"#;
    ///
    /// let document = parse_document(xml)?;
    /// let citation = document.find_citation("#b0").expect("citation b0");
    /// assert_eq!(citation.journal.as_deref(), Some("A journal"));
    /// assert!(document.find_citation("b1").is_none());
    /// # Ok::<(), grobid::Error>(())
    /// ```
    pub fn find_citation(&self, id: &str) -> Option<&Citation> {
        let id = id.strip_prefix('#').unwrap_or(id);
        self.citations.iter().find(|c| c.id.as_deref() == Some(id))
    }
}

/// Collect all text fragments of a section, in document order.
fn collect_div_text(div: &Div, out: &mut Vec<String>) {
    if let Some(head) = &div.head {
        out.push(head.clone());
    }
    for block in &div.blocks {
        match block {
            Block::Paragraph(p) => out.push(p.text.clone()),
            Block::Figure(f) => {
                if let Some(head) = &f.head {
                    out.push(head.clone());
                }
                if let Some(caption) = &f.caption {
                    out.push(caption.clone());
                }
            }
            Block::Formula(f) => out.push(f.text.clone()),
            Block::Note(n) => out.push(n.text.clone()),
            Block::List(list) => out.extend(list.items.iter().cloned()),
            Block::Div(div) => collect_div_text(div, out),
        }
    }
}