mdict-rs 0.1.4

Library-first Rust parser for MDict .mdx and .mdd dictionaries
Documentation
use std::path::Path;

use mdict_rs::{KeyOrdinal, MddFile, MdxFile};

const MDX_SAMPLE: &str = "tmp-dict/LDOCE5++/LDOCE5++ V 2-15.mdx";
const MDD_SAMPLE: &str = "tmp-dict/LDOCE5++/LDOCE5++ V 2-15.mdd";

#[test]
#[ignore = "uses large local-only fixture"]
fn mdx_sample_lookup_and_iteration_work() {
    if !Path::new(MDX_SAMPLE).exists() {
        return;
    }

    let file = MdxFile::open(MDX_SAMPLE).unwrap();
    assert_eq!(file.len(), 285_017);

    let apple = file.lookup("Apple").unwrap().unwrap();
    assert_eq!(apple.key, "apple");
    assert!(apple.text.contains("LM5style.css"));

    let first_keys = file
        .keys()
        .take(3)
        .map(|key| key.unwrap())
        .collect::<Vec<_>>();
    assert_eq!(first_keys, vec!["000", "1", "$100/50 a clip"]);

    let ordinal_keys = file
        .keys_with_ordinals()
        .take(3)
        .map(|entry| entry.unwrap())
        .collect::<Vec<_>>();
    assert_eq!(ordinal_keys[0].ordinal, KeyOrdinal::from(0));
    assert_eq!(ordinal_keys[1].ordinal, KeyOrdinal::from(1));
    assert_eq!(ordinal_keys[2].ordinal, KeyOrdinal::from(2));
    assert_eq!(
        ordinal_keys
            .into_iter()
            .map(|entry| entry.key)
            .collect::<Vec<_>>(),
        vec!["000", "1", "$100/50 a clip"]
    );
    assert_eq!(
        file.key_at(KeyOrdinal::from(0)).unwrap().as_deref(),
        Some("000")
    );
    assert_eq!(
        file.key_at(KeyOrdinal::from(2)).unwrap().as_deref(),
        Some("$100/50 a clip")
    );
    assert!(file.key_at(KeyOrdinal::from(file.len())).unwrap().is_none());
    assert_eq!(
        file.keys_at(&[
            KeyOrdinal::from(2),
            KeyOrdinal::from(0),
            KeyOrdinal::from(2),
            KeyOrdinal::from(file.len()),
        ])
        .unwrap(),
        vec![
            Some("$100/50 a clip".to_owned()),
            Some("000".to_owned()),
            Some("$100/50 a clip".to_owned()),
            None,
        ]
    );

    let first_entry = file.entries().next().unwrap().unwrap();
    assert_eq!(first_entry.key, "000");
    assert!(!first_entry.text.is_empty());
}

#[test]
#[ignore = "uses large local-only fixture"]
fn mdd_sample_lookup_and_iteration_work() {
    if !Path::new(MDD_SAMPLE).exists() {
        return;
    }

    let file = MddFile::open(MDD_SAMPLE).unwrap();
    assert_eq!(file.len(), 183_926);

    let css = file.lookup("\\lm5style.css").unwrap().unwrap();
    assert_eq!(css.key, "\\LM5style.css");
    assert!(css.data.starts_with(b"@charset \"utf-8\""));

    let first_keys = file
        .keys()
        .take(3)
        .map(|key| key.unwrap())
        .collect::<Vec<_>>();
    assert_eq!(
        first_keys,
        vec![
            "\\jquery-3.2.1.min.js",
            "\\LM5style.css",
            "\\LM5style_show.css"
        ]
    );

    let ordinal_keys = file
        .keys_with_ordinals()
        .take(3)
        .map(|entry| entry.unwrap())
        .collect::<Vec<_>>();
    assert_eq!(ordinal_keys[0].ordinal, KeyOrdinal::from(0));
    assert_eq!(ordinal_keys[1].ordinal, KeyOrdinal::from(1));
    assert_eq!(ordinal_keys[2].ordinal, KeyOrdinal::from(2));
    assert_eq!(
        ordinal_keys
            .into_iter()
            .map(|entry| entry.key)
            .collect::<Vec<_>>(),
        vec![
            "\\jquery-3.2.1.min.js",
            "\\LM5style.css",
            "\\LM5style_show.css"
        ]
    );
    assert_eq!(
        file.key_at(KeyOrdinal::from(0)).unwrap().as_deref(),
        Some("\\jquery-3.2.1.min.js")
    );
    assert_eq!(
        file.key_at(KeyOrdinal::from(2)).unwrap().as_deref(),
        Some("\\LM5style_show.css")
    );
    assert!(file.key_at(KeyOrdinal::from(file.len())).unwrap().is_none());
    assert_eq!(
        file.keys_at(&[
            KeyOrdinal::from(1),
            KeyOrdinal::from(0),
            KeyOrdinal::from(1),
            KeyOrdinal::from(file.len()),
        ])
        .unwrap(),
        vec![
            Some("\\LM5style.css".to_owned()),
            Some("\\jquery-3.2.1.min.js".to_owned()),
            Some("\\LM5style.css".to_owned()),
            None,
        ]
    );

    let first_entry = file.entries().next().unwrap().unwrap();
    assert_eq!(first_entry.key, "\\jquery-3.2.1.min.js");
    assert!(!first_entry.data.is_empty());
}