Skip to main content

mdict_rs/
types.rs

1use std::collections::BTreeMap;
2
3/// Distinguishes text dictionaries (`.mdx`) from resource dictionaries
4/// (`.mdd`).
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum ContainerKind {
7    Mdx,
8    Mdd,
9}
10
11/// Zero-based key ordinal within a specific dictionary file snapshot.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct KeyOrdinal(u64);
14
15impl KeyOrdinal {
16    /// Creates a zero-based key ordinal.
17    pub const fn new(value: u64) -> Self {
18        Self(value)
19    }
20
21    /// Returns the underlying zero-based ordinal value.
22    pub const fn get(self) -> u64 {
23        self.0
24    }
25}
26
27impl From<u64> for KeyOrdinal {
28    fn from(value: u64) -> Self {
29        Self::new(value)
30    }
31}
32
33impl From<KeyOrdinal> for u64 {
34    fn from(value: KeyOrdinal) -> Self {
35        value.get()
36    }
37}
38
39/// Raw MDict encryption bit flags from the header metadata.
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub struct EncryptionMode(pub u8);
42
43impl EncryptionMode {
44    pub fn has_keyword_header(self) -> bool {
45        self.0 & 0b01 != 0
46    }
47
48    pub fn has_keyword_index(self) -> bool {
49        self.0 & 0b10 != 0
50    }
51}
52
53/// Parsed top-level XML metadata from an MDict file.
54#[derive(Debug, Clone, PartialEq, Eq)]
55pub struct Header {
56    pub raw_xml: String,
57    pub tag_name: String,
58    pub attributes: BTreeMap<String, String>,
59    pub generated_by_engine_version: String,
60    pub required_engine_version: String,
61    pub encoding_label: Option<String>,
62    pub format: Option<String>,
63    pub key_case_sensitive: bool,
64    pub strip_key: bool,
65    pub encrypted: u8,
66    pub register_by: Option<String>,
67    pub reg_code: Option<String>,
68    pub description: Option<String>,
69    pub title: Option<String>,
70    pub creation_date: Option<String>,
71}
72
73impl Header {
74    /// Returns the interpreted encryption flags from the header.
75    pub fn encryption_mode(&self) -> EncryptionMode {
76        EncryptionMode(self.encrypted)
77    }
78
79    /// Returns `true` when the dictionary declares a version 2.x engine.
80    pub fn is_v2(&self) -> bool {
81        self.generated_by_engine_version
82            .split('.')
83            .next()
84            .and_then(|v| v.parse::<u32>().ok())
85            .unwrap_or(0)
86            >= 2
87    }
88}
89
90/// Passcode material for dictionaries that encrypt the keyword header.
91#[derive(Debug, Clone, PartialEq, Eq)]
92pub struct Passcode {
93    pub reg_code_hex: String,
94    pub user_id: String,
95}
96
97/// Options used when opening an MDX or MDD file.
98#[derive(Debug, Clone, Default, PartialEq, Eq)]
99pub struct OpenOptions {
100    pub passcode: Option<Passcode>,
101}