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
#[macro_use]
extern crate lazy_static;
extern crate locale_config;
extern crate regex;
use std::collections::BTreeMap;
use locale_config::LanguageRange;
macro_rules! unpack {
($x:expr => $p:pat => $r:expr) => {{
match $x {
$p => Some($r),
_ => None,
}
}};
($x:expr => $p:pat if $c:expr => $r:expr) => {{
match $x {
$p if $c => Some($r),
_ => None,
}
}};
}
macro_rules! is {
($x:expr => $p:pat) => {{
match $x {
$p => true,
_ => false,
}
}};
($x:expr => $p:pat if $c:expr) => {{
match $x {
$p if $c => true,
_ => false,
}
}};
}
pub mod po;
#[derive(Copy,Clone,Debug,Eq,PartialEq,Ord,PartialOrd,Hash)]
pub enum Count {
Zero,
One,
Two,
Few,
Many,
Other,
}
impl Default for Count {
fn default() -> Count { Count::One }
}
#[derive(Clone,Debug,Eq,PartialEq,Ord,PartialOrd,Hash)]
pub enum Message {
Empty,
Singular(String),
Plural(BTreeMap<Count, String>),
}
impl Message {
pub fn is_empty(&self) -> bool {
match self {
&Message::Empty => true,
_ => false,
}
}
pub fn is_singular(&self) -> bool {
match self {
&Message::Singular(_) => true,
_ => false,
}
}
pub fn is_plural(&self) -> bool {
match self {
&Message::Plural(_) => true,
_ => false,
}
}
pub fn is_blank(&self) -> bool {
match self {
&Message::Empty => true,
&Message::Singular(ref s) => s == "",
&Message::Plural(ref m) => m.values().all(|s| s == ""),
}
}
pub fn singular(&self) -> Option<&str> {
match self {
&Message::Singular(ref s) => Some(s.as_ref()),
_ => None,
}
}
}
impl Default for Message {
fn default() -> Message { Message::Empty }
}
#[derive(Clone,Debug,Eq,PartialEq,Ord,PartialOrd,Hash)]
pub enum Origin {
Developer,
Translator,
Tag(String),
}
#[derive(Copy,Clone,PartialEq,Eq,PartialOrd,Ord,Debug,Hash)]
pub enum State {
Empty,
NeedsWork,
Final,
}
impl Default for State {
fn default() -> State { State::Empty }
}
#[derive(Clone,Debug,Default)]
pub struct Unit {
_context: Option<String>,
_source: Message,
_target: Message,
_prev_context: Option<String>,
_prev_source: Message,
_notes: Vec<(Origin, String)>,
_locations: Vec<String>,
_state: State,
_obsolete: bool,
}
impl Unit {
pub fn context(&self) -> &Option<String> { &self._context }
pub fn source(&self) -> &Message { &self._source }
pub fn target(&self) -> &Message { &self._target }
pub fn prev_context(&self) -> &Option<String> { &self._prev_context }
pub fn prev_source(&self) -> &Message { &self._prev_source }
pub fn notes(&self) -> &Vec<(Origin, String)> { &self._notes }
pub fn locations(&self) -> &Vec<String> { &self._locations }
pub fn state(&self) -> State { self._state }
pub fn is_translated(&self) -> bool { self._state == State::Final }
pub fn is_obsolete(&self) -> bool { self._obsolete }
}
pub trait CatalogueReader : Iterator<Item = Result<Unit, Error>> {
fn target_language(&self) -> &LanguageRange<'static>;
}
#[derive(Debug)]
pub enum Error {
Io(usize, std::io::Error),
Parse(usize, Option<String>, Vec<&'static str>),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
&Error::Io(0, ref err) => err.fmt(f),
&Error::Io(line, ref err) => write!(f, "{} at line {}", err, line),
&Error::Parse(line, ref got, ref exp) => {
write!(f, "Parse error at line {}", line)?;
if !exp.is_empty() {
let mut prefix = ", expected";
for e in exp {
write!(f, "{} ‘{}’", prefix, e)?;
prefix = " or";
}
}
if got.is_some() {
write!(f, ", got ‘{}’", got.as_ref().unwrap())?;
}
Ok(())
}
}
}
}
impl std::error::Error for Error {
fn description(&self) -> &str {
self.cause().map(std::error::Error::description).unwrap_or("parse error")
}
fn cause(&self) -> Option<&std::error::Error> {
match self {
&Error::Io(_, ref err) => Some(err),
&Error::Parse(..) => None,
}
}
}