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
use std::fmt;
use std::io::Write;
use std::iter::FusedIterator;
use std::path::Path;
use std::sync::Arc;
use crate::core::{MdictFile, RecordDescriptor, RecordIter};
use crate::error::{Error, Result};
use crate::lookup::{KeyMatchPage, KeyMatches};
use crate::types::{ContainerKind, Header, KeyEntry, KeyOrdinal, MemoryUsage, OpenOptions};
/// An opened MDD resource dictionary.
pub struct MddFile {
inner: Arc<MdictFile>,
}
impl fmt::Debug for MddFile {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("MddFile")
.field("len", &self.len())
.field("title", &self.header().title())
.finish_non_exhaustive()
}
}
/// One materialized MDD resource with its physical identity.
#[derive(PartialEq, Eq)]
pub struct MddResource {
key: KeyEntry,
bytes: Vec<u8>,
}
impl fmt::Debug for MddResource {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("MddResource")
.field("ordinal", &self.ordinal())
.field("key", &self.key())
.field("len", &self.bytes.len())
.finish()
}
}
impl MddResource {
fn new(key: KeyEntry, bytes: Vec<u8>) -> Self {
Self { key, bytes }
}
/// Returns the resource's physical ordinal.
pub const fn ordinal(&self) -> KeyOrdinal {
self.key.ordinal()
}
/// Returns the original decoded resource key.
pub fn key(&self) -> &str {
self.key.key()
}
/// Returns the physical key row.
pub fn key_entry(&self) -> &KeyEntry {
&self.key
}
/// Returns the materialized resource bytes.
pub fn bytes(&self) -> &[u8] {
&self.bytes
}
/// Consumes the resource into its key row and bytes.
pub fn into_parts(self) -> (KeyEntry, Vec<u8>) {
(self.key, self.bytes)
}
}
/// A source-bound, lazily readable MDD resource span.
///
/// The span retains its originating open file, so it cannot accidentally be
/// read against a different MDD file. It is valid while that file snapshot is
/// unchanged.
#[derive(Clone)]
pub struct MddResourceSpan {
source: Arc<MdictFile>,
key: KeyEntry,
start: u64,
end: u64,
len: u64,
}
impl fmt::Debug for MddResourceSpan {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("MddResourceSpan")
.field("ordinal", &self.ordinal())
.field("key", &self.key())
.field("len", &self.len)
.finish_non_exhaustive()
}
}
impl MddResourceSpan {
fn new(source: Arc<MdictFile>, descriptor: RecordDescriptor) -> Result<Self> {
let len = source.validate_record_span(descriptor.start, descriptor.end)?;
Ok(Self {
source,
key: descriptor.key,
start: descriptor.start,
end: descriptor.end,
len,
})
}
/// Returns the resource's physical ordinal.
pub const fn ordinal(&self) -> KeyOrdinal {
self.key.ordinal()
}
/// Returns the original decoded resource key.
pub fn key(&self) -> &str {
self.key.key()
}
/// Returns the physical key row.
pub fn key_entry(&self) -> &KeyEntry {
&self.key
}
/// Returns the decoded resource length without materializing it.
pub const fn len(&self) -> u64 {
self.len
}
/// Returns whether this resource is empty.
pub const fn is_empty(&self) -> bool {
self.len == 0
}
/// Materializes this resource into memory.
///
/// # Errors
///
/// Returns an error if the span is invalid, exceeds the materialization
/// limit, cannot be decoded, or cannot be read from its source.
pub fn read(&self) -> Result<MddResource> {
let bytes = self.source.read_record_span(self.start, self.end)?;
Ok(MddResource::new(self.key.try_clone()?, bytes))
}
/// Streams the resource into an `std::io::Write` destination.
///
/// Streaming is not subject to the whole-resource materialization limit;
/// individual record blocks remain bounded.
///
/// # Errors
///
/// Returns an error if the span or a record block is invalid, the source
/// cannot be read, or the destination cannot be written.
pub fn copy_to<W: Write + ?Sized>(&self, destination: &mut W) -> Result<u64> {
let mut written = 0u64;
self.source
.visit_record_span(self.start, self.end, |chunk| {
destination.write_all(chunk)?;
let chunk_len = u64::try_from(chunk.len())
.map_err(|_| Error::InvalidFormat("resource chunk length exceeds u64"))?;
written = written
.checked_add(chunk_len)
.ok_or(Error::InvalidFormat("resource write length overflow"))?;
Ok(())
})?;
Ok(written)
}
}
impl MddFile {
/// The shared core state, for crate-internal structural work.
pub(crate) fn core(&self) -> &MdictFile {
&self.inner
}
/// Opens an MDD file with default options.
///
/// As with [`MdxFile::open`](crate::MdxFile::open), the path may be a
/// `file://` URL from a mobile file picker.
///
/// # Errors
///
/// Returns an error if the file cannot be read, requests an unsupported
/// format path, exceeds a safety limit, or contains malformed metadata or
/// indexes.
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
Self::open_with_options(path, &OpenOptions::new())
}
/// Opens an MDD file with reusable explicit options.
///
/// # Errors
///
/// Returns an error under the same conditions as [`Self::open`], including
/// when an encrypted header cannot be opened with the supplied options.
pub fn open_with_options(path: impl AsRef<Path>, options: &OpenOptions) -> Result<Self> {
Ok(Self {
inner: Arc::new(MdictFile::open(path, ContainerKind::Mdd, options)?),
})
}
/// Returns parsed top-level header metadata.
pub fn header(&self) -> &Header {
self.inner.header()
}
/// Returns the number of physical resource entries.
pub fn len(&self) -> u64 {
self.inner.len()
}
/// Returns whether the resource dictionary declares no entries.
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
/// Reports parser memory charged to this open handle's safety budget.
///
/// # Errors
///
/// Returns an error if an internal cache mutex was poisoned by a panic in
/// another thread.
pub fn memory_usage(&self) -> Result<MemoryUsage> {
self.inner.memory_usage()
}
/// Iterates over original keys in physical file order without reading
/// resource payloads.
///
/// Block read and decode failures are yielded as an item; after yielding
/// an error, the iterator is exhausted.
pub fn keys(&self) -> impl FusedIterator<Item = Result<KeyEntry>> + '_ {
self.inner.keys()
}
/// Returns one physical key row by ordinal without reading its resource.
///
/// # Errors
///
/// Returns an error if the corresponding key block cannot be read or is
/// malformed.
pub fn key_at(&self, ordinal: KeyOrdinal) -> Result<Option<KeyEntry>> {
self.inner.key_at_ordinal(ordinal)
}
/// Resolves physical key rows for the supplied ordinals in input order.
///
/// # Errors
///
/// Returns an error if any requested key block cannot be read or is
/// malformed.
pub fn keys_at(&self, ordinals: &[KeyOrdinal]) -> Result<Vec<Option<KeyEntry>>> {
self.inner.keys_at_ordinals(ordinals)
}
/// Resolves one resource to a source-bound span without reading its bytes.
///
/// # Errors
///
/// Returns an error if its key block is malformed or its record offsets do
/// not describe a valid span.
pub fn span_at(&self, ordinal: KeyOrdinal) -> Result<Option<MddResourceSpan>> {
self.inner
.record_at_ordinal(ordinal)?
.map(|descriptor| MddResourceSpan::new(Arc::clone(&self.inner), descriptor))
.transpose()
}
/// Materializes one resource by physical ordinal.
///
/// # Errors
///
/// Returns an error if the resource cannot be resolved, exceeds the
/// materialization limit, or its record blocks cannot be read and decoded.
pub fn resource_at(&self, ordinal: KeyOrdinal) -> Result<Option<MddResource>> {
self.span_at(ordinal)?.map(|span| span.read()).transpose()
}
/// Locates every physical resource matching a query without reading bytes.
///
/// Raw-exact matches across the entire file always win. Header-normalized
/// fallback is considered only when no raw key matches, and duplicates are
/// returned in ascending physical order.
///
/// # Errors
///
/// Returns an error if the global locator cannot be built because key data
/// is malformed, unreadable, or exceeds a safety limit.
pub fn locate(&self, query: &str) -> Result<Option<KeyMatches>> {
self.inner
.locate_keys(query)
.map(|matches| matches.map(KeyMatches::from_located))
}
/// Locates a bounded window of matching physical resources without
/// reading bytes or materializing the complete duplicate set.
///
/// Ordering, duplicate identity, total-count reporting, and global
/// raw-exact precedence are identical to [`Self::locate`]. A page beyond
/// the complete match set is `Some` but empty.
///
/// # Errors
///
/// Returns an error if the global locator cannot be built, source key data
/// is malformed or unreadable, or the requested page exceeds a configured
/// safety or aggregate-memory limit.
pub fn locate_page(
&self,
query: &str,
offset: usize,
limit: usize,
) -> Result<Option<KeyMatchPage>> {
self.inner
.locate_key_page(query, offset, limit)
.map(|page| page.map(KeyMatchPage::from_located))
}
/// Resolves a query to a source-bound resource span without reading its
/// bytes.
///
/// # Errors
///
/// Returns an error if lookup encounters malformed key data or the matched
/// record offsets do not describe a valid span.
pub fn lookup_span(&self, query: &str) -> Result<Option<MddResourceSpan>> {
let Some(matches) = self.locate(query)? else {
return Ok(None);
};
self.span_at(matches.first())
}
/// Looks up and materializes one resource.
///
/// # Errors
///
/// Returns an error if lookup fails structurally or the matched resource
/// cannot be read within the materialization limit.
pub fn lookup(&self, query: &str) -> Result<Option<MddResource>> {
self.lookup_span(query)?.map(|span| span.read()).transpose()
}
/// Iterates lazily over materialized resources in physical file order.
///
/// Block read and decode failures are yielded as an item; after yielding
/// an error, the iterator is exhausted.
pub fn resources(&self) -> impl FusedIterator<Item = Result<MddResource>> + '_ {
MddResourceIter {
file: self,
records: self.inner.records(),
done: false,
}
}
fn read_resource(&self, descriptor: RecordDescriptor) -> Result<MddResource> {
let bytes = self
.inner
.read_record_span(descriptor.start, descriptor.end)?;
Ok(MddResource::new(descriptor.key, bytes))
}
}
struct MddResourceIter<'a> {
file: &'a MddFile,
records: RecordIter<'a>,
done: bool,
}
impl Iterator for MddResourceIter<'_> {
type Item = Result<MddResource>;
fn next(&mut self) -> Option<Self::Item> {
if self.done {
return None;
}
let result = self
.records
.next()
.map(|result| result.and_then(|descriptor| self.file.read_resource(descriptor)));
if result.as_ref().is_some_and(Result::is_err) {
self.done = true;
}
result
}
}
impl FusedIterator for MddResourceIter<'_> {}