typing_engine 0.4.3

A typing game engine for Japanese and English.
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
use std::collections::VecDeque;
use std::error::Error;
use std::fmt::{Debug, Display};
use std::num::NonZeroUsize;

use crate::typing_primitive_types::spell::SpellString;
use crate::typing_primitive_types::vocabulary::{VocabularyEntry, VocabularySpellElement};

#[derive(Debug, Clone, PartialEq, Eq)]
enum VocabularyParseErrorKind {
    MultipleLines,
    ComponentsCountMisMatch,
    CompoundSymbolMisMatch,
    EmptyCompound,
    ViewAndSpellsCountMisMatch,
    InvalidSpellString(String),
    Internal(String),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VocabularyParseError {
    kind: VocabularyParseErrorKind,
}

impl VocabularyParseError {
    fn new(kind: VocabularyParseErrorKind) -> Self {
        Self { kind }
    }
}

impl Display for VocabularyParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            VocabularyParseErrorKind::MultipleLines => {
                write!(f, "Multiple lines in input, expected a single line")
            }
            VocabularyParseErrorKind::ComponentsCountMisMatch => write!(
                f,
                "Component count mismatch, expected 2 components separated by ':'"
            ),
            VocabularyParseErrorKind::CompoundSymbolMisMatch => write!(
                f,
                "Compound symbol mismatch, '[' and ']' don't match properly"
            ),
            VocabularyParseErrorKind::EmptyCompound => write!(f, "Empty compound is not allowed"),
            VocabularyParseErrorKind::ViewAndSpellsCountMisMatch => {
                write!(f, "View and spells count mismatch")
            }
            VocabularyParseErrorKind::InvalidSpellString(s) => {
                write!(f, "Invalid spell string: {}", s)
            }
            VocabularyParseErrorKind::Internal(s) => write!(f, "Internal error: {}", s),
        }
    }
}

impl Error for VocabularyParseError {}

/// Parses a single line into a [`VocabularyEntry`](VocabularyEntry).
///
/// Passed line must follow the format:
/// `word:reading1,reading2,...,readingN`
///
/// - The left-hand side of the colon (`:`) is the word to be displayed.
/// - The right-hand side is a comma-separated list of readings corresponding to each character or grouped characters in the word.
/// - Groups of characters that should be treated as a single unit (e.g., 熟字訓) must be enclosed in square brackets, e.g., `[明日]のジョー:あした,の,じ,ょ,ー`.
/// - Katakana words must use Hiragana for their readings.
/// - English words should be spelled out letter by letter in the reading section.
/// - Small kana characters (e.g., "ょ") count as individual units.
/// - Special characters used for formatting (`:`, `[`, `]`, `,`) can be escaped with a backslash (`\`) to be interpreted literally.
/// - A literal backslash (`\`) must be escaped as a double backslash (`\\`).
/// - If the number of readings does not match the number of character units (taking brackets and escapes into account), the line is considered invalid and an error ([`VocabularyParseError`](VocabularyParseError)) is returned.
///
/// ## Examples
///
/// ```
/// use typing_engine::parse_vocabulary_entry;
///
/// // Valid entries:
///
/// // Basic Japanese example
/// let result = parse_vocabulary_entry("頑張る:がん,ば,る");
/// assert!(result.is_ok());
///
/// // Katakana example
/// let result = parse_vocabulary_entry("タイピング:た,い,ぴ,ん,ぐ");
/// assert!(result.is_ok());
///
/// // Example with compound characters
/// let result = parse_vocabulary_entry("[明日]のジョー:あした,の,じ,ょ,ー");
/// assert!(result.is_ok());
///
/// // English example
/// let result = parse_vocabulary_entry("America:A,m,e,r,i,c,a");
/// assert!(result.is_ok());
///
/// // Example with punctuation
/// let result = parse_vocabulary_entry("メロスは激怒した。:め,ろ,す,は,げき,ど,し,た,。");
/// assert!(result.is_ok());
///
/// // Escaped colon
/// let result = parse_vocabulary_entry(r"a\:b:a,\:,b");
/// assert!(result.is_ok());
/// assert_eq!(result.unwrap().view(), "a:b");
///
/// // Escaped comma in word
/// let result = parse_vocabulary_entry(r"カン,マ:か,ん,\,,ま");
/// //assert!(result.is_ok());
/// assert_eq!(result.unwrap().view(), "カン,マ");
///
/// // Escaped brackets
/// let result = parse_vocabulary_entry(r"\[テスト\]:[,て,す,と,]");
/// assert!(result.is_ok());
/// assert_eq!(result.unwrap().view(), "[テスト]");
///
/// // Escaped backslash
/// let result = parse_vocabulary_entry(r"\\:\\");
/// assert!(result.is_ok());
/// assert_eq!(result.unwrap().view(), r"\");
///
/// // Invalid entries:
///
/// // Extra reading segment
/// let result = parse_vocabulary_entry("頑張る:が,ん,ば,る");
/// assert!(result.is_err());
///
/// // Reading count mismatch due to grouping
/// let result = parse_vocabulary_entry("[明日]の:あ,した,の");
/// assert!(result.is_err());
/// ```
pub fn parse_vocabulary_entry(line: &str) -> Result<VocabularyEntry, VocabularyParseError> {
    if line.lines().count() > 1 {
        return Err(VocabularyParseError::new(
            VocabularyParseErrorKind::MultipleLines,
        ));
    }

    let elements: Vec<String> = split_by_non_escaped_separator(line, ':');

    if elements.len() != 2 {
        return Err(VocabularyParseError::new(
            VocabularyParseErrorKind::ComponentsCountMisMatch,
        ));
    }

    let view = elements.first().unwrap();
    let spells_str = elements.get(1).unwrap();

    let (view, view_parts_counts) = remove_square_parentheses(view)?;
    let spells = split_by_non_escaped_separator(spells_str, ',');

    // Convert two consecutive backslashes in spells
    let spells: Vec<String> = spells
        .iter()
        .map(|spell| convert_two_backslash_to_single(spell))
        .collect();

    if spells.len() != view_parts_counts.len() {
        return Err(VocabularyParseError::new(
            VocabularyParseErrorKind::ViewAndSpellsCountMisMatch,
        ));
    }

    let spells: Vec<VocabularySpellElement> = construct_spell_strings(&spells)?
        .iter()
        .zip(view_parts_counts)
        .map(|(spell, count)| {
            if count == NonZeroUsize::new(1).unwrap() {
                VocabularySpellElement::Normal(spell.clone())
            } else {
                VocabularySpellElement::Compound((spell.clone(), count))
            }
        })
        .collect();

    let vocabulary_entry = VocabularyEntry::new(view.clone(), spells.clone()).ok_or(
        VocabularyParseError::new(VocabularyParseErrorKind::Internal(format!(
            "Failed to create VocabularyEntry for view: {}, spell: {:?}",
            view, spells
        ))),
    )?;

    Ok(vocabulary_entry)
}

// Convert a slice of strings to a list of spell strings
// Returns error if any of the strings are invalid spell strings
fn construct_spell_strings(strs: &[String]) -> Result<Vec<SpellString>, VocabularyParseError> {
    let mut spell_strings = vec![];
    for str in strs {
        if let Ok(spell_string) = SpellString::try_from(str.to_string()) {
            spell_strings.push(spell_string);
        } else {
            return Err(VocabularyParseError::new(
                VocabularyParseErrorKind::InvalidSpellString(str.clone()),
            ));
        }
    }

    Ok(spell_strings)
}

/// Separate passed line into multiple components with passed separator charactor.
/// Backslashed separators are recognized as separator charactor itself.
/// Backslash not followed after separator is retain.
///
/// Ex. When separator is colon ( : ),
/// a:b:c -> (a,b,c)
/// a<bslash>:b:c -> (a:b, c)
/// a<bslash><bslash>:b:c -> (a<bslash>:b, c)
fn split_by_non_escaped_separator(line: &str, separator: char) -> Vec<String> {
    assert_ne!(separator, '\\');

    let mut separated = Vec::<String>::new();
    let mut component = String::new();

    let mut is_prev_escape = false;

    for char in line.chars() {
        if char == separator {
            // Escaped separator is recognized as separator charactor itself
            if is_prev_escape {
                component.push(char);

                is_prev_escape = false;
            } else {
                separated.push(component.clone());
                component.clear();

                is_prev_escape = false;
            }
        } else if char == '\\' {
            if is_prev_escape {
                component.push(char);
                component.push(char);

                is_prev_escape = false;
            } else {
                is_prev_escape = true;
            }
        } else {
            if is_prev_escape {
                // Backslash not followed after separator is retain
                component.push('\\');
            }

            component.push(char);

            is_prev_escape = false;
        }
    }

    // Remained component should be added
    separated.push(component);

    separated
}

/// Removes square brackets ([]) and constructs a count of characters for each group of enclosed content.
/// Backslash-escaped square brackets and backslashes are treated as literal characters.
/// Returns Err if brackets are nested or don't match properly.
/// Other backslashes are left unchanged.
fn remove_square_parentheses(s: &str) -> Result<(String, Vec<NonZeroUsize>), VocabularyParseError> {
    // Construction is done in 2 stages:
    // 1. Remove square brackets while recording the position (start and end indices in the resulting string) of the enclosed parts
    // 2. Based on the positions of the enclosed parts, determine how many characters are in each compound unit
    let mut string = String::new();
    let mut surround_positions = VecDeque::<(usize, usize)>::new();

    let mut is_prev_escape = false;
    let mut i = 0;
    let mut compound_start_i: Option<usize> = None;

    // 1.
    for char in s.chars() {
        if char == '[' {
            if is_prev_escape {
                string.push(char);

                i += 1;
            } else {
                if compound_start_i.is_some() {
                    return Err(VocabularyParseError::new(
                        VocabularyParseErrorKind::CompoundSymbolMisMatch,
                    ));
                }
                compound_start_i.replace(i);
            }
            is_prev_escape = false;
        } else if char == ']' {
            if is_prev_escape {
                string.push(char);

                i += 1;
            } else {
                if let Some(compound_start_i) = compound_start_i {
                    // Empty compound are not allowed
                    if compound_start_i == i {
                        return Err(VocabularyParseError::new(
                            VocabularyParseErrorKind::EmptyCompound,
                        ));
                    }
                    surround_positions.push_back((compound_start_i, i - 1));
                } else {
                    return Err(VocabularyParseError::new(
                        VocabularyParseErrorKind::CompoundSymbolMisMatch,
                    ));
                }

                compound_start_i.take();
            }

            is_prev_escape = false;
        } else if char == '\\' {
            if is_prev_escape {
                string.push(char);
                i += 1;

                is_prev_escape = false;
            } else {
                is_prev_escape = true;
            }
        } else {
            if is_prev_escape {
                string.push('\\');
                i += 1;
            }

            string.push(char);
            i += 1;

            is_prev_escape = false;
        }
    }

    // Half-opened compound symbols are not allowed
    if compound_start_i.is_some() {
        return Err(VocabularyParseError::new(
            VocabularyParseErrorKind::CompoundSymbolMisMatch,
        ));
    }

    // 2.
    let mut character_counts: Vec<NonZeroUsize> = vec![];

    string.chars().enumerate().try_for_each(|(i, _)| {
        let front_position = surround_positions.front();

        if let Some((pos_start_i, pos_end_i)) = front_position {
            if pos_end_i < pos_start_i || i > *pos_end_i {
                return Err(VocabularyParseError::new(
                    VocabularyParseErrorKind::Internal(
                        "Compound symbol position index is corrupted".to_string(),
                    ),
                ));
            }

            if *pos_start_i <= i && i <= *pos_end_i {
                if i == *pos_end_i {
                    let character_count = NonZeroUsize::new(*pos_end_i - *pos_start_i + 1).ok_or(
                        VocabularyParseError::new(VocabularyParseErrorKind::Internal(
                            "charactor count in square parenthes is not NonZeroUsize".to_string(),
                        )),
                    )?;
                    character_counts.push(character_count);
                    surround_positions.pop_front();
                }
            } else {
                character_counts.push(NonZeroUsize::new(1).unwrap());
            }
        } else {
            character_counts.push(NonZeroUsize::new(1).unwrap());
        }

        Ok(())
    })?;

    Ok((string, character_counts))
}

/// Convert two consecutive backslashes into a single one
fn convert_two_backslash_to_single(s: &str) -> String {
    let mut string = String::new();

    let mut is_prev_escape = false;
    for char in s.chars() {
        if char == '\\' {
            if is_prev_escape {
                string.push(char);
                is_prev_escape = false;
            } else {
                is_prev_escape = true;
            }
        } else {
            if is_prev_escape {
                string.push('\\');
            }

            string.push(char);
            is_prev_escape = false;
        }
    }

    if is_prev_escape {
        string.push('\\');
    }

    string
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{VocabularyEntry, VocabularySpellElement};
    use std::num::NonZeroUsize;

    #[test]
    fn split_by_non_escaped_separator_split_empty_string_correctly() {
        let v = split_by_non_escaped_separator("", ':');
        assert_eq!(v, vec![""]);
    }

    #[test]
    fn split_by_non_escaped_separator_split_non_escaped_correctly() {
        let v = split_by_non_escaped_separator(r"hoge:fuga:jojo", ':');
        assert_eq!(
            v,
            vec![
                String::from("hoge"),
                String::from("fuga"),
                String::from("jojo")
            ]
        );
    }

    #[test]
    fn split_by_non_escaped_separator_escape_backslash_correctly() {
        let v = split_by_non_escaped_separator(r"hoge\::fuga", ':');
        assert_eq!(v, vec![String::from("hoge:"), String::from("fuga")]);
    }

    #[test]
    fn split_by_non_escaped_separator_remain_backslash_correctly() {
        let v = split_by_non_escaped_separator(r"h\o\\ge:fuga", ':');
        assert_eq!(v, vec![String::from(r"h\o\\ge"), String::from("fuga")]);
    }

    #[test]
    fn split_by_non_escaped_separator_split_empty_component_correctly() {
        let v = split_by_non_escaped_separator(r"::", ':');
        assert_eq!(
            v,
            vec![String::from(""), String::from(""), String::from("")]
        );
    }

    #[test]
    fn remove_square_parentheses_recognize_count_correctly() {
        assert_eq!(
            remove_square_parentheses(r"a[123]bc"),
            Ok((
                "a123bc".to_string(),
                vec![
                    NonZeroUsize::new(1).unwrap(),
                    NonZeroUsize::new(3).unwrap(),
                    NonZeroUsize::new(1).unwrap(),
                    NonZeroUsize::new(1).unwrap()
                ]
            ))
        );
    }

    #[test]
    fn remove_square_parentheses_recognize_backslashed_backslash_correctly() {
        assert_eq!(
            remove_square_parentheses(r"a\\bc"),
            Ok((
                r"a\bc".to_string(),
                vec![
                    NonZeroUsize::new(1).unwrap(),
                    NonZeroUsize::new(1).unwrap(),
                    NonZeroUsize::new(1).unwrap(),
                    NonZeroUsize::new(1).unwrap()
                ]
            ))
        );
    }

    #[test]
    fn remove_square_parentheses_recognize_backslashed_square_parentheses_correctly() {
        assert_eq!(
            remove_square_parentheses(r"a[\[123\]]b\[\]"),
            Ok((
                "a[123]b[]".to_string(),
                vec![
                    NonZeroUsize::new(1).unwrap(),
                    NonZeroUsize::new(5).unwrap(),
                    NonZeroUsize::new(1).unwrap(),
                    NonZeroUsize::new(1).unwrap(),
                    NonZeroUsize::new(1).unwrap()
                ]
            ))
        );
    }

    #[test]
    fn remove_square_parentheses_remain_backslash_not_following_special_charactors_correctly() {
        assert_eq!(
            remove_square_parentheses(r"a\bc"),
            Ok((
                r"a\bc".to_string(),
                vec![
                    NonZeroUsize::new(1).unwrap(),
                    NonZeroUsize::new(1).unwrap(),
                    NonZeroUsize::new(1).unwrap(),
                    NonZeroUsize::new(1).unwrap()
                ]
            ))
        );
    }

    #[test]
    fn remove_square_parentheses_returns_err_when_nesting() {
        assert_eq!(
            remove_square_parentheses(r"[[]]"),
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::CompoundSymbolMisMatch,
            ))
        );
    }

    #[test]
    fn remove_square_parentheses_returns_err_when_compound_is_closed_without_opened() {
        assert_eq!(
            remove_square_parentheses(r"a]bdf\["),
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::CompoundSymbolMisMatch,
            ))
        );
    }

    #[test]
    fn remove_square_parentheses_returns_err_when_compound_is_not_closed() {
        assert_eq!(
            remove_square_parentheses(r"a[bdf\["),
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::CompoundSymbolMisMatch,
            ))
        );
    }

    #[test]
    fn remove_square_parentheses_returns_err_when_compound_is_empty() {
        assert_eq!(
            remove_square_parentheses(r"[]"),
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::EmptyCompound,
            ))
        );
    }

    #[test]
    fn convert_two_backslash_to_single_convert_two_backslashes_correctly() {
        assert_eq!(convert_two_backslash_to_single(r"\\"), r"\");
    }

    #[test]
    fn convert_two_backslash_to_single_not_convert_third_backslash_with_following_charactor() {
        assert_eq!(convert_two_backslash_to_single(r"\\\a"), r"\\a");
    }

    #[test]
    fn convert_two_backslash_to_single_not_convert_third_backslash_without_following_charactor() {
        assert_eq!(convert_two_backslash_to_single(r"\\\"), r"\\");
    }

    #[test]
    fn convert_two_backslash_to_single_convert_four_backshasled_totwo_backslashes() {
        assert_eq!(convert_two_backslash_to_single(r"\\\\"), r"\\");
    }

    #[test]
    fn parse_vocabulary_entry_success_normal() {
        let result = parse_vocabulary_entry("頑張る:がん,ば,る");

        assert_eq!(
            result,
            Ok(VocabularyEntry::new(
                "頑張る".to_string(),
                vec![
                    VocabularySpellElement::Normal("がん".to_string().try_into().unwrap()),
                    VocabularySpellElement::Normal("".to_string().try_into().unwrap()),
                    VocabularySpellElement::Normal("".to_string().try_into().unwrap())
                ]
            )
            .unwrap())
        );
    }

    #[test]
    fn parse_vocabulary_entry_success_with_compound() {
        let result =
            parse_vocabulary_entry("[昨日]の敵は[今日]の友:きのう,の,てき,は,きょう,の,とも");

        assert_eq!(
            result,
            Ok(VocabularyEntry::new(
                "昨日の敵は今日の友".to_string(),
                vec![
                    VocabularySpellElement::Compound((
                        "きのう".to_string().try_into().unwrap(),
                        NonZeroUsize::new(2).unwrap()
                    )),
                    VocabularySpellElement::Normal("".to_string().try_into().unwrap()),
                    VocabularySpellElement::Normal("てき".to_string().try_into().unwrap()),
                    VocabularySpellElement::Normal("".to_string().try_into().unwrap()),
                    VocabularySpellElement::Compound((
                        "きょう".to_string().try_into().unwrap(),
                        NonZeroUsize::new(2).unwrap()
                    )),
                    VocabularySpellElement::Normal("".to_string().try_into().unwrap()),
                    VocabularySpellElement::Normal("とも".to_string().try_into().unwrap()),
                ]
            )
            .unwrap())
        );
    }

    #[test]
    fn parse_vocabulary_entry_success_with_escaped_characters() {
        let result = parse_vocabulary_entry(r"\\\::\\,\:");

        assert_eq!(
            result,
            Ok(VocabularyEntry::new(
                r"\:".to_string(),
                vec![
                    VocabularySpellElement::Normal(r"\".to_string().try_into().unwrap()),
                    VocabularySpellElement::Normal(":".to_string().try_into().unwrap()),
                ]
            )
            .unwrap())
        );
    }

    #[test]
    fn parse_vocabulary_entry_success_with_escaped_brackets() {
        let result = parse_vocabulary_entry(r"[\[]12:[,1,2");

        assert_eq!(
            result,
            Ok(VocabularyEntry::new(
                "[12".to_string(),
                vec![
                    VocabularySpellElement::Normal(r"[".to_string().try_into().unwrap()),
                    VocabularySpellElement::Normal("1".to_string().try_into().unwrap()),
                    VocabularySpellElement::Normal("2".to_string().try_into().unwrap()),
                ]
            )
            .unwrap())
        );
    }

    #[test]
    fn parse_vocabulary_entry_error_multiple_lines() {
        let result = parse_vocabulary_entry("頑張る:がん,ば,る\n頑張る:がんば,る");

        assert_eq!(
            result,
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::MultipleLines
            ))
        );
    }

    #[test]
    fn parse_vocabulary_entry_error_components_count_mismatch_no_colon() {
        let result = parse_vocabulary_entry("頑張る");

        assert_eq!(
            result,
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::ComponentsCountMisMatch
            ))
        );
    }

    #[test]
    fn parse_vocabulary_entry_error_components_count_mismatch_too_many_colons() {
        let result = parse_vocabulary_entry("頑:張:る:がん,ば,る");

        assert_eq!(
            result,
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::ComponentsCountMisMatch
            ))
        );
    }

    #[test]
    fn parse_vocabulary_entry_error_compound_symbol_mismatch_unclosed() {
        let result = parse_vocabulary_entry("[頑張る:がん,ば,る");

        assert_eq!(
            result,
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::CompoundSymbolMisMatch
            ))
        );
    }

    #[test]
    fn parse_vocabulary_entry_error_compound_symbol_mismatch_unopened() {
        let result = parse_vocabulary_entry("頑張る]:がん,ば,る");

        assert_eq!(
            result,
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::CompoundSymbolMisMatch
            ))
        );
    }

    #[test]
    fn parse_vocabulary_entry_error_compound_symbol_mismatch_nested() {
        let result = parse_vocabulary_entry("[[頑張る]]:がん,ば,る");

        assert_eq!(
            result,
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::CompoundSymbolMisMatch
            ))
        );
    }

    #[test]
    fn parse_vocabulary_entry_error_empty_compound() {
        let result = parse_vocabulary_entry("頑張[]る:がん,ば,る");

        assert_eq!(
            result,
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::EmptyCompound
            ))
        );
    }

    #[test]
    fn parse_vocabulary_entry_error_view_and_spells_count_mismatch_too_few_spells() {
        let result = parse_vocabulary_entry("頑張る:がん,ば");

        assert_eq!(
            result,
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::ViewAndSpellsCountMisMatch
            ))
        );
    }

    #[test]
    fn parse_vocabulary_entry_error_view_and_spells_count_mismatch_too_many_spells() {
        let result = parse_vocabulary_entry("頑張る:がん,ば,る,よ");

        assert_eq!(
            result,
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::ViewAndSpellsCountMisMatch
            ))
        );
    }

    #[test]
    fn parse_vocabulary_entry_error_view_and_spells_count_mismatch_with_compound() {
        let result = parse_vocabulary_entry("[今日]の:きょ,う,の");

        assert_eq!(
            result,
            Err(VocabularyParseError::new(
                VocabularyParseErrorKind::ViewAndSpellsCountMisMatch
            ))
        );
    }

    #[test]
    fn parse_vocabulary_entry_error_invalid_spell_string() {
        let result = parse_vocabulary_entry("頑張る:がん,ば,る三");

        match result {
            Err(VocabularyParseError {
                kind: VocabularyParseErrorKind::InvalidSpellString(_),
            }) => assert!(true),
            _ => assert!(false, "Expected InvalidSpellString error, got {:?}", result),
        }
    }
}