wty 0.8.2

Yomitan-compatible dictionaries from wikitionary data
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
//! Command line interface.

use std::{fmt, ops::Deref, path::PathBuf, str::FromStr};

use anyhow::{Ok, Result, bail};
use clap::{Parser, Subcommand};

use crate::{
    dict::{Langs, WriterFormat},
    lang::{Edition, EditionSpec, Lang},
    models::kaikki::WordEntry,
    path::{DictionaryType, PathManager},
};

#[derive(Debug, Parser)]
#[command(version)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Command,

    // NOTE: the order in which this --verbose flag appears in subcommands help seems cursed.
    //
    /// Verbose output (set logging level to DEBUG)
    #[arg(long, short, global = true)]
    pub verbose: bool,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    /// Main dictionary. Uses target for the edition
    Main(MainArgs),

    /// Short dictionary made from translations. Uses source for the edition
    Glossary(GlossaryArgs),

    /// Short dictionary made from translations. Supports any language pair
    GlossaryExtended(GlossaryExtendedArgs),

    /// Phonetic transcription dictionary. Uses target for the edition
    Ipa(IpaArgs),

    /// Phonetic transcription dictionary. Uses all editions
    IpaMerged(IpaMergedArgs),

    /// Download a Kaikki jsonlines
    Download(MainArgs),

    /// Show supported iso codes, with coloured editions
    Iso(IsoArgs),

    /// Build a release with all dictionaries
    Release(ReleaseArgs),

    /// Diagnostic utility
    Scan(MainLangs),
}

#[derive(Parser, Debug, Clone)]
pub struct MainArgs {
    #[command(flatten)]
    pub langs: MainLangs,

    /// Dictionary name
    #[arg(default_value_t)]
    pub dict_name: DictName,

    #[command(flatten)]
    pub options: Options,
}

#[derive(Parser, Debug)]
pub struct GlossaryArgs {
    #[command(flatten)]
    pub langs: GlossaryLangs,

    /// Dictionary name
    #[arg(default_value_t)]
    pub dict_name: DictName,

    #[command(flatten)]
    pub options: Options,
}

#[derive(Parser, Debug)]
pub struct GlossaryExtendedArgs {
    #[command(flatten)]
    pub langs: GlossaryExtendedLangs,

    /// Dictionary name
    #[arg(default_value_t)]
    pub dict_name: DictName,

    #[command(flatten)]
    pub options: Options,
}

#[derive(Parser, Debug)]
pub struct IpaArgs {
    #[command(flatten)]
    pub langs: MainLangs,

    /// Dictionary name
    #[arg(default_value_t)]
    pub dict_name: DictName,

    #[command(flatten)]
    pub options: Options,
}

#[derive(Parser, Debug)]
pub struct IpaMergedArgs {
    #[command(flatten)]
    pub langs: IpaMergedLangs,

    /// Dictionary name
    #[arg(default_value_t)]
    pub dict_name: DictName,

    #[command(flatten)]
    pub options: Options,
}

#[derive(Parser, Debug)]
pub struct ReleaseArgs {
    /// Change the root directory
    #[arg(long, default_value = "data")]
    pub root_dir: PathBuf,
}

#[derive(Parser, Debug, Default)]
pub struct IsoArgs {
    /// Only print languages with edition
    #[arg(long)]
    pub edition: bool,
}

/// Langs-like struct that validates edition for `target` and skips `edition`.
#[derive(Parser, Debug, Clone)]
pub struct MainLangs {
    /// Source language
    pub source: Lang,

    /// Target language (edition)
    pub target: Edition,
}

/// Langs-like struct that validates edition for `source` and skips `edition`.
#[derive(Parser, Debug, Clone)]
pub struct GlossaryLangs {
    /// Source language (edition)
    pub source: Edition,

    /// Target language
    pub target: Lang,
}

/// Langs-like struct that validates edition for `edition`.
#[derive(Parser, Debug, Clone)]
pub struct GlossaryExtendedLangs {
    /// Edition language
    pub edition: EditionSpec,

    /// Source language
    pub source: Lang,

    /// Target language
    pub target: Lang,
}

/// Langs-like struct that only takes one language.
#[derive(Parser, Debug, Clone)]
pub struct IpaMergedLangs {
    /// Target language
    pub target: Lang,
}

#[expect(clippy::struct_excessive_bools)]
#[derive(Parser, Debug, Default, Clone)]
pub struct Options {
    /// Redownload kaikki files
    #[arg(long, short)]
    pub redownload: bool,

    /// Only keep the first n filtered lines. -1 keeps all
    #[arg(long, default_value_t = -1)]
    pub first: i32,

    // Example:
    //   `--filter pos,adv`
    //
    // You can specify this option multiple times:
    //   `--filter pos,adv --filter word,foo`
    //
    /// Only keep entries matching certain key–value filters
    #[arg(long, value_parser = parse_tuple)]
    pub filter: Vec<(FilterKey, String)>,

    // Example:
    //   `--reject pos,adj`
    //
    // You can specify this option multiple times:
    //   `--reject pos,adj --reject word,foo`
    //
    /// Only keep entries not matching certain key–value filters
    #[arg(long, value_parser = parse_tuple)]
    pub reject: Vec<(FilterKey, String)>,

    /// Do not print anything to the console
    #[arg(long, short)]
    pub quiet: bool,

    /// Write jsons with whitespace
    #[arg(short, long)]
    pub pretty: bool,

    /// Include experimental features
    #[arg(short, long)]
    pub experimental: bool,

    /// Change the root directory
    #[arg(long, default_value = "data")]
    pub root_dir: PathBuf,

    /// Writer format
    #[arg(long, default_value_t = WriterFormat::Yomitan)]
    pub format: WriterFormat,
}

/// Newtype string wrapper to overwrite Default with `wty`.
#[derive(Debug, Clone)]
pub struct DictName(String);

impl Default for DictName {
    fn default() -> Self {
        Self(String::from("wty"))
    }
}

impl FromStr for DictName {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Result::Ok(Self(String::from(s)))
    }
}

impl fmt::Display for DictName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl Deref for DictName {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

fn parse_tuple(s: &str) -> Result<(FilterKey, String), String> {
    let parts: Vec<_> = s.split(',').map(|x| x.trim().to_string()).collect();
    if parts.len() != 2 {
        return Err("expected two comma-separated values".into());
    }
    let filter_key = FilterKey::try_from(parts[0].as_str()).map_err(|e| e.to_string())?;
    core::result::Result::Ok((filter_key, parts[1].clone()))
}

/// A key used to filter a [`WordEntry`] by one of its fields.
#[derive(Debug, Clone)]
pub enum FilterKey {
    LangCode,
    Word,
    Pos,
}

impl FilterKey {
    pub fn field_value<'a>(&self, entry: &'a WordEntry) -> &'a str {
        match self {
            Self::LangCode => &entry.lang_code,
            Self::Word => &entry.word,
            Self::Pos => &entry.pos,
        }
    }

    fn try_from(s: &str) -> Result<Self> {
        match s {
            "lang_code" => Ok(Self::LangCode),
            "word" => Ok(Self::Word),
            "pos" => Ok(Self::Pos),
            other => bail!("unknown filter key '{other}'. Choose between: lang_code | word | pos"),
        }
    }
}

fn check_simple_english(source: &Lang, target: &Lang) -> Result<()> {
    match (source, target) {
        (Lang::Simple, Lang::Simple) => Ok(()),
        (Lang::Simple, _) | (_, Lang::Simple) => {
            anyhow::bail!("Simple English must be used as both source and target.")
        }
        _ => Ok(()),
    }
}

// Only the main dictionary makes sense with Simple English
fn err_on_simple_english(source: &Lang, target: &Lang) -> Result<()> {
    match (source, target) {
        (Lang::Simple, _) | (_, Lang::Simple) => {
            anyhow::bail!("Simple English can not be used for this dictionary.")
        }
        _ => Ok(()),
    }
}

fn err_on_source_being_target(source: &Lang, target: &Lang) -> Result<()> {
    if source == target {
        anyhow::bail!("in a glossary dictionary source must be different from target.");
    }
    Ok(())
}

impl Cli {
    pub fn parse_cli() -> Self {
        Self::parse()
    }
}

/// Unified language configuration. See [`crate::dict::Langs`].
#[derive(Debug, Clone, Copy)]
pub struct LangSpecs {
    pub edition: EditionSpec,
    pub source: Lang,
    pub target: Lang,
}

impl From<Langs> for LangSpecs {
    fn from(value: Langs) -> Self {
        Self {
            edition: EditionSpec::One(value.edition),
            source: value.source,
            target: value.target,
        }
    }
}

impl TryFrom<MainLangs> for LangSpecs {
    type Error = anyhow::Error;

    fn try_from(langs: MainLangs) -> Result<Self> {
        check_simple_english(&langs.source, &langs.target.into())?;

        Ok(Self {
            edition: EditionSpec::One(langs.target),
            source: langs.source,
            target: langs.target.into(),
        })
    }
}

impl TryFrom<GlossaryLangs> for LangSpecs {
    type Error = anyhow::Error;

    fn try_from(langs: GlossaryLangs) -> Result<Self> {
        err_on_simple_english(&langs.source.into(), &langs.target)?;
        err_on_source_being_target(&langs.source.into(), &langs.target)?;

        Ok(Self {
            edition: EditionSpec::One(langs.source),
            source: langs.source.into(),
            target: langs.target,
        })
    }
}

impl TryFrom<GlossaryExtendedLangs> for LangSpecs {
    type Error = anyhow::Error;

    fn try_from(langs: GlossaryExtendedLangs) -> Result<Self> {
        err_on_simple_english(&langs.source, &langs.target)?;
        err_on_source_being_target(&langs.source, &langs.target)?;

        Ok(Self {
            edition: langs.edition,
            source: langs.source,
            target: langs.target,
        })
    }
}

impl TryFrom<IpaMergedLangs> for LangSpecs {
    type Error = anyhow::Error;

    fn try_from(langs: IpaMergedLangs) -> Result<Self> {
        err_on_simple_english(&langs.target, &langs.target)?;

        Ok(Self {
            edition: EditionSpec::All,
            source: langs.target, // Not used
            target: langs.target,
        })
    }
}

macro_rules! impl_try_into_pathmanager {
    ($ty:ty, $dict_ty:expr) => {
        impl TryFrom<$ty> for PathManager {
            type Error = anyhow::Error;

            fn try_from(args: $ty) -> Result<Self> {
                Ok(Self {
                    dict_ty: $dict_ty,
                    dict_name: args.dict_name,
                    langs: args.langs.try_into()?,
                    opts: args.options,
                })
            }
        }
    };
}

impl_try_into_pathmanager!(MainArgs, DictionaryType::Main);
impl_try_into_pathmanager!(GlossaryArgs, DictionaryType::Glossary);
impl_try_into_pathmanager!(GlossaryExtendedArgs, DictionaryType::GlossaryExtended);
impl_try_into_pathmanager!(IpaArgs, DictionaryType::Ipa);
impl_try_into_pathmanager!(IpaMergedArgs, DictionaryType::IpaMerged);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn base_commands() {
        assert!(Cli::try_parse_from(["wty", "main", "el", "en"]).is_ok());
        assert!(Cli::try_parse_from(["wty", "glossary", "el", "en"]).is_ok());
    }

    #[test]
    fn main_needs_target_edition() {
        assert!(Cli::try_parse_from(["wty", "main", "grc", "el"]).is_ok());
        assert!(Cli::try_parse_from(["wty", "main", "el", "grc"]).is_err());
    }

    #[test]
    fn glossary_needs_source_edition() {
        assert!(Cli::try_parse_from(["wty", "glossary", "grc", "el"]).is_err());
        assert!(Cli::try_parse_from(["wty", "glossary", "el", "grc"]).is_ok());
    }

    #[test]
    fn glossary_can_not_be_monolingual() {
        let res = Cli::try_parse_from(["wty", "glossary", "el", "el"]);
        let cli = res.unwrap(); // The parsing should be ok
        if let Command::Glossary(glossary_args) = cli.command {
            assert!(LangSpecs::try_from(glossary_args.langs).is_err());
        } else {
            panic!()
        }

        let res = Cli::try_parse_from(["wty", "glossary-extended", "all", "el", "el"]);
        let cli = res.unwrap(); // The parsing should be ok
        if let Command::GlossaryExtended(glossary_args) = cli.command {
            assert!(LangSpecs::try_from(glossary_args.langs).is_err());
        } else {
            panic!()
        }
    }

    #[test]
    fn filter_flag() {
        assert!(MainArgs::try_parse_from(["_pname", "el", "el", "--filter", "foo,bar"]).is_err());
        assert!(MainArgs::try_parse_from(["_pname", "el", "el", "--filter", "word,hello"]).is_ok());
        assert!(MainArgs::try_parse_from(["_pname", "el", "el", "--reject", "pos,name"]).is_ok());
    }
}