Crate ged_io

Source
Expand description

ged_io is a Rust crate for parsing GEDCOM formatted text.

The library works with GEDCOM (Genealogical Data Communication), a text-based format widely supported by genealogy software for storing and exchanging family history data. ged_io transforms this text format into workable Rust data structures.

Basic example:

use ged_io::Gedcom;
use std::error::Error;
use std::fs;

// Parse a GEDCOM file
fn main() -> Result<(), Box<dyn Error>> {
    let source = fs::read_to_string("./tests/fixtures/sample.ged")?;
    let mut gedcom = Gedcom::new(source.chars())?;
    let gedcom_data = gedcom.parse_data()?;

    // Display file statistics
    gedcom_data.stats();
    Ok(())
}

This crate contains an optional "json" feature that implements serialization and deserialization to JSON with serde.

To enable JSON support, add the feature to your Cargo.toml:

[dependencies]
ged_io = { version = "0.2.1", features = ["json"] }

JSON serialization example:

use ged_io::Gedcom;
use std::error::Error;

#[cfg(feature = "json")]
fn serialize_to_json() -> Result<(), Box<dyn Error>> {
    let source = "0 HEAD\n1 GEDC\n2 VERS 5.5\n0 @I1@ INDI\n1 NAME John /Doe/\n0 TRLR";
    let mut gedcom = Gedcom::new(source.chars())?;
    let gedcom_data = gedcom.parse_data()?;

    let json_output = serde_json::to_string_pretty(&gedcom_data)?;
    println!("GEDCOM as JSON:\n{}", json_output);

    Ok(())
}

// This function only exists if the "json" feature is enabled in Cargo.toml

§Error Handling Example

This example demonstrates how to handle GedcomError when parsing a malformed GEDCOM string.

use ged_io::Gedcom;
use ged_io::GedcomError;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let malformed_gedcom = "0 HEAD\n1 GEDC\n2 VERS 5.5\n1 INVALID_TAG\n0 TRLR";
    let mut gedcom = Gedcom::new(malformed_gedcom.chars())?;

    match gedcom.parse_data() {
        Ok(_) => println!("Parsing successful!"),
        Err(e) => {
            eprintln!("Error parsing GEDCOM: {}", e);
            match e {
                GedcomError::ParseError { line, message } => {
                    eprintln!("Specific Parse Error at line {}: {}", line, message);
                }
                GedcomError::InvalidFormat(msg) => {
                    eprintln!("Specific Invalid Format Error: {}", msg);
                }
                GedcomError::EncodingError(msg) => {
                    eprintln!("Specific Encoding Error: {}", msg);
                }
            }
        }
    }
    Ok(())
}

Re-exports§

pub use error::GedcomError;

Modules§

error
Error types for the ged_io crate.
parser
Shared parsing utilities and traits for GEDCOM records.
tokenizer
Processes character streams into tokens.
types
Data structures representing the parsed contents of a GEDCOM file.

Macros§

fmt_optional_value
Macro for displaying Options in debug mode without the text wrapping.

Structs§

Gedcom
The main interface for parsing GEDCOM files into structured Rust data types.