Skip to content

Commit 7157441

Browse files
committed
Insert newlines between entries in string dump
Closes #19
1 parent 5075220 commit 7157441

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

bench/src/bench.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ fn benchmarks(c: &mut Criterion) {
1212
};
1313
}
1414

15-
let bib = Bibliography::parse(GRAL);
15+
let bib = Bibliography::parse(GRAL).unwrap();
1616
let entry = bib.get("kim2009").unwrap();
1717

18-
bench!("parse-gral": Bibliography::parse(GRAL));
18+
bench!("parse-gral": Bibliography::parse(GRAL).unwrap());
1919
bench!("get-last-gral": bib.get("adedeji2017"));
2020
bench!("get-author-gral": entry.author());
2121

22-
let bib = Bibliography::parse(CROSS);
22+
let bib = Bibliography::parse(CROSS).unwrap();
2323
let entry = bib.get("issue201").unwrap();
2424
bench!("get-date-cross": entry.date());
2525
}

src/lib.rs

+24-21
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ pub use types::*;
3333
use std::collections::{BTreeMap, HashMap};
3434
use std::error::Error;
3535
use std::fmt;
36-
use std::fmt::{Display, Formatter, Write as _};
37-
use std::io::{self, Write};
36+
use std::fmt::{Display, Formatter, Write};
3837

3938
use mechanics::{AuthorMode, PagesChapterMode};
4039

@@ -196,41 +195,45 @@ impl Bibliography {
196195
self.entries.iter_mut()
197196
}
198197

198+
/// Write the entry into a writer in the BibLaTeX format.
199+
pub fn write_biblatex(&self, mut sink: impl Write) -> fmt::Result {
200+
let mut first = true;
201+
for entry in &self.entries {
202+
if !first {
203+
write!(sink, "\n")?;
204+
}
205+
writeln!(sink, "{}", entry.to_biblatex_string())?;
206+
first = false;
207+
}
208+
Ok(())
209+
}
210+
199211
/// Serialize this bibliography into a BibLaTeX string.
200212
pub fn to_biblatex_string(&self) -> String {
201213
let mut biblatex = String::new();
202-
for entry in &self.entries {
203-
biblatex.push_str(&entry.to_biblatex_string());
204-
biblatex.push('\n');
205-
}
214+
self.write_biblatex(&mut biblatex).unwrap();
206215
biblatex
207216
}
208217

209-
/// Write the entry into a writer in the BibLaTeX format.
210-
pub fn write_biblatex(&self, mut sink: impl Write) -> io::Result<()> {
218+
/// Write the entry into a writer in the BibTeX format.
219+
pub fn write_bibtex(&self, mut sink: impl Write) -> fmt::Result {
220+
let mut first = true;
211221
for entry in &self.entries {
212-
writeln!(sink, "{}", entry.to_biblatex_string())?;
222+
if !first {
223+
write!(sink, "\n")?;
224+
}
225+
writeln!(sink, "{}", entry.to_bibtex_string())?;
226+
first = false;
213227
}
214228
Ok(())
215229
}
216230

217231
/// Serialize this bibliography into a BibTeX string.
218232
pub fn to_bibtex_string(&self) -> String {
219233
let mut bibtex = String::new();
220-
for entry in &self.entries {
221-
bibtex.push_str(&entry.to_bibtex_string());
222-
bibtex.push('\n');
223-
}
234+
self.write_bibtex(&mut bibtex).unwrap();
224235
bibtex
225236
}
226-
227-
/// Write the entry into a writer in the BibTeX format.
228-
pub fn write_bibtex(&self, mut sink: impl Write) -> io::Result<()> {
229-
for entry in &self.entries {
230-
writeln!(sink, "{}", entry.to_bibtex_string())?;
231-
}
232-
Ok(())
233-
}
234237
}
235238

236239
impl IntoIterator for Bibliography {

src/mechanics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub enum PagesChapterMode {
107107
/// `chapter` field.
108108
PagesOptional,
109109
/// The `pages` field must be present.
110+
#[allow(dead_code)]
110111
PagesRequired,
111112
}
112113

0 commit comments

Comments
 (0)