| Crates.io | marc-rs |
| lib.rs | marc-rs |
| version | 0.1.3 |
| created_at | 2026-01-19 15:41:13.122133+00 |
| updated_at | 2026-01-21 06:54:09.619052+00 |
| description | Rust library for MARC21, UNIMARC, and MARC XML format support |
| homepage | |
| repository | https://github.com/jcollonville/marc-rs |
| max_upload_size | |
| id | 2054825 |
| size | 20,616,332 |
A Rust library for parsing and writing MARC21, UNIMARC, and MARC XML bibliographic records.
Add this to your Cargo.toml:
[dependencies]
marc-rs = "0.1.0"
# Optional: Enable Serde support
marc-rs = { version = "0.1.0", features = ["serde"] }
use marc_rs::{parse, FormatEncoding, MarcFormat, Encoding};
let data = b"..."; // MARC binary data
let format_encoding = FormatEncoding::new(MarcFormat::Marc21, Encoding::Marc8);
let records = parse(data, format_encoding)?;
for record in records {
println!("Record with {} fields", record.data_fields.len());
}
use marc_rs::{write, FormatEncoding, Record};
use std::io::stdout;
let records = vec![/* your records */];
let format_encoding = FormatEncoding::marc_xml();
write(&records, format_encoding, &mut stdout())?;
use marc_rs::{MainEntry, Title, Subject, MarcFormat};
// Tags depend on the format
let format = MarcFormat::Marc21;
let main_entry_tag = MainEntry::PersonalName.tag(format); // "100" in MARC21, "700" in UNIMARC
let title_tag = Title::TitleStatement.tag(format); // "245" in MARC21, "200" in UNIMARC
let subject_tag = Subject::SubjectTopicalTerm.tag(format); // "650" in MARC21, "606" in UNIMARC
With the serde feature enabled, you can serialize/deserialize directly to/from MARC formats:
use marc_rs::{Record, FormatEncoding, MarcFormat, Encoding, serde_marc};
use std::fs::File;
// Deserialize from bytes
let data = b"..."; // MARC binary data
let format = FormatEncoding::new(MarcFormat::Marc21, Encoding::Marc8);
let record = serde_marc::from_slice(data, format)?;
// Deserialize from reader
let file = File::open("record.mrc")?;
let record = serde_marc::from_reader(file, format)?;
// Deserialize from string (for XML)
let xml = r#"<?xml version="1.0"?><record>...</record>"#;
let xml_format = FormatEncoding::marc_xml();
let record = serde_marc::from_str(xml, xml_format)?;
// Serialize to bytes
let bytes = serde_marc::to_vec(&record, format)?;
// Serialize to writer
let mut output = Vec::new();
serde_marc::to_writer(&record, format, &mut output)?;
// Serialize to string (for XML)
let xml_string = serde_marc::to_string(&record, xml_format)?;
// Multiple records
let records = serde_marc::from_slice_many(data, format)?;
let bytes = serde_marc::to_records(&records, format)?;
// Or use JSON for cross-format serialization
use serde_json;
let json = serde_json::to_string(&record)?;
let deserialized: Record = serde_json::from_str(&json)?;
Supported encodings:
The library provides enums for different field categories:
The crate includes a command-line viewer tool to inspect MARC files:
# Build the viewer (requires serde feature)
cargo build --bin marc-viewer --features serde
# View a MARC file (auto-detect format, plain output)
cargo run --bin marc-viewer --features serde -- path/to/file.mrc
# Specify format explicitly
cargo run --bin marc-viewer --features serde -- path/to/file.mrc marc21
# Specify format and encoding
cargo run --bin marc-viewer --features serde -- path/to/file.mrc unimarc utf8
# Output in JSON format
cargo run --bin marc-viewer --features serde -- path/to/file.mrc marc21 utf8 json
# Output in XML format
cargo run --bin marc-viewer --features serde -- path/to/file.mrc marc21 utf8 xml
# Output in MARC21 binary format
cargo run --bin marc-viewer --features serde -- path/to/file.mrc marc21 utf8 marc > output.mrc
# Output in UNIMARC binary format
cargo run --bin marc-viewer --features serde -- path/to/file.mrc unimarc utf8 unimarc > output.mrc
The viewer supports five output formats:
The plain format displays:
MIT OR Apache-2.0