ichiran/
rusty.rs

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
//! Contains more Rusty equivalents of the raw types.

use crate::raw;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Segment {
    /// Japanese
    Words(Vec<WordSegment>),
    /// Punctuation, etc.
    Other(String),
}

impl From<raw::FullSplitInfo> for Vec<Segment> {
    fn from(value: raw::FullSplitInfo) -> Self {
        value
            .0
            .into_iter()
            .map(|s| match s {
                raw::Segment::Words(words) => {
                    Segment::Words(words.into_iter().map(Into::into).collect())
                }
                raw::Segment::Other(other) => Segment::Other(other),
            })
            .collect()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WordSegment {
    pub words: Vec<Word>,
    pub unknown: i32,
}

impl From<raw::WordSegment> for WordSegment {
    fn from(value: raw::WordSegment) -> Self {
        Self {
            words: value.0.into_iter().map(Into::into).collect(),
            unknown: value.1,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Word {
    pub romanized: String,
    pub alternatives: Vec<Alternative>,
}

impl From<raw::Word> for Word {
    fn from(value: raw::Word) -> Self {
        Self {
            romanized: value.0,
            alternatives: value.1.into(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Alternative {
    WordInfo(WordInfo),
    CompoundWordInfo(CompoundWordInfo),
}

impl From<raw::Alternatives> for Vec<Alternative> {
    fn from(value: raw::Alternatives) -> Self {
        let mut alternatives = vec![];
        match value {
            raw::Alternatives::WordInfo(info) => alternatives.push(info.into()),
            raw::Alternatives::Alternatives { alternative } => {
                alternatives.extend(alternative.into_iter().map(Into::into))
            }
        };
        alternatives
    }
}

impl From<raw::Alternative> for Alternative {
    fn from(value: raw::Alternative) -> Self {
        match value {
            raw::Alternative::WordInfo(info) => Self::WordInfo(info.into()),
            raw::Alternative::CompoundWordInfo(compound) => Self::CompoundWordInfo(compound.into()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WordInfo {
    pub reading: String,
    pub text: String,
    pub kana: String,
    pub score: i32,
    pub counter: Option<Counter>,
    pub seq: Option<i32>,
    pub gloss: Vec<Gloss>,
    pub suffix: Option<String>,
    pub conj: Vec<Conj>,
}

impl From<raw::WordInfo> for WordInfo {
    fn from(value: raw::WordInfo) -> Self {
        Self {
            reading: value.reading,
            text: value.text,
            kana: value.kana,
            score: value.score,
            counter: value.counter.map(Into::into),
            seq: value.seq,
            gloss: value.gloss.into_iter().map(Into::into).collect(),
            suffix: value.suffix,
            conj: value.conj.into_iter().map(Into::into).collect(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompoundWordInfo {
    pub reading: String,
    pub text: String,
    pub kana: String,
    pub score: i32,
    pub compound: Vec<String>,
    pub components: Vec<WordInfo>,
}

impl From<raw::CompoundWordInfo> for CompoundWordInfo {
    fn from(value: raw::CompoundWordInfo) -> Self {
        Self {
            reading: value.reading,
            text: value.text,
            kana: value.kana,
            score: value.score,
            compound: value.compound,
            components: value.components.into_iter().map(Into::into).collect(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Counter {
    pub value: String,
    pub ordinal: bool,
}

impl From<raw::Counter> for Counter {
    fn from(value: raw::Counter) -> Self {
        Self {
            value: value.value,
            ordinal: match value.ordinal {
                raw::Ordinal::Bool(b) => b,
                raw::Ordinal::Vec(_) => false,
            },
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Gloss {
    pub pos: String,
    pub gloss: String,
    pub info: Option<String>,
}

impl From<raw::Gloss> for Gloss {
    fn from(value: raw::Gloss) -> Self {
        Self {
            pos: value.pos,
            gloss: value.gloss,
            info: value.info,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Conj {
    pub prop: Vec<ConjProp>,
    pub via: Vec<Via>,
    pub reading: Option<String>,
    pub gloss: Vec<Gloss>,
    pub readok: bool,
}

impl From<raw::Conj> for Conj {
    fn from(value: raw::Conj) -> Self {
        Self {
            prop: value.prop.into_iter().map(Into::into).collect(),
            via: value.via.into_iter().map(Into::into).collect(),
            reading: value.reading,
            gloss: value.gloss.into_iter().map(Into::into).collect(),
            readok: value.readok.into(),
        }
    }
}

impl From<raw::Readok> for bool {
    fn from(value: raw::Readok) -> Self {
        match value {
            raw::Readok::Bool(b) => b,
            raw::Readok::Vec(_) => false,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConjProp {
    pub pos: String,
    pub prop_type: Option<String>,
    pub fml: bool,
    pub neg: bool,
}

impl From<raw::ConjProp> for ConjProp {
    fn from(value: raw::ConjProp) -> Self {
        Self {
            pos: value.pos,
            prop_type: value.prop_type.into(),
            fml: value.fml,
            neg: value.neg,
        }
    }
}

impl From<raw::PropType> for Option<String> {
    fn from(value: raw::PropType) -> Self {
        match value {
            raw::PropType::String(s) => Some(s),
            raw::PropType::Vec(_) => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Via {
    pub prop: Vec<ConjProp>,
    pub reading: Option<String>,
    pub gloss: Vec<Gloss>,
    pub readok: bool,
}

impl From<raw::Via> for Via {
    fn from(value: raw::Via) -> Self {
        Self {
            prop: value.prop.into_iter().map(Into::into).collect(),
            reading: value.reading,
            gloss: value.gloss.into_iter().map(Into::into).collect(),
            readok: value.readok.into(),
        }
    }
}