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
extern crate num;

mod numbers;
mod place;
mod block;

use std::cmp::{min};
use std::cmp::Ord;
use num::{BigInt, FromPrimitive, PrimInt};

/// Parses an int into a Hangeul String.
///
/// ```md
/// Args:
/// *input* - Any {integer}.
/// *is_sino* - true  => parse as a Sino-Korean number.
///             false => parse as a Pure-Korean number.
/// ```
pub fn hangeul_from_int<T>(input: T, is_sino: bool) -> String
    where T: PrimInt + ToString + FromPrimitive
{
    validate(input, is_sino);
    let prepared_input = prepare_input(input);
    match is_sino {
        true => parse_hangeul_sino(prepared_input),
        false => parse_hangeul_pure(prepared_input),
    }
}

/// Parses an int String into a Hangeul String.
///
/// ```md
/// Args:
/// *input* - A String that can be parsed to an {integer}.
/// *is_sino* - true  => parse as a Sino-Korean number.
///             false => parse as a Pure-Korean number.
/// ```
pub fn hangeul_from_string(input: String, is_sino: bool) -> String {
    hangeul_from_int(input.parse::<u64>().unwrap(), is_sino)
}

/// Parses a BigInt into a Hangeul String.
///
/// ```md
/// Args:
/// *input* - A BigInt.
/// ```
pub fn hangeul_from_bigint(input: BigInt) -> String
{
    if input < FromPrimitive::from_i8(0).unwrap() {
        panic!("Input cannot be negative.")
    }
    let prepared_input = input
        .to_string()
        .replace(",", "")
        .chars()
        .rev()
        .collect();

    parse_hangeul_sino(prepared_input)
}

fn validate<T>(input: T, is_sino: bool)
    where T: PrimInt + ToString + FromPrimitive
{
    if is_sino == false && input > FromPrimitive::from_u64(99).unwrap() {
        panic!("Pure korean numbers only go up to 99.");
    }
    if input < FromPrimitive::from_u64(0).unwrap() {
        panic!("Input cannot be negative.")
    }
}

fn prepare_input<T>(input: T) -> Vec<char>
    where T: PrimInt + ToString + Ord
{
    let nums = input
        .to_string()
        .replace(",", "")
        .chars()
        .rev()
        .collect();

    nums
}

fn parse_hangeul_sino(numbers: Vec<char>) -> String {
    let len = numbers.len() - 1;
    let mut output = String::new();
    let mut iter = numbers.iter().enumerate();

    while let Some((idx, input_num)) = iter.next() {
        let remaining = len - idx;
        let min_peek_len = min(remaining, 3);
        let mut num = numbers::KoreanNumberSino::from_char(input_num).unwrap();

        if min_peek_len != 0 && num == 0 {
            let mut zeroes = 1;
            while let Some((_, next_num)) = iter.next() {
                if *next_num != '0' {
                    num = numbers::KoreanNumberSino::from_char(next_num).unwrap();
                    break;
                }
                zeroes += 1;
            }

            if let Some(block) = block::Block::from_usize(zeroes) {
                output.push_str(block.to_str());
                if num != 1 {
                    output.push_str(num.to_str());
                }
                continue;
            } else {
                let zmod = zeroes % 4;
                if zeroes >= 4 {
                    let block = block::Block::from_usize(zeroes - zmod).unwrap();
                    output.push_str(block.to_str());
                }
                let place = place::Place::from_usize(zeroes % 4).unwrap();
                output.push_str(place.to_str());
                if num != 1 {
                    output.push_str(num.to_str());
                }
                continue;
            }
        }

        let modulo = idx % 4;
        match modulo {
            1|2|3 => {
                let place = place::Place::from_usize(modulo).unwrap();
                output.push_str(place.to_str());

                if num != 1 {
                    output.push_str(num.to_str());
                }
            },
            _ => {
                if idx != 0 {
                    let block = block::Block::from_usize(idx)
                        .expect("Block counter doesn't go high enough for this...");
                    output.push_str(&block.to_str_with_space());
                    if num != 1 || remaining > 0 {
                        output.push_str(num.to_str());
                    }
                } else {
                    output.push_str(num.to_str());
                }
            }
        }
    }

    output.chars().rev().collect::<String>()
}

fn parse_hangeul_pure(numbers: Vec<char>) -> String {
    let mut output = String::new();
    let mut iter = numbers.iter().enumerate().peekable();

    while let Some((idx, input_num)) = iter.next() {
        match (idx, input_num) {
            (0, '0') => {
                if let Some((_, next_num)) = iter.peek() {
                    let new_input = format!("{}{}", next_num, "0");
                    let num = numbers::KoreanNumberPure::from_str(&new_input).unwrap();

                    output.push_str(num.to_str());
                    return output;
                } else {
                    let num = numbers::KoreanNumberPure::from_char(input_num).unwrap();
                    output.push_str(num.to_str());
                    return output;
                }
            },
            (0, _) => {
                if let Some((_, next_num)) = iter.peek() {
                    let next_input = format!("{}{}", next_num, "0");
                    let next_num = numbers::KoreanNumberPure::from_str(&next_input).unwrap();
                    output.push_str(next_num.to_str());

                    let input_num = numbers::KoreanNumberPure::from_char(input_num).unwrap();
                    output.push_str(input_num.to_str());
                    return output;
                } else {
                    let num = numbers::KoreanNumberPure::from_char(input_num).unwrap();
                    output.push_str(num.to_str());
                }
            },
            (1, _) => {
                let num = numbers::KoreanNumberPure::from_char(input_num).unwrap();
                output.push_str(num.to_str());
            }
            (_, _) => {}
        }
    }
    output
}