wodix 0.1.0

A Rust library for reading and searching MDict MDX/MDD dictionaries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Read-only composition of a source, its derived index and optional resources.

use std::collections::HashSet;
use std::path::Path;

use memmap2::Mmap;

use crate::index::{Cache, Index, Lookup, Query};
use crate::link::{Link, Trace};
use crate::mdd::Resources;
use crate::reader::{Read, Reader};
use crate::stylesheet::Styles;
use crate::{Error, Limits, Result};

/// An MDX article source paired with its own headword index.
///
/// [`crate::Mdx::lookup`] resolves redirects and expands stylesheet markers;
/// [`crate::Read::read`] and [`crate::Read::text`] on its reader expose lower-level
/// record access. Parsing bytes does not discover filesystem resources, whereas
/// opening a file discovers associated volumes for later lazy reads.
pub struct Mdx<R = Reader<Mmap>, I = Index> {
    reader: R,
    index: I,
    resources: Resources,
}

enum Step {
    Enter { id: usize, depth: usize },
    Exit(usize),
}

impl Step {
    fn queue(stack: &mut Vec<Self>, ids: &[usize], depth: usize, limits: Limits) -> Result<()> {
        let count = stack
            .len()
            .checked_add(ids.len())
            .ok_or_else(|| Error::Corrupt("redirect queue overflow".into()))?;
        let bytes = count
            .checked_mul(std::mem::size_of::<Self>())
            .ok_or_else(|| Error::Corrupt("redirect queue size overflow".into()))?;
        Limits::check("redirect queue", bytes as u64, limits.metadata)?;
        stack
            .try_reserve(ids.len())
            .map_err(|error| Error::Corrupt(format!("redirect queue allocation: {error}")))?;
        stack.extend(ids.iter().rev().map(|&id| Self::Enter { id, depth }));
        Ok(())
    }
}

impl Mdx {
    /// Open an immutable memory-mapped MDX with default [`crate::Limits`].
    ///
    /// Reads or creates the neighboring `.mdict.idx` derived index cache and
    /// discovers `.mdd` and consecutively numbered `.1.mdd`, `.2.mdd` volumes.
    /// Cache I/O may create a persistent `.lock` sidecar and atomically replace
    /// the cache; see [`crate::Cache::open`]. MDD mapping is deferred until needed.
    ///
    /// # Errors
    ///
    /// Returns source I/O, parsing, index-building or discovery errors.
    /// Cache I/O/publication failures are warned about and fall back to an
    /// in-memory index; errors in lazily read records or volumes appear later.
    ///
    /// # Safety
    /// MDX and associated MDD files must remain unmodified and untruncated while
    /// this dictionary exists, including before lazy resource reads.
    /// Resource directory paths must remain stable for the same period.
    /// Advisory locking alone does not establish these invariants.
    pub unsafe fn open(path: &Path) -> Result<Self> {
        let limits = Limits::default();
        // SAFETY: The caller guarantees immutable source files.
        unsafe { Self::open_with(path, limits) }
    }

    /// Open an immutable memory-mapped MDX with explicit component budgets.
    ///
    /// Has the same cache-writing and resource-discovery effects as
    /// [`crate::Mdx::open`]; budgets are not an aggregate RSS cap.
    ///
    /// # Errors
    ///
    /// Returns the source, index or discovery errors from [`crate::Mdx::open`],
    /// including [`crate::Error::Limit`] when a component budget is exhausted.
    ///
    /// # Safety
    /// The MDX and associated MDD files must remain unmodified and untruncated
    /// for this dictionary's lifetime, including before lazy volume opening.
    /// Resource directory paths must remain stable. Advisory locks alone do
    /// not ensure this; use [`crate::Mdx::parse`] with owned bytes when uncertain.
    pub unsafe fn open_with(path: &Path, limits: Limits) -> Result<Self> {
        // SAFETY: The caller keeps all backing files immutable.
        let reader = unsafe { Reader::open_with(path, limits)? };
        let index =
            Cache::new(path.with_extension("mdict.idx"), reader.entries(), limits).open()?;
        // SAFETY: Associated volumes have the same caller-provided guarantee.
        let resources = unsafe { Resources::discover_with(path, limits)? };
        Ok(Self {
            reader,
            index,
            resources,
        })
    }
}

impl<B: AsRef<[u8]>> Mdx<Reader<B>> {
    /// Parse source bytes and build their index using default budgets.
    ///
    /// `fallback` supplies a missing title, not an encoding. No files are read or
    /// written and the associated resource collection is empty.
    ///
    /// # Errors
    ///
    /// Propagates [`crate::Reader::parse`] and [`crate::Lookup::build`] failures.
    /// Records are lazy, so later lookups can still report damaged payloads.
    ///
    /// ```
    /// use wodix::Mdx;
    ///
    /// let data = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/basic.mdx"));
    /// let dictionary = Mdx::parse(data.as_slice(), "fallback")?;
    /// assert_eq!(dictionary.lookup("apples")?, vec!["<p>A fruit.</p>"]);
    /// assert_eq!(dictionary.prefix("ap", 10)?, vec!["apple", "apples", "apricot"]);
    /// assert!(dictionary.lookup("not-in-this-fixture")?.is_empty());
    /// # Ok::<(), wodix::Error>(())
    /// ```
    pub fn parse(data: B, fallback: &str) -> Result<Self> {
        let reader = Reader::parse(data, fallback)?;
        Self::new(reader)
    }

    /// Parse and index source bytes with explicit [`crate::Limits`].
    ///
    /// Like [`crate::Mdx::parse`], this performs no filesystem I/O or resource
    /// discovery. `fallback` is used for a missing source title.
    ///
    /// # Errors
    ///
    /// Returns parsing or index-building errors, including exceeded budgets.
    pub fn with_limits(data: B, fallback: &str, limits: Limits) -> Result<Self> {
        let reader = Reader::with_limits(data, fallback, limits)?;
        Self::new(reader)
    }
}

impl<R: Read, I: Lookup> Mdx<R, I> {
    /// Build the index from this exact source; callers cannot replace either half.
    ///
    /// The new dictionary has no associated filesystem resources.
    ///
    /// # Errors
    ///
    /// Propagates [`crate::Lookup::build`] errors using the reader's limits.
    pub fn new(reader: R) -> Result<Self> {
        let index = I::build(reader.entries(), reader.limits())?;
        Ok(Self {
            reader,
            index,
            resources: Resources::default(),
        })
    }

    /// Borrow the source for metadata, source-order entries and raw/text reads.
    pub fn reader(&self) -> &R {
        &self.reader
    }

    /// Borrow the index whose entry IDs refer only to this dictionary's reader.
    pub fn index(&self) -> &I {
        &self.index
    }

    /// Borrow the associated lazy resource collection.
    ///
    /// File-opened dictionaries search local files and discovered MDD volumes;
    /// byte-parsed dictionaries have an empty collection. Its `load` operation
    /// may perform filesystem I/O, open volume index caches and resolve redirects.
    pub fn resources(&self) -> &Resources {
        &self.resources
    }

    /// Return resolved, style-expanded articles for a normalized headword.
    ///
    /// An absent headword returns an empty vector. Duplicate IDs and aliases
    /// are traversed in source order; identical decoded article text is emitted
    /// once before style expansion. Input matching follows [`crate::Index`].
    /// Unlike [`crate::Read::text`], this follows `@@@LINK=` targets and expands
    /// the source's stylesheet markers. It does not fetch resources or render HTML.
    ///
    /// # Errors
    ///
    /// Missing redirect targets, cycles and depth exhaustion return
    /// [`crate::Error::RedirectMissing`], [`crate::Error::RedirectCycle`] or
    /// [`crate::Error::RedirectLimit`]. Read, encoding and component-budget errors
    /// are propagated. On failure no partial article vector is returned.
    pub fn lookup(&self, word: &str) -> Result<Vec<String>> {
        let limits = self.reader.limits();
        let mut stack = Vec::new();
        Step::queue(&mut stack, self.index.exact(word), 0, limits)?;
        let mut trace = Trace {
            active: HashSet::new(),
            limit: limits.redirects,
        };
        let mut complete = HashSet::new();
        let mut seen = HashSet::new();
        let mut articles = Vec::new();
        let mut output_size = 0usize;
        let mut visits = 0u64;
        let mut decoded_size = 0usize;

        while let Some(step) = stack.pop() {
            match step {
                Step::Exit(id) => {
                    trace.active.remove(&id);
                    complete.insert(id);
                }
                Step::Enter { id, depth } => {
                    if complete.contains(&id) {
                        continue;
                    }
                    trace.enter(id, depth)?;
                    visits = visits
                        .checked_add(1)
                        .ok_or_else(|| Error::Corrupt("lookup work overflow".into()))?;
                    Limits::check("lookup visits", visits, limits.entries)?;
                    let raw = self.reader.text(id)?;
                    decoded_size = decoded_size
                        .checked_add(raw.capacity())
                        .ok_or_else(|| Error::Corrupt("lookup decoded size overflow".into()))?;
                    Limits::check("lookup decoded bytes", decoded_size as u64, limits.metadata)?;
                    if let Some(link) = Link::parse(&raw)? {
                        let targets = self.redirect(link.target);
                        if targets.is_empty() {
                            return Err(Error::RedirectMissing(link.target.to_owned()));
                        }
                        let next = depth
                            .checked_add(1)
                            .ok_or(Error::RedirectLimit(limits.redirects))?;
                        stack.push(Step::Exit(id));
                        Step::queue(&mut stack, targets, next, limits)?;
                    } else {
                        trace.active.remove(&id);
                        complete.insert(id);
                        if !seen.contains(&raw) {
                            let styles = Styles {
                                rules: &self.reader.metadata().stylesheets,
                                limit: limits.metadata - output_size,
                            };
                            let article = styles.apply(&raw)?;
                            output_size += article.len();
                            seen.insert(raw);
                            articles.push(article);
                        }
                    }
                }
            }
        }
        Ok(articles)
    }

    /// Return at most `limit` distinct original headwords matching a prefix.
    ///
    /// With [`crate::Index`], normalized keys are visited lexicographically and
    /// duplicate groups retain source order. No article payloads are decoded.
    /// A zero limit returns an empty vector without evaluating the query.
    ///
    /// # Errors
    ///
    /// Propagates query validation or budget errors from [`crate::Lookup::visit`].
    /// On error no partial result vector is returned.
    pub fn prefix(&self, word: &str, limit: usize) -> Result<Vec<&str>> {
        let query = Query::Prefix(word);
        self.search(query, limit)
    }

    /// Return at most `limit` distinct headwords within the given edit distance.
    ///
    /// [`crate::Index`] measures Levenshtein distance in Unicode scalar values
    /// after normalization, not UTF-8 bytes or grapheme clusters. Results follow
    /// normalized lexical order, not a relevance or distance ranking. A zero
    /// limit returns no results without query evaluation.
    ///
    /// # Errors
    ///
    /// Propagates search and fuzzy work-budget errors from [`crate::Lookup::visit`].
    /// No partial result vector is returned if the search fails.
    pub fn fuzzy(&self, word: &str, distance: u32, limit: usize) -> Result<Vec<&str>> {
        let query = Query::Fuzzy { word, distance };
        self.search(query, limit)
    }

    fn search(&self, query: Query<'_>, limit: usize) -> Result<Vec<&str>> {
        let mut results = Vec::new();
        if limit == 0 {
            return Ok(results);
        }
        let entries = self.reader.entries();
        let mut seen = HashSet::new();
        self.index.visit(query, |ids| {
            for &id in ids {
                if let Some(entry) = entries.get(id) {
                    if seen.insert(entry.headword.as_str()) {
                        results.push(entry.headword.as_str());
                        if results.len() == limit {
                            return false;
                        }
                    }
                }
            }
            true
        })?;
        Ok(results)
    }

    fn redirect(&self, target: &str) -> &[usize] {
        let entries = self.index.exact(target);
        if !entries.is_empty() {
            return entries;
        }
        let trimmed = target
            .trim()
            .trim_end_matches(|character: char| character.is_ascii_punctuation());
        if trimmed != target {
            return self.index.exact(trimmed);
        }
        &[]
    }
}

impl<R: Read, I> std::fmt::Debug for Mdx<R, I> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("Mdx")
            .field("title_bytes", &self.reader.metadata().title.len())
            .field("entries", &self.reader.entries().len())
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::record_block::Bytes;
    use crate::types::{Encoding, Encryption, Entry, Metadata, StyleSheet, Version};

    #[test]
    fn debug_omits_unbounded_metadata() {
        let mut reader = Memory::new(&[("word", "article")], Limits::default());
        reader.metadata.title = "private-title".repeat(100_000);
        let dictionary: Mdx<_> = Mdx::new(reader).unwrap();
        for rendered in [format!("{dictionary:?}"), format!("{dictionary:#?}")] {
            assert!(rendered.len() < 256);
            assert!(!rendered.contains("private-title"));
        }
    }

    struct Memory<'a> {
        data: &'a [(&'a str, &'a str)],
        entries: Vec<Entry>,
        metadata: Metadata,
        limits: Limits,
    }

    impl<'a> Memory<'a> {
        fn new(data: &'a [(&'a str, &'a str)], limits: Limits) -> Self {
            let entries = data
                .iter()
                .enumerate()
                .map(|(id, (word, _))| Entry {
                    headword: (*word).to_owned(),
                    record_offset: id as u64,
                })
                .collect();
            Self {
                data,
                entries,
                limits,
                metadata: Metadata {
                    version: Version::V2,
                    encoding: Encoding::Utf8,
                    encrypted: Encryption::NONE,
                    title: "memory source".into(),
                    description: String::new(),
                    stylesheets: StyleSheet::new(),
                    rtl: false,
                },
            }
        }
    }

    impl Read for Memory<'_> {
        fn metadata(&self) -> &Metadata {
            &self.metadata
        }
        fn entries(&self) -> &[Entry] {
            &self.entries
        }
        fn limits(&self) -> Limits {
            self.limits
        }
        fn read(&self, id: usize) -> Result<Bytes> {
            let (_, text) = self
                .data
                .get(id)
                .ok_or_else(|| Error::Corrupt("bad memory entry".into()))?;
            Ok(text.as_bytes().to_vec().into())
        }
    }

    #[test]
    fn unique_candidates_are_not_limited_by_duplicate_postings() {
        let data = [("aa", "A"); 6]
            .into_iter()
            .chain([("ab", "B")])
            .collect::<Vec<_>>();
        let dictionary = Mdx::<_, Index>::new(Memory::new(&data, Limits::default())).unwrap();
        assert_eq!(dictionary.prefix("a", 2).unwrap(), vec!["aa", "ab"]);
        assert_eq!(dictionary.fuzzy("a", 1, 2).unwrap(), vec!["aa", "ab"]);
        assert!(dictionary.prefix("a", 0).unwrap().is_empty());
        assert!(dictionary.fuzzy("a", 1, 0).unwrap().is_empty());
    }

    #[test]
    fn fuzzy_distance_counts_scalars_and_reports_exhausted_work() {
        let data = [
            ("猫", "cat"),
            ("狗", "dog"),
            ("猫咪", "kitten"),
            ("\u{20000}", "han"),
            ("\u{10400}", "deseret"),
        ];
        let dictionary = Mdx::<_, Index>::new(Memory::new(&data, Limits::default())).unwrap();
        assert_eq!(
            dictionary.fuzzy("猫", 1, 10).unwrap(),
            vec!["狗", "猫", "猫咪", "\u{10400}", "\u{20000}"]
        );
        let limits = Limits {
            fuzzy_cells: 4,
            ..Limits::default()
        };
        let short = [("a", "A"), ("b", "B")];
        let limited = Mdx::<_, Index>::new(Memory::new(&short, limits)).unwrap();
        assert!(matches!(
            limited.fuzzy("z", 1, 10),
            Err(Error::Limit {
                kind: "fuzzy cells",
                ..
            })
        ));
    }

    #[test]
    fn redirects_distinguish_missing_cycles_and_real_misses() {
        let data = [
            ("x", "@@@LINK=missing"),
            ("y", "@@@LINK=z"),
            ("z", "@@@LINK=y"),
        ];
        let dictionary = Mdx::<_, Index>::new(Memory::new(&data, Limits::default())).unwrap();
        assert!(matches!(
            dictionary.lookup("x"),
            Err(Error::RedirectMissing(_))
        ));
        assert!(matches!(
            dictionary.lookup("y"),
            Err(Error::RedirectCycle(_))
        ));
        assert!(dictionary.lookup("not present").unwrap().is_empty());
    }

    #[test]
    fn shared_redirect_targets_are_not_cycles() {
        let data = [
            ("root", "@@@LINK=target"),
            ("root", "@@@LINK=target"),
            ("target", "definition"),
        ];
        let dictionary = Mdx::<_, Index>::new(Memory::new(&data, Limits::default())).unwrap();
        assert_eq!(dictionary.lookup("root").unwrap(), vec!["definition"]);
        let limits = Limits {
            redirects: 0,
            ..Limits::default()
        };
        let dictionary = Mdx::<_, Index>::new(Memory::new(&data, limits)).unwrap();
        assert!(matches!(
            dictionary.lookup("root"),
            Err(Error::RedirectLimit(0))
        ));
        assert_eq!(dictionary.lookup("target").unwrap(), vec!["definition"]);
    }

    #[test]
    fn empty_and_truncated_sources_are_errors() {
        assert!(matches!(
            Mdx::parse(Vec::new(), "empty"),
            Err(Error::InvalidHeader(_))
        ));
        assert!(matches!(
            Mdx::parse(100_i32.to_be_bytes(), "short"),
            Err(Error::InvalidHeader(_))
        ));
        // SAFETY: No backing file exists.
        assert!(matches!(
            unsafe { Mdx::open(Path::new("/nonexistent/dict.mdx")) },
            Err(Error::Io(_))
        ));
    }
}