Extract all keys from a bibfile to store them in var/vec/...
Closed this issue · 2 comments
Hey,
first of all, it is totally possible that I'm missing out on something. If thats the case, I'm sorry ;-)
I'm trying to get only the citekeys of all BibTeX entries. When I parse a file and print its whole content to stdout
there is kind of an indexed array at the end containing all the keys. E.g.:
use std::fs;
use biblatex::{self, Bibliography};
fn main() {
let bibfile = fs::read_to_string("test.bib").unwrap();
let bibliography = Bibliography::parse(&bibfile).unwrap();
println!("{:?}", bibliography);
}
will output this (full entries cut off):
, keys: {"grandsire_the_metafonttutorial_2004": 1, "gruber_markdown": 2, "how_tex_macros_actually_work": 0, "skibinski_automated_jats_xml_to_pdf_conversion_2018": 3}
As far as I can see, there is no struct or implementation to extract all (only) citekeys from the parsed bibfile. Of course, I can extract the keys on my own using some kind of regex pattern matching etc. But I was wondering if such a functionality is missing (and would then pledge for implementing it) or if I just overlooked something very obvious...
Thanks
You can use Bibliography::iter()
to iterate over the entries, and each entry contains its own key as a public key
field. I'm not sure if a separate keys
iterator would be useful as the entries are stored as a vector rather than a hash map or similar, so it'd be equivalent to iter().map(|entry| entry.key)
, but it could be considered.
@PgBiel thanks for the hint. Thats indeed something I missed out on. It works just fine, just had to add .to_owned()
method to get a vector with owned strings:
let bib = Bibliography::parse(&file).unwrap();
let citekeys: Vec<String> = bib.iter().map(|entry| entry.to_owned().key).collect();
That makes my code shorter and more efficient.
I would still vote for implementing this with an own implementation/method whatever, but its definitely not a must.