Skip to content

Compiling Dictionaries

This guide shows how to compile ODXML into .odict binary files programmatically. For CLI usage, see the Quick Start.

The simplest approach is to compile an XML string directly into an in-memory dictionary.

use odict::{OpenDictionary, ToDictionary};
fn main() -> odict::Result<()> {
let xml = r#"
<dictionary name="My Dictionary">
<entry term="hello">
<ety>
<sense pos="intj">
<definition value="A greeting" />
</sense>
</ety>
</entry>
</dictionary>
"#;
// Parse XML → build binary → get OpenDictionary
let dict = xml.to_dictionary()?.build()?;
// Write to disk
dict.to_disk("my-dictionary.odict")?;
Ok(())
}

If your XML lives on disk, read it first and then compile.

use odict::schema::Dictionary;
fn main() -> odict::Result<()> {
// Parse and compile from a file path
let dict = Dictionary::from_path("my-dictionary.xml")?
.build()?;
dict.to_disk("my-dictionary.odict")?;
Ok(())
}

ODict uses Brotli compression. You can configure the compression level when saving.

use odict::{compile::CompilerOptions, CompressOptions, ToDictionary};
fn main() -> odict::Result<()> {
let xml = std::fs::read_to_string("my-dictionary.xml")?;
let compress = CompressOptions::default()
.quality(11) // Maximum compression (0–11)
.window_size(22); // Window size (0–22)
let options = CompilerOptions::default()
.with_compression(compress);
xml.as_str()
.to_dictionary()?
.build()?
.to_disk_with_options("my-dictionary.odict", options)?;
Ok(())
}

Once compiled, you can load .odict files from disk or from the remote registry.

use odict::OpenDictionary;
fn main() -> odict::Result<()> {
// Load from disk
let file = OpenDictionary::from_path("my-dictionary.odict")?;
let dict = file.contents()?;
println!("Entries: {}", dict.entries.len());
Ok(())
}