bibtex-parser 0.3.1

BibTeX parser for Rust
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
//! BibTeX writer for serializing libraries

use crate::{Block, Entry, Library, ParsedBlock, ParsedDocument, ParsedEntry, Result, Value};
use std::borrow::Cow;
use std::io::{self, Write};

/// Configuration for writing BibTeX
#[derive(Debug, Clone)]
pub struct WriterConfig {
    /// Indentation string (default: "  ")
    pub indent: String,
    /// Whether to align field values (default: false)
    pub align_values: bool,
    /// Maximum line length for wrapping (default: 80)
    pub max_line_length: usize,
    /// Whether to sort entries by key (default: false)
    pub sort_entries: bool,
    /// Whether to sort fields within entries (default: false)
    pub sort_fields: bool,
    /// Raw-backed document writing behavior.
    pub raw_write_mode: RawWriteMode,
    /// Trailing comma behavior for structured entry writing.
    pub trailing_comma: TrailingComma,
    /// Separator written between document blocks.
    pub entry_separator: String,
}

/// Raw-backed document writing behavior.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RawWriteMode {
    /// Reuse retained raw text where possible.
    Preserve,
    /// Ignore retained raw text and write normalized structured data.
    Normalize,
}

/// Trailing comma behavior for structured entry writing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrailingComma {
    /// Omit a trailing comma after the final field.
    Omit,
    /// Add a trailing comma after the final field.
    Always,
}

impl Default for WriterConfig {
    fn default() -> Self {
        Self {
            indent: "  ".to_string(),
            align_values: false,
            max_line_length: 80,
            sort_entries: false,
            sort_fields: false,
            raw_write_mode: RawWriteMode::Preserve,
            trailing_comma: TrailingComma::Omit,
            entry_separator: "\n".to_string(),
        }
    }
}

/// BibTeX writer
#[derive(Debug)]
pub struct Writer<W: Write> {
    writer: W,
    config: WriterConfig,
}

impl<W: Write> Writer<W> {
    /// Create a new writer with default configuration
    pub fn new(writer: W) -> Self {
        Self {
            writer,
            config: WriterConfig::default(),
        }
    }

    /// Create a new writer with custom configuration
    pub const fn with_config(writer: W, config: WriterConfig) -> Self {
        Self { writer, config }
    }

    /// Access the writer configuration mutably
    #[must_use]
    pub fn config_mut(&mut self) -> &mut WriterConfig {
        &mut self.config
    }

    /// Consume the writer and return the underlying writer
    #[must_use]
    pub fn into_inner(self) -> W {
        self.writer
    }

    /// Write a complete library.
    pub fn write_library(&mut self, library: &Library) -> io::Result<()> {
        if self.config.sort_entries {
            return self.write_library_sorted(library);
        }

        for (index, block) in library.blocks().into_iter().enumerate() {
            if index > 0 {
                writeln!(self.writer)?;
            }
            match block {
                Block::Entry(entry, _) => self.write_entry(entry)?,
                Block::String(definition) => {
                    self.write_string(&definition.name, &definition.value)?;
                }
                Block::Preamble(preamble) => self.write_preamble(&preamble.value)?,
                Block::Comment(comment) => self.write_comment(comment.text())?,
                Block::Failed(failed) => self.writer.write_all(failed.raw.as_bytes())?,
            }
        }

        Ok(())
    }

    /// Write a parsed document, reusing retained raw blocks when configured.
    pub fn write_document(&mut self, document: &ParsedDocument) -> io::Result<()> {
        self.write_document_with_raw_source(document, None)
    }

    pub(crate) fn write_document_with_raw_source(
        &mut self,
        document: &ParsedDocument,
        raw_source: Option<&str>,
    ) -> io::Result<()> {
        for (index, block) in document.blocks().iter().copied().enumerate() {
            if index > 0 {
                self.writer
                    .write_all(self.config.entry_separator.as_bytes())?;
            }

            match block {
                ParsedBlock::Entry(entry_index) => {
                    self.write_parsed_entry_with_raw_source(
                        &document.entries()[entry_index],
                        raw_source,
                    )?;
                }
                ParsedBlock::String(string_index) => {
                    let string = &document.strings()[string_index];
                    if self.config.raw_write_mode == RawWriteMode::Preserve {
                        if let Some(raw) =
                            raw_text_with_source(string.raw.as_deref(), raw_source, string.source)
                        {
                            self.writer.write_all(raw.as_bytes())?;
                            continue;
                        }
                    }
                    self.write_string(&string.name, &string.value.value)?;
                }
                ParsedBlock::Preamble(preamble_index) => {
                    let preamble = &document.preambles()[preamble_index];
                    if self.config.raw_write_mode == RawWriteMode::Preserve {
                        if let Some(raw) = raw_text_with_source(
                            preamble.raw.as_deref(),
                            raw_source,
                            preamble.source,
                        ) {
                            self.writer.write_all(raw.as_bytes())?;
                            continue;
                        }
                    }
                    self.write_preamble(&preamble.value.value)?;
                }
                ParsedBlock::Comment(comment_index) => {
                    let comment = &document.comments()[comment_index];
                    if self.config.raw_write_mode == RawWriteMode::Preserve {
                        if let Some(raw) =
                            raw_text_with_source(comment.raw.as_deref(), raw_source, comment.source)
                        {
                            self.writer.write_all(raw.as_bytes())?;
                            continue;
                        }
                    }
                    self.write_comment(&comment.text)?;
                }
                ParsedBlock::Failed(failed_index) => {
                    self.writer
                        .write_all(document.failed_blocks()[failed_index].raw.as_bytes())?;
                }
            }
        }

        Ok(())
    }

    /// Write selected parsed-document entries in source order.
    ///
    /// Non-entry blocks are skipped. Duplicate keys in `keys` do not duplicate
    /// output entries.
    pub fn write_selected_entries(
        &mut self,
        document: &ParsedDocument,
        keys: &[&str],
    ) -> io::Result<()> {
        self.write_selected_entries_with_raw_source(document, keys, None)
    }

    pub(crate) fn write_selected_entries_with_raw_source(
        &mut self,
        document: &ParsedDocument,
        keys: &[&str],
        raw_source: Option<&str>,
    ) -> io::Result<()> {
        let mut written = 0usize;
        for block in document.blocks().iter().copied() {
            let ParsedBlock::Entry(entry_index) = block else {
                continue;
            };
            let entry = &document.entries()[entry_index];
            if !keys.iter().any(|key| *key == entry.key()) {
                continue;
            }
            if written > 0 {
                self.writer
                    .write_all(self.config.entry_separator.as_bytes())?;
            }
            self.write_parsed_entry_with_raw_source(entry, raw_source)?;
            written += 1;
        }

        Ok(())
    }

    fn write_library_sorted(&mut self, library: &Library) -> io::Result<()> {
        // Write preambles
        for preamble in library.preambles() {
            self.write_preamble(&preamble.value)?;
            writeln!(self.writer)?;
        }

        // Write strings
        let mut strings: Vec<_> = library.strings().iter().collect();
        if self.config.sort_entries {
            strings.sort_by(|a, b| a.name.cmp(&b.name));
        }

        for definition in strings {
            self.write_string(&definition.name, &definition.value)?;
            writeln!(self.writer)?;
        }

        // Write entries
        let mut entries = library.entries().iter().collect::<Vec<_>>();
        if self.config.sort_entries {
            entries.sort_by(|a, b| a.key.cmp(&b.key));
        }

        for (i, entry) in entries.iter().enumerate() {
            if i > 0 {
                writeln!(self.writer)?;
            }
            self.write_entry(entry)?;
        }

        Ok(())
    }

    /// Write a single entry
    pub fn write_entry(&mut self, entry: &Entry) -> io::Result<()> {
        writeln!(self.writer, "@{}{{{},", entry.ty, entry.key)?;

        let mut fields = entry.fields().to_vec();
        if self.config.sort_fields {
            fields.sort_by(|a, b| a.name.cmp(&b.name));
        }

        // Calculate alignment if needed
        let max_name_len = if self.config.align_values {
            fields.iter().map(|f| f.name.len()).max().unwrap_or(0)
        } else {
            0
        };

        for (i, field) in fields.iter().enumerate() {
            write!(self.writer, "{}", self.config.indent)?;
            write!(self.writer, "{}", field.name)?;

            if self.config.align_values {
                let padding = max_name_len - field.name.len();
                write!(self.writer, "{}", " ".repeat(padding))?;
            }

            write!(self.writer, " = ")?;
            self.write_value(&field.value)?;

            if i < fields.len() - 1 || self.config.trailing_comma == TrailingComma::Always {
                writeln!(self.writer, ",")?;
            } else {
                writeln!(self.writer)?;
            }
        }

        writeln!(self.writer, "}}")?;
        Ok(())
    }

    fn write_parsed_entry_with_raw_source(
        &mut self,
        entry: &ParsedEntry,
        raw_source: Option<&str>,
    ) -> io::Result<()> {
        if self.config.raw_write_mode == RawWriteMode::Preserve {
            if let Some(raw) = patched_entry_raw(entry, raw_source) {
                self.writer.write_all(raw.as_bytes())?;
                return Ok(());
            }
        }

        self.write_entry(&entry.clone().into_entry())
    }

    /// Write a string definition
    fn write_string(&mut self, name: &str, value: &Value) -> io::Result<()> {
        write!(self.writer, "@string{{{name} = ")?;
        self.write_value(value)?;
        writeln!(self.writer, "}}")?;
        Ok(())
    }

    /// Write a preamble
    fn write_preamble(&mut self, value: &Value) -> io::Result<()> {
        write!(self.writer, "@preamble{{")?;
        self.write_value(value)?;
        writeln!(self.writer, "}}")?;
        Ok(())
    }

    /// Write a comment.
    fn write_comment(&mut self, text: &str) -> io::Result<()> {
        let trimmed = text.trim_start();
        if trimmed.starts_with('%') || trimmed.starts_with('@') {
            self.writer.write_all(text.as_bytes())?;
            if !text.ends_with('\n') {
                writeln!(self.writer)?;
            }
        } else {
            writeln!(self.writer, "@comment{{{text}}}")?;
        }
        Ok(())
    }

    /// Write a value
    fn write_value(&mut self, value: &Value) -> io::Result<()> {
        match value {
            Value::Literal(s) => {
                // Quote if contains special characters
                if needs_quoting(s) {
                    write!(self.writer, "\"{}\"", escape_quotes(s))?;
                } else {
                    write!(self.writer, "{{{s}}}")?;
                }
            }
            Value::Number(n) => write!(self.writer, "{n}")?,
            Value::Variable(name) => write!(self.writer, "{name}")?,
            Value::Concat(parts) => {
                for (i, part) in parts.iter().enumerate() {
                    if i > 0 {
                        write!(self.writer, " # ")?;
                    }
                    self.write_value(part)?;
                }
            }
        }
        Ok(())
    }
}

/// Check if a string needs quoting
#[must_use]
fn needs_quoting(s: &str) -> bool {
    s.contains(['{', '}', ',', '='])
}

/// Escape quotes in a string
#[must_use]
fn escape_quotes(s: &str) -> String {
    s.replace('"', "\\\"")
}

fn raw_text_with_source<'a>(
    raw: Option<&'a str>,
    raw_source: Option<&'a str>,
    span: Option<crate::SourceSpan>,
) -> Option<&'a str> {
    raw.or_else(|| source_slice(raw_source, span?))
}

fn source_slice(raw_source: Option<&str>, span: crate::SourceSpan) -> Option<&str> {
    let raw_source = raw_source?;
    raw_source.get(span.byte_start..span.byte_end)
}

fn patched_entry_raw<'entry>(
    entry: &'entry ParsedEntry<'_>,
    raw_source: Option<&'entry str>,
) -> Option<Cow<'entry, str>> {
    let source = entry.source?;
    let raw = raw_text_with_source(entry.raw.as_deref(), raw_source, Some(source))?;
    let mut replacements = Vec::new();

    push_token_replacement(
        &mut replacements,
        raw,
        source.byte_start,
        entry.entry_type_source,
        &entry.ty.to_string(),
        |raw_type| crate::EntryType::parse(raw_type) == entry.ty,
    )?;
    push_token_replacement(
        &mut replacements,
        raw,
        source.byte_start,
        entry.key_source,
        &entry.key,
        |raw_key| raw_key == entry.key,
    )?;

    for field in &entry.fields {
        push_token_replacement(
            &mut replacements,
            raw,
            source.byte_start,
            field.name_source,
            &field.name,
            |raw_name| raw_name == field.name,
        )?;

        if field.value.raw.is_none() {
            let value_source = field.value_source?;
            if source_slice(raw_source, value_source).is_none() {
                let start = value_source.byte_start.checked_sub(source.byte_start)?;
                let end = value_source.byte_end.checked_sub(source.byte_start)?;
                replacements.push((start, end, field.value.value.to_bibtex_source()));
            }
        }
    }

    if replacements.is_empty() {
        return Some(Cow::Borrowed(raw));
    }

    replacements.sort_by_key(|(start, _, _)| *start);
    let mut output = String::with_capacity(raw.len());
    let mut cursor = 0;
    for (start, end, replacement) in replacements {
        if start < cursor || end > raw.len() {
            return None;
        }
        output.push_str(&raw[cursor..start]);
        output.push_str(&replacement);
        cursor = end;
    }
    output.push_str(&raw[cursor..]);
    Some(Cow::Owned(output))
}

fn push_token_replacement(
    replacements: &mut Vec<(usize, usize, String)>,
    raw: &str,
    base: usize,
    span: Option<crate::SourceSpan>,
    replacement: &str,
    unchanged: impl FnOnce(&str) -> bool,
) -> Option<()> {
    let span = span?;
    let start = span.byte_start.checked_sub(base)?;
    let end = span.byte_end.checked_sub(base)?;
    let original = raw.get(start..end)?;
    if !unchanged(original) {
        replacements.push((start, end, replacement.to_string()));
    }
    Some(())
}

/// Convenience function to write a library to a string.
#[must_use = "Check the result to detect serialization errors"]
pub fn to_string(library: &Library) -> Result<String> {
    let mut buf = Vec::new();
    let mut writer = Writer::new(&mut buf);
    writer.write_library(library)?;
    Ok(String::from_utf8(buf).expect("valid UTF-8"))
}

/// Convenience function to write a parsed document to a string.
#[must_use = "Check the result to detect serialization errors"]
pub fn document_to_string(document: &ParsedDocument) -> Result<String> {
    let mut buf = Vec::new();
    let mut writer = Writer::new(&mut buf);
    writer.write_document(document)?;
    Ok(String::from_utf8(buf).expect("valid UTF-8"))
}

/// Convenience function to write selected parsed-document entries to a string.
#[must_use = "Check the result to detect serialization errors"]
pub fn selected_entries_to_string(document: &ParsedDocument, keys: &[&str]) -> Result<String> {
    let mut buf = Vec::new();
    let mut writer = Writer::new(&mut buf);
    writer.write_selected_entries(document, keys)?;
    Ok(String::from_utf8(buf).expect("valid UTF-8"))
}

/// Convenience function to write a library to a file.
#[must_use = "Check the result to detect IO or serialization errors"]
pub fn to_file(library: &Library, path: impl AsRef<std::path::Path>) -> Result<()> {
    let file = std::fs::File::create(path)?;
    let mut writer = Writer::new(file);
    writer.write_library(library)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{EntryType, Field};
    use std::borrow::Cow;

    #[test]
    fn test_write_entry() {
        let entry = Entry {
            ty: EntryType::Article,
            key: Cow::Borrowed("test2023"),
            fields: vec![
                Field::new("author", Value::Literal(Cow::Borrowed("John Doe"))),
                Field::new("title", Value::Literal(Cow::Borrowed("Test Article"))),
                Field::new("year", Value::Number(2023)),
            ],
        };

        let mut buf = Vec::new();
        let mut writer = Writer::new(&mut buf);
        writer.write_entry(&entry).unwrap();

        let result = String::from_utf8(buf).unwrap();
        assert!(result.contains("@article{test2023,"));
        assert!(result.contains("author = {John Doe}"));
        assert!(result.contains("title = {Test Article}"));
        assert!(result.contains("year = 2023"));
    }
}