Skip to main content

Crate bibtex_parser

Crate bibtex_parser 

Source
Expand description

§bibtex-parser

BibTeX parsing with a Rust Library API.

bibtex-parser supports strict parsing by default, explicit tolerant recovery for malformed input, string and month expansion, comments and preambles, validation, query/edit helpers, and configurable writing.

§Features

  • Borrowed values where possible for low-allocation parsing.
  • String variables, concatenation, and standard month constants.
  • Entries, strings, preambles, comments, and tolerant failures in source order.
  • Opt-in source-span capture.
  • DOI normalization, duplicate detection, validation, sorting, and field normalization.
  • Configurable writer for formatting and file output.
  • Optional parallel feature for parsing multiple files concurrently.
  • Optional latex_to_unicode feature for LaTeX accent conversion helpers.

§Parse

use bibtex_parser::Library;

let input = r#"
    @string{venue = "VLDB"}
    @article{paper,
        author = "Jane Doe and John Smith",
        title = "Example Paper",
        journal = venue,
        year = 2026
    }
"#;

let library = Library::parse(input)?;
let entry = library.find_by_key("paper").unwrap();

assert_eq!(entry.get("journal"), Some("VLDB"));
assert_eq!(entry.year(), Some("2026".to_string()));
assert_eq!(entry.authors().len(), 2);

§Tolerant Recovery

use bibtex_parser::{Block, Library};

let library = Library::parser()
    .tolerant()
    .capture_source()
    .parse(r#"
        @article{ok, title = "Good"}
        @article{bad, title = "Missing close"
        @book{recovered, title = "Recovered"}
    "#)?;

assert_eq!(library.entries().len(), 2);
assert_eq!(library.failed_blocks().len(), 1);

let has_failure_span = library.blocks().iter().any(|block| {
    matches!(block, Block::Failed(failed) if failed.source.is_some())
});
assert!(has_failure_span);

§Write

use bibtex_parser::{Library, Writer, WriterConfig};

let library = Library::parse(r#"@article{paper, title = "Example Paper"}"#)?;
let mut output = Vec::new();
let config = WriterConfig {
    align_values: true,
    ..Default::default()
};

Writer::with_config(&mut output, config).write_library(&library)?;
assert!(String::from_utf8(output).unwrap().contains("@article{paper"));

§Library Versus ParsedDocument

Use Library when application code wants structured bibliography data. Use ParsedDocument when tooling needs source-order blocks, diagnostics, partial results, or source-preserving metadata.

use bibtex_parser::{ParsedBlock, Parser};

let input = r#"
    % retained comment
    @article{paper, title = "Example Paper"}
"#;

let document = Parser::new()
    .capture_source()
    .parse_document(input)?;

assert_eq!(document.library().entries().len(), 1);
assert_eq!(document.entries()[0].key(), "paper");
assert!(matches!(document.blocks()[0], ParsedBlock::Comment(0)));
assert!(document.entries()[0].source.is_some());

Re-exports§

pub use corpus::CorpusEvent;
pub use corpus::CorpusSource;
pub use corpus::DuplicateKeyGroup;
pub use corpus::DuplicateKeyOccurrence;
pub use corpus::ParsedCorpus;
pub use document::Diagnostic;
pub use document::DiagnosticCode;
pub use document::DiagnosticSeverity;
pub use document::DiagnosticTarget;
pub use document::EntryDelimiter;
pub use document::ExpansionOptions;
pub use document::ParseEvent;
pub use document::ParseFlow;
pub use document::ParseStatus;
pub use document::ParseSummary;
pub use document::ParsedBlock;
pub use document::ParsedComment;
pub use document::ParsedDocument;
pub use document::ParsedEntry;
pub use document::ParsedEntryStatus;
pub use document::ParsedFailedBlock;
pub use document::ParsedField;
pub use document::ParsedPreamble;
pub use document::ParsedSource;
pub use document::ParsedString;
pub use document::ParsedValue;
pub use document::StreamingSummary;
pub use document::UnresolvedVariablePolicy;
pub use document::ValueDelimiter;
pub use error::Error;
pub use error::Result;
pub use error::SourceId;
pub use error::SourceSpan;
pub use model::canonical_biblatex_field_alias;
pub use model::classify_resource_field;
pub use model::normalize_biblatex_field_name;
pub use model::normalize_doi;
pub use model::normalize_field_name_ascii;
pub use model::parse_date_parts;
pub use model::parse_names;
pub use model::DateParseError;
pub use model::DateParts;
pub use model::Entry;
pub use model::EntryType;
pub use model::Field;
pub use model::PersonName;
pub use model::ResourceField;
pub use model::ResourceKind;
pub use model::ValidationError;
pub use model::ValidationLevel;
pub use model::ValidationSeverity;
pub use model::Value;
pub use parser::parse_bibtex;
pub use parser::ParsedItem;
pub use source::SourceMap;

Modules§

corpus
Corpus-level parsed bibliography model.
document
Parsed bibliography model with source metadata.
error
Error types for the bibtex-parser crate
latex_unicode
LaTeX to Unicode conversion for common escape sequences
model
Data models for BibTeX entries
parser
BibTeX parser implementation using winnow
prelude
Re-export of common parser functions
source
Source identity and byte-to-line location utilities.

Structs§

Comment
A BibTeX comment block.
FailedBlock
A malformed block retained by tolerant parsing.
FieldNormalizeOptions
Field normalization options.
IssueSummary
Summary of validation issues by severity
Library
A parsed BibTeX library.
LibraryBuilder
Builder for creating libraries programmatically
LibraryStats
Statistics about a library
Parser
Parser configuration.
Preamble
A BibTeX preamble block.
SortOptions
Entry and field ordering options.
StringDefinition
A BibTeX string definition.
ValidationReport
Comprehensive validation report for a library
Writer
BibTeX writer
WriterConfig
Configuration for writing BibTeX

Enums§

Block
A high-level block in a parsed BibTeX library.
FieldNameCase
Field-name casing policy for field normalization.
MonthStyle
Month rendering style used by month normalization.
RawWriteMode
Raw-backed document writing behavior.
TrailingComma
Trailing comma behavior for structured entry writing.

Functions§

document_to_string
Convenience function to write a parsed document to a string.
parse
Parse a BibTeX library from a string.
parse_file
Parse a BibTeX library from a file.
selected_entries_to_string
Convenience function to write selected parsed-document entries to a string.
to_file
Convenience function to write a library to a file.
to_string
Convenience function to write a library to a string.