yosina 0.2.0

Japanese text transliteration library
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
use itertools::Itertools as _;
use serde::{Deserialize, Serialize};

use crate::transliterator::{Transliterator, TransliteratorFactory, TransliteratorFactoryError};
use crate::transliterators::{
    Charset, HyphensTransliterationVariant, IvsSvsBaseMode, IvsSvsBaseTransliteratorOptions,
    TransliteratorConfig,
};

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct TransliteratorConfigBuilder {
    head: Vec<TransliteratorConfig>,
    tail: Vec<TransliteratorConfig>,
}

impl TransliteratorConfigBuilder {
    pub fn new() -> Self {
        TransliteratorConfigBuilder {
            head: Vec::new(),
            tail: Vec::new(),
        }
    }

    pub fn find_in_head(&self, config: &TransliteratorConfig) -> Option<usize> {
        self.head
            .iter()
            .position(|c| std::mem::discriminant(c) == std::mem::discriminant(config))
    }

    pub fn find_in_tail(&self, config: &TransliteratorConfig) -> Option<usize> {
        self.tail
            .iter()
            .position(|c| std::mem::discriminant(c) == std::mem::discriminant(config))
    }

    pub fn insert_head(&self, config: TransliteratorConfig, force_replace: bool) -> Self {
        if let Some(i) = self.find_in_head(&config) {
            if force_replace {
                let mut new_head = self.head.clone();
                new_head[i] = config;
                TransliteratorConfigBuilder {
                    head: new_head,
                    tail: self.tail.clone(),
                }
            } else {
                self.clone()
            }
        } else {
            let mut new_head = self.head.clone();
            new_head.insert(0, config);
            TransliteratorConfigBuilder {
                head: new_head,
                tail: self.tail.clone(),
            }
        }
    }

    pub fn insert_middle(&self, config: TransliteratorConfig, force_replace: bool) -> Self {
        if let Some(i) = self.find_in_tail(&config) {
            if force_replace {
                let mut new_tail = self.tail.clone();
                new_tail[i] = config;
                TransliteratorConfigBuilder {
                    head: self.head.clone(),
                    tail: new_tail,
                }
            } else {
                self.clone()
            }
        } else {
            let mut new_tail = self.tail.clone();
            new_tail.insert(0, config);
            TransliteratorConfigBuilder {
                head: self.head.clone(),
                tail: new_tail,
            }
        }
    }

    pub fn insert_tail(&self, config: TransliteratorConfig, force_replace: bool) -> Self {
        if let Some(i) = self.find_in_tail(&config) {
            if force_replace {
                let mut new_tail = self.tail.clone();
                new_tail[i] = config;
                TransliteratorConfigBuilder {
                    head: self.head.clone(),
                    tail: new_tail,
                }
            } else {
                self.clone()
            }
        } else {
            let mut new_tail = self.tail.clone();
            new_tail.push(config);
            TransliteratorConfigBuilder {
                head: self.head.clone(),
                tail: new_tail,
            }
        }
    }

    pub fn build(self) -> Vec<TransliteratorConfig> {
        let mut configs = self.head;
        configs.extend(self.tail);
        configs
    }
}

impl Default for TransliteratorConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub enum ToFullWidthOptions {
    #[default]
    No,
    Yes {
        u005c_as_yen_sign: bool,
    },
}

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub enum ToHalfwidthOptions {
    #[default]
    No,
    Yes {
        hankaku_kana: bool,
    },
}

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub enum RemoveIVSSVSOptions {
    #[default]
    No,
    Yes {
        drop_all_selectors: bool,
    },
}

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub enum ReplaceCircledOrSquaredCharactersOptions {
    #[default]
    No,
    Yes {
        exclude_emojis: bool,
    },
}

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub enum ReplaceHyphensOptions {
    #[default]
    No,
    Yes {
        precedence: Vec<HyphensTransliterationVariant>,
    },
}

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub enum HiraKataOptions {
    #[default]
    No,
    #[serde(rename = "hira-to-kata")]
    HiraToKata,
    #[serde(rename = "kata-to-hira")]
    KataToHira,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TransliterationRecipe {
    /// Replace codepoints that correspond to old-style kanji glyphs (旧字体; kyu-ji-tai) with their modern equivalents (新字体; shin-ji-tai).
    ///
    /// # Example
    /// ```text
    /// Input:  "舊字體の變換"
    /// Output: "旧字体の変換"
    /// ```
    #[serde(default)]
    pub kanji_old_new: bool,
    /// Convert between hiragana and katakana scripts.
    ///
    /// # Example
    /// ```text
    /// Input:  "ひらがな" (with HiraToKata)
    /// Output: "ヒラガナ"
    /// ```
    /// ```text
    /// Input:  "カタカナ" (with KataToHira)
    /// Output: "かたかな"
    /// ```
    #[serde(default)]
    pub hira_kata: HiraKataOptions,
    /// Replace Japanese iteration marks with the characters they represent.
    ///
    /// # Example
    /// ```text
    /// Input:  "時々"
    /// Output: "時時"
    /// ```
    /// ```text
    /// Input:  "いすゞ"
    /// Output: "いすず"
    /// ```
    #[serde(default)]
    pub replace_japanese_iteration_marks: bool,
    /// Replace "suspicious" hyphens with prolonged sound marks, and vice versa.
    ///
    /// # Example
    /// ```text
    /// Input:  "スーパ-" (with hyphen-minus)
    /// Output: "スーパー" (becomes prolonged sound mark)
    /// ```
    #[serde(default)]
    pub replace_suspicious_hyphens_to_prolonged_sound_marks: bool,
    /// Replace combined characters with their corresponding characters.
    ///
    /// # Example
    /// ```text
    /// Input:  "㍻" (single character for Heisei era)
    /// Output: "平成"
    /// ```
    /// ```text
    /// Input:  "㈱"
    /// Output: "(株)"
    /// ```
    #[serde(default)]
    pub replace_combined_characters: bool,
    /// Replace circled or squared characters with their corresponding templates.
    ///
    /// # Example
    /// ```text
    /// Input:  "①②③"
    /// Output: "(1)(2)(3)"
    /// ```
    /// ```text
    /// Input:  "㊙㊗"
    /// Output: "(秘)(祝)"
    /// ```
    #[serde(default)]
    pub replace_circled_or_squared_characters: ReplaceCircledOrSquaredCharactersOptions,
    /// Replace ideographic annotations used in the traditional method of Chinese-to-Japanese translation deviced in the ancient Japan.
    ///
    /// # Example
    /// ```text
    /// Input:  "㆖㆘" (ideographic annotations)
    /// Output: "上下"
    /// ```
    #[serde(default)]
    pub replace_ideographic_annotations: bool,
    /// Replace codepoints for the Kang Xi radicals whose glyphs resemble those of CJK ideographs with the CJK ideograph counterparts.
    ///
    /// # Example
    /// ```text
    /// Input:  "⾔⾨⾷" (Kangxi radicals)
    /// Output: "言門食" (CJK ideographs)
    /// ```
    #[serde(default)]
    pub replace_radicals: bool,
    /// Replace various space characters with plain whitespaces or empty strings.
    ///
    /// # Example
    /// ```text
    /// Input:  "A B" (ideographic space U+3000)
    /// Output: "A B" (half-width space)
    /// ```
    /// ```text
    /// Input:  "A B" (non-breaking space U+00A0)
    /// Output: "A B" (regular space)
    /// ```
    #[serde(default)]
    pub replace_spaces: bool,
    /// Replace various dash or hyphen symbols with those common in Japanese writing.
    ///
    /// # Example
    /// ```text
    /// Input:  "2019—2020" (em dash)
    /// Output: "2019-2020" (hyphen-minus)
    /// ```
    /// ```text
    /// Input:  "A–B" (en dash)
    /// Output: "A-B"
    /// ```
    #[serde(default)]
    pub replace_hyphens: ReplaceHyphensOptions,
    /// Replace mathematical alphanumerics with their plain ASCII equivalents.
    ///
    /// # Example
    /// ```text
    /// Input:  "𝐀𝐁𝐂" (mathematical bold)
    /// Output: "ABC"
    /// ```
    /// ```text
    /// Input:  "𝟏𝟐𝟑" (mathematical bold digits)
    /// Output: "123"
    /// ```
    pub replace_mathematical_alphanumerics: bool,
    /// Replace Roman numeral characters with their ASCII equivalents.
    ///
    /// # Example
    /// ```text
    /// Input:  "Chapter Ⅻ"
    /// Output: "Chapter XII"
    /// ```
    /// ```text
    /// Input:  "section ⅲ"
    /// Output: "section iii"
    /// ```
    #[serde(default)]
    pub replace_roman_numerals: bool,
    /// Combine decomposed hiraganas and katakanas into single counterparts.
    ///
    /// # Example
    /// ```text
    /// Input:  "が" (か + ゙)
    /// Output: "が" (single character)
    /// ```
    /// ```text
    /// Input:  "ヘ゜" (ヘ + ゜)
    /// Output: "ペ" (single character)
    /// ```
    #[serde(default)]
    pub combine_decomposed_hiraganas_and_katakanas: bool,
    /// Replace half-width characters in that they are marked as half-width or ambiguous in the East Asian Width table to the fullwidth equivalents.
    /// Specify `"u005c-as-yen-sign"` instead of a bool to treat the backslash character (U+005C) as the yen sign in JIS X 0201.
    ///
    /// # Example
    /// ```text
    /// Input:  "ABC123"
    /// Output: "ABC123"
    /// ```
    /// ```text
    /// Input:  "カタカナ"
    /// Output: "カタカナ"
    /// ```
    #[serde(default)]
    pub to_fullwidth: ToFullWidthOptions,
    /// Replace full-width characters with their half-width equivalents.
    /// Specify `"hankaku-kana"` instead of a bool to handle half-width katakanas too.
    ///
    /// # Example
    /// ```text
    /// Input:  "ABC123"
    /// Output: "ABC123"
    /// ```
    /// ```text
    /// Input:  "カタカナ" (with hankaku-kana)
    /// Output: "カタカナ"
    /// ```
    #[serde(default)]
    pub to_halfwidth: ToHalfwidthOptions,
    /// Replace CJK ideographs followed by IVSes and SVSes with those without selectors based on Adobe-Japan1 character mappings.
    /// Specify `Yes { drop_all_selectors: true }` to get rid of all selectors from the result.
    ///
    /// # Example
    /// ```text
    /// Input:  "葛󠄀" (葛 + IVS U+E0100)
    /// Output: "葛" (without selector)
    /// ```
    /// ```text
    /// Input:  "辻󠄀" (辻 + IVS)
    /// Output: "辻"
    /// ```
    #[serde(default)]
    pub remove_ivs_svs: RemoveIVSSVSOptions,
    /// Assumed charset for IVS/SVS base transliterator
    #[serde(default)]
    pub charset: Charset,
}

#[derive(thiserror::Error, Debug)]
pub enum TransliterationRecipeError {
    #[error("kanji_old_new: {}", .0.join(", "))]
    KanjiOldNewError(Vec<String>),
    #[error("replace_suspicious_hyphens_to_prolonged_sound_marks: {}", .0.join(", "))]
    ReplaceSuspiciousHyphensError(Vec<String>),
    #[error("replace_combined_characters: {}", .0.join(", "))]
    ReplaceCombinedCharactersError(Vec<String>),
    #[error("replace_circled_or_squared_characters: {}", .0.join(", "))]
    ReplaceCircledOrSquaredCharactersError(Vec<String>),
    #[error("replace_ideographic_annotations: {}", .0.join(", "))]
    ReplaceIdeographicAnnotationsError(Vec<String>),
    #[error("replace_radicals: {}", .0.join(", "))]
    ReplaceRadicalsError(Vec<String>),
    #[error("replace_spaces: {}", .0.join(", "))]
    ReplaceSpacesError(Vec<String>),
    #[error("replace_hyphens: {}", .0.join(", "))]
    ReplaceHyphensError(Vec<String>),
    #[error("replace_mathematical_alphanumerics: {}", .0.join(", "))]
    ReplaceMathematicalAlphanumericsError(Vec<String>),
    #[error("combine_decomposed_hiraganas_and_katakanas: {}", .0.join(", "))]
    CombineDecomposedHiraganasAndKatakanasError(Vec<String>),
    #[error("to_fullwidth: {}", .0.join(", "))]
    ToFullwidthError(Vec<String>),
    #[error("to_halfwidth: {}", .0.join(", "))]
    ToHalfwidthError(Vec<String>),
    #[error("remove_ivs_svs: {}", .0.join(", "))]
    RemoveIVSSVSError(Vec<String>),
}

impl TransliterationRecipe {
    fn remove_ivs_svs_helper(
        &self,
        mut builder: TransliteratorConfigBuilder,
        drop_selectors_altogether: bool,
    ) -> TransliteratorConfigBuilder {
        // First insert "ivs-or-svs" mode at head
        builder = builder.insert_head(
            TransliteratorConfig::IvsSvsBase(IvsSvsBaseTransliteratorOptions {
                mode: IvsSvsBaseMode::IvsOrSvs(false),
                charset: self.charset,
                ..Default::default()
            }),
            true,
        );

        // Then insert "base" mode at tail
        builder.insert_tail(
            TransliteratorConfig::IvsSvsBase(IvsSvsBaseTransliteratorOptions {
                mode: IvsSvsBaseMode::Base,
                charset: self.charset,
                drop_selectors_altogether,
            }),
            true,
        )
    }

    fn apply_kanji_old_new(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if self.kanji_old_new {
            builder = self.remove_ivs_svs_helper(builder, false);
            builder = builder.insert_middle(TransliteratorConfig::KanjiOldNew, false);
        }
        (builder, None)
    }

    fn apply_hira_kata(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        match &self.hira_kata {
            HiraKataOptions::HiraToKata => {
                builder = builder.insert_middle(
                    TransliteratorConfig::HiraKata(
                        crate::transliterators::HiraKataTransliteratorOptions {
                            mode: crate::transliterators::HiraKataMode::HiraToKata,
                        },
                    ),
                    false,
                );
            }
            HiraKataOptions::KataToHira => {
                builder = builder.insert_middle(
                    TransliteratorConfig::HiraKata(
                        crate::transliterators::HiraKataTransliteratorOptions {
                            mode: crate::transliterators::HiraKataMode::KataToHira,
                        },
                    ),
                    false,
                );
            }
            HiraKataOptions::No => {}
        }
        (builder, None)
    }

    fn apply_replace_japanese_iteration_marks(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if self.replace_japanese_iteration_marks {
            // Insert HiraKataComposition at head to ensure composed forms
            builder = builder.insert_head(
                TransliteratorConfig::HiraKataComposition(
                    crate::transliterators::HiraKataCompositionTransliteratorOptions {
                        compose_non_combining_marks: true,
                    },
                ),
                false,
            );
            // Then insert the japanese-iteration-marks in the middle
            builder = builder.insert_middle(
                TransliteratorConfig::JapaneseIterationMarks(
                    crate::transliterators::JapaneseIterationMarksTransliteratorOptions::default(),
                ),
                false,
            );
        }
        (builder, None)
    }

    fn apply_replace_suspicious_hyphens_to_prolonged_sound_marks(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if self.replace_suspicious_hyphens_to_prolonged_sound_marks {
            builder = builder.insert_middle(
                TransliteratorConfig::ProlongedSoundMarks(
                    crate::transliterators::ProlongedSoundMarksTransliteratorOptions {
                        replace_prolonged_marks_following_alnums: true,
                        ..Default::default()
                    },
                ),
                false,
            );
        }
        (builder, None)
    }

    fn apply_replace_ideographic_annotations(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if self.replace_ideographic_annotations {
            builder = builder.insert_middle(TransliteratorConfig::IdeographicAnnotations, false);
        }
        (builder, None)
    }

    fn apply_replace_radicals(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if self.replace_radicals {
            builder = builder.insert_middle(TransliteratorConfig::Radicals, false);
        }
        (builder, None)
    }

    fn apply_replace_spaces(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if self.replace_spaces {
            builder = builder.insert_middle(TransliteratorConfig::Spaces, false);
        }
        (builder, None)
    }

    fn apply_replace_hyphens(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if let ReplaceHyphensOptions::Yes { precedence } = &self.replace_hyphens {
            let precedence = if precedence.is_empty() {
                vec![
                    HyphensTransliterationVariant::Jisx0208_90Windows,
                    HyphensTransliterationVariant::Jisx0201,
                ]
            } else {
                precedence.clone()
            };
            builder = builder.insert_middle(
                TransliteratorConfig::Hyphens(
                    crate::transliterators::HyphensTransliteratorOptions { precedence },
                ),
                false,
            );
        }
        (builder, None)
    }

    fn apply_replace_mathematical_alphanumerics(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if self.replace_mathematical_alphanumerics {
            builder = builder.insert_middle(TransliteratorConfig::MathematicalAlphanumerics, false);
        }
        (builder, None)
    }

    fn apply_replace_roman_numerals(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if self.replace_roman_numerals {
            builder = builder.insert_middle(TransliteratorConfig::RomanNumerals, false);
        }
        (builder, None)
    }

    fn apply_combine_decomposed_hiraganas_and_katakanas(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if self.combine_decomposed_hiraganas_and_katakanas {
            builder = builder.insert_head(
                TransliteratorConfig::HiraKataComposition(
                    crate::transliterators::HiraKataCompositionTransliteratorOptions {
                        compose_non_combining_marks: true,
                    },
                ),
                false,
            );
        }
        (builder, None)
    }

    fn apply_to_fullwidth(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if let ToFullWidthOptions::Yes { u005c_as_yen_sign } = self.to_fullwidth {
            if self.to_halfwidth != ToHalfwidthOptions::No {
                return (
                    builder,
                    Some(TransliterationRecipeError::ToFullwidthError(vec![
                        "to_fullwidth and to_halfwidth are mutually exclusive".to_string(),
                    ])),
                );
            }

            builder = builder.insert_tail(
                TransliteratorConfig::Jisx0201AndAlike {
                    fullwidth_to_halfwidth: false,
                    options: crate::transliterators::Jisx0201AndAlikeTransliteratorOptions {
                        u005c_as_yen_sign: Some(u005c_as_yen_sign),
                        ..Default::default()
                    },
                },
                false,
            );
        }
        (builder, None)
    }

    fn apply_to_halfwidth(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if let ToHalfwidthOptions::Yes { hankaku_kana } = self.to_halfwidth {
            if self.to_fullwidth != ToFullWidthOptions::No {
                return (
                    builder,
                    Some(TransliterationRecipeError::ToHalfwidthError(vec![
                        "to_halfwidth and to_fullwidth are mutually exclusive".to_string(),
                    ])),
                );
            }

            builder = builder.insert_tail(
                TransliteratorConfig::Jisx0201AndAlike {
                    fullwidth_to_halfwidth: true,
                    options: crate::transliterators::Jisx0201AndAlikeTransliteratorOptions {
                        convert_gl: true,
                        convert_gr: hankaku_kana,
                        ..Default::default()
                    },
                },
                false,
            );
        }
        (builder, None)
    }

    fn apply_remove_ivs_svs(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if let RemoveIVSSVSOptions::Yes { drop_all_selectors } = self.remove_ivs_svs {
            builder = self.remove_ivs_svs_helper(builder, drop_all_selectors);
        }
        (builder, None)
    }

    fn apply_replace_combined_characters(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if self.replace_combined_characters {
            builder = builder.insert_middle(TransliteratorConfig::Combined, false);
        }
        (builder, None)
    }

    fn apply_replace_circled_or_squared_characters(
        &self,
        mut builder: TransliteratorConfigBuilder,
    ) -> (
        TransliteratorConfigBuilder,
        Option<TransliterationRecipeError>,
    ) {
        if let ReplaceCircledOrSquaredCharactersOptions::Yes { exclude_emojis } =
            self.replace_circled_or_squared_characters
        {
            builder = builder.insert_middle(
                TransliteratorConfig::CircledOrSquared(
                    crate::transliterators::CircledOrSquaredTransliteratorOptions {
                        include_emojis: !exclude_emojis,
                    },
                ),
                false,
            );
        }
        (builder, None)
    }

    /// Build transliterator configs from a recipe
    pub fn build(&self) -> Result<Vec<TransliteratorConfig>, TransliteratorFactoryError> {
        // Check for mutually exclusive options
        let builder = TransliteratorConfigBuilder::new();
        let mut errors = Vec::<TransliterationRecipeError>::new();
        // Apply transliterators in the specified order
        // 1. kanjiOldNew
        let (builder, e) = self.apply_kanji_old_new(builder);
        if let Some(e) = e {
            errors.push(e)
        }

        // 2. replaceSuspiciousHyphensToProlongedSoundMarks
        let (builder, e) = self.apply_replace_suspicious_hyphens_to_prolonged_sound_marks(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 3. replaceCircledOrSquaredCharacters
        let (builder, e) = self.apply_replace_circled_or_squared_characters(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 4. replaceCombinedCharacters
        let (builder, e) = self.apply_replace_combined_characters(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 5. replaceIdeographicAnnotations
        let (builder, e) = self.apply_replace_ideographic_annotations(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 6. replaceRadicals
        let (builder, e) = self.apply_replace_radicals(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 7. replaceSpaces
        let (builder, e) = self.apply_replace_spaces(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 8. replaceHyphens
        let (builder, e) = self.apply_replace_hyphens(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 9. replaceMathematicalAlphanumerics
        let (builder, e) = self.apply_replace_mathematical_alphanumerics(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 10. replaceRomanNumerals
        let (builder, e) = self.apply_replace_roman_numerals(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 11. combineDecomposedHiraganasAndKatakanas
        let (builder, e) = self.apply_combine_decomposed_hiraganas_and_katakanas(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 11. toFullwidth
        let (builder, e) = self.apply_to_fullwidth(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 12. hiraKata
        let (builder, e) = self.apply_hira_kata(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 13. replaceJapaneseIterationMarks
        let (builder, e) = self.apply_replace_japanese_iteration_marks(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 14. toHalfwidth
        let (builder, e) = self.apply_to_halfwidth(builder);
        if let Some(e) = e {
            errors.push(e);
        }

        // 15. removeIVSSVS
        let (builder, e) = self.apply_remove_ivs_svs(builder);
        if let Some(e) = e {
            errors.push(e);
        }
        if !errors.is_empty() {
            return Err(TransliteratorFactoryError::new(
                errors.iter().map(|e| e.to_string()).join("; "),
            ));
        }
        Ok(builder.build())
    }
}

impl TryFrom<&TransliterationRecipe> for Vec<TransliteratorConfig> {
    type Error = TransliteratorFactoryError;
    fn try_from(value: &TransliterationRecipe) -> Result<Vec<TransliteratorConfig>, Self::Error> {
        value.build()
    }
}

impl Default for TransliterationRecipe {
    fn default() -> Self {
        Self {
            kanji_old_new: false,
            hira_kata: HiraKataOptions::No,
            replace_japanese_iteration_marks: false,
            replace_suspicious_hyphens_to_prolonged_sound_marks: false,
            replace_combined_characters: false,
            replace_circled_or_squared_characters: ReplaceCircledOrSquaredCharactersOptions::No,
            replace_ideographic_annotations: false,
            replace_radicals: false,
            replace_spaces: false,
            replace_hyphens: ReplaceHyphensOptions::No,
            replace_mathematical_alphanumerics: false,
            replace_roman_numerals: false,
            combine_decomposed_hiraganas_and_katakanas: false,
            to_fullwidth: ToFullWidthOptions::No,
            to_halfwidth: ToHalfwidthOptions::No,
            remove_ivs_svs: RemoveIVSSVSOptions::No,
            charset: Charset::Unijis2004,
        }
    }
}

impl TransliteratorFactory for TransliterationRecipe {
    fn new_transliterator(&self) -> Result<Box<dyn Transliterator>, TransliteratorFactoryError> {
        self.build()?.new_transliterator()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::char::{from_chars, CharPool};
    use crate::transliterators::{
        HiraKataCompositionTransliteratorOptions, ProlongedSoundMarksTransliteratorOptions,
    };

    #[test]
    fn test_recipe_build_basic() {
        let recipe = TransliterationRecipe {
            kanji_old_new: true,
            replace_spaces: true,
            ..Default::default()
        };

        let result = recipe.build();
        assert!(result.is_ok(), "Recipe build should succeed");

        let configs = result.unwrap();
        assert!(!configs.is_empty(), "Should have at least one config");
    }

    #[test]
    fn test_mutual_exclusion() {
        let recipe = TransliterationRecipe {
            to_fullwidth: ToFullWidthOptions::Yes {
                u005c_as_yen_sign: false,
            },
            to_halfwidth: ToHalfwidthOptions::Yes {
                hankaku_kana: false,
            },
            ..Default::default()
        };

        let result = recipe.build();
        assert!(
            result.is_err(),
            "Should error on mutually exclusive options"
        );
    }

    #[test]
    fn test_vec_transliterator_config_factory() {
        let configs = vec![
            TransliteratorConfig::HiraKataComposition(
                HiraKataCompositionTransliteratorOptions::default(),
            ),
            TransliteratorConfig::ProlongedSoundMarks(
                ProlongedSoundMarksTransliteratorOptions::default(),
            ),
        ];

        let factory_result = configs.new_transliterator();
        assert!(factory_result.is_ok());

        let transliterator = factory_result.unwrap();

        // Since all current implementations are stubs, this should preserve input
        let test_cases = &[("test", "test"), ("こんにちは", "こんにちは"), ("", "")];

        // Test each case
        for (input, expected) in test_cases {
            let mut pool = CharPool::new();
            let input_chars = pool.build_char_array(input);
            let result = transliterator
                .transliterate(&mut pool, &input_chars)
                .unwrap();
            let result_string = from_chars(result.iter().cloned());
            assert_eq!(result_string, *expected, "Failed for input '{input}'");
        }
    }

    #[test]
    fn test_vec_transliterator_config_empty_error() {
        let configs: Vec<TransliteratorConfig> = vec![];
        let factory_result = configs.new_transliterator();
        assert!(factory_result.is_err());
    }
}