-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle.rs
420 lines (395 loc) · 15.2 KB
/
style.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
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
//! Structs for handling the style file.
use anyhow::{anyhow, Result};
use printpdf::Mm;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::iter::Enumerate;
use std::num::ParseFloatError;
use std::path::PathBuf;
use std::vec::Vec;
pub(crate) mod command;
pub(crate) mod core;
use crate::style::command::{
EducationExperience, History, HistoryPosition, Line, Lines, MiscBox, MultiLines, Photo, Text,
TextBox, YMBox,
};
use crate::style::core::{FontOptions, LineOptions, LineStyle, Point, Size};
fn handle_missing<T>(
expression: Option<T>,
value_name: &str,
command_name: &str,
line_number: usize,
) -> T {
let message = format!(
"Missing {} value for {} at line: {}",
value_name, command_name, line_number
);
expression.expect(&message)
}
fn parse_size(raw_size: &str) -> Result<Mm, ParseFloatError> {
let len = raw_size.len();
let prefix = &raw_size.get(len - 2..);
match prefix {
Some("mm") => {
let mm_number = raw_size.trim_end_matches("mm");
let mm_as_float: f64 = mm_number.parse::<f64>()?;
Ok(Mm(mm_as_float))
}
Some("cm") => {
let cm_number = raw_size.trim_end_matches("cm");
let cm_as_float: f64 = cm_number.parse::<f64>()?;
Ok(Mm(cm_as_float * 10.0_f64))
}
_ => {
let size_as_float: f64 = raw_size.parse::<f64>()?;
Ok(Mm(size_as_float))
}
}
}
fn parse_option<T: std::str::FromStr>(name: &str, raw_option: &str) -> Result<T, T::Err> {
let pattern = format!("{}=", name);
let option_number = raw_option.trim_start_matches(&pattern);
let option_value = option_number.parse::<T>()?;
Ok(option_value)
}
fn parse_font_options(parameters: &[&str]) -> Result<FontOptions> {
let mut font_size: Option<f64> = None;
let mut font_face: Option<String> = None;
for parameter in parameters {
if let Some((command, value)) = (*parameter).to_owned().split_once('=') {
match command {
"font_size" => font_size = Some(value.parse::<f64>()?),
"font_face" => font_face = Some(value.to_owned()),
_ => continue,
}
}
}
Ok(FontOptions {
font_size,
font_face,
})
}
fn parse_line_options(parameters: &[&str]) -> Result<LineOptions> {
let mut line_width: Option<f32> = None;
let mut line_style: Option<LineStyle> = None;
for parameter in parameters {
if let Some((command, value)) = (*parameter).to_owned().split_once('=') {
match command {
"line_width" => line_width = Some(value.parse::<f32>()?),
"line_style" => line_style = Some(value.parse::<LineStyle>()?),
_ => continue,
}
}
}
Ok(LineOptions {
line_width,
line_style,
})
}
fn parse_string(parameters: &[&str], line_number: usize) -> Result<Text> {
let raw_x = *handle_missing(parameters.get(1), "x", "string", line_number);
let raw_y = *handle_missing(parameters.get(2), "y", "string", line_number);
let raw_value = *handle_missing(parameters.get(3), "value", "string", line_number);
let position = Point {
x: parse_size(raw_x)?,
y: parse_size(raw_y)?,
};
Ok(Text {
position,
value: raw_value.to_owned(),
font_options: parse_font_options(parameters)?,
})
}
fn parse_line(parameters: &[&str], line_number: usize) -> Result<Line> {
let raw_starting_x = *handle_missing(parameters.get(1), "x1", "line", line_number);
let raw_starting_y = *handle_missing(parameters.get(2), "2", "line", line_number);
let raw_ending_x = *handle_missing(parameters.get(3), "x2", "line", line_number);
let raw_ending_y = *handle_missing(parameters.get(4), "y2", "line", line_number);
let start_position = Point {
x: parse_size(raw_starting_x)?,
y: parse_size(raw_starting_y)?,
};
let end_position = Point {
x: parse_size(raw_ending_x)?,
y: parse_size(raw_ending_y)?,
};
Ok(Line {
start_position,
end_position,
line_options: parse_line_options(parameters)?,
})
}
fn parse_box(parameters: &[&str], line_number: usize) -> Result<command::Box> {
let raw_pos_x = *handle_missing(parameters.get(1), "x", "box", line_number);
let raw_pos_y = *handle_missing(parameters.get(2), "y", "box", line_number);
let raw_width = *handle_missing(parameters.get(3), "width", "box", line_number);
let raw_height = *handle_missing(parameters.get(4), "height", "box", line_number);
let position = Point {
x: parse_size(raw_pos_x)?,
y: parse_size(raw_pos_y)?,
};
let size = Size {
width: parse_size(raw_width)?,
height: parse_size(raw_height)?,
};
Ok(command::Box {
position,
size,
line_options: parse_line_options(parameters)?,
})
}
fn parse_photo(parameters: &[&str], line_number: usize) -> Result<Photo, ParseFloatError> {
let raw_pos_x = *handle_missing(parameters.get(1), "x", "photo", line_number);
let raw_pos_y = *handle_missing(parameters.get(2), "y", "photo", line_number);
let raw_width = *handle_missing(parameters.get(3), "width", "photo", line_number);
let raw_height = *handle_missing(parameters.get(4), "height", "photo", line_number);
let position = Point {
x: parse_size(raw_pos_x)?,
y: parse_size(raw_pos_y)?,
};
let size = Size {
width: parse_size(raw_width)?,
height: parse_size(raw_height)?,
};
Ok(Photo { position, size })
}
fn parse_textbox(parameters: &[&str], line_number: usize) -> Result<TextBox> {
let raw_pos_x = *handle_missing(parameters.get(1), "x", "text box", line_number);
let raw_pos_y = *handle_missing(parameters.get(2), "y", "text box", line_number);
let raw_width = *handle_missing(parameters.get(3), "width", "text box", line_number);
let raw_height = *handle_missing(parameters.get(4), "height", "text box", line_number);
let raw_value = *handle_missing(parameters.get(5), "value", "text box", line_number);
let position = Point {
x: parse_size(raw_pos_x)?,
y: parse_size(raw_pos_y)?,
};
let size = Size {
width: parse_size(raw_width)?,
height: parse_size(raw_height)?,
};
Ok(TextBox {
position,
size,
value: raw_value.to_owned(),
font_options: parse_font_options(parameters)?,
})
}
fn parse_multilines(parameters: &[&str], line_number: usize) -> Result<MultiLines> {
let raw_pos_x = *handle_missing(parameters.get(1), "x", "multi-lines", line_number);
let raw_pos_y = *handle_missing(parameters.get(2), "y", "multi-lines", line_number);
let raw_direction_x = *handle_missing(parameters.get(3), "dx", "multi-lines", line_number);
let raw_direction_y = *handle_missing(parameters.get(4), "dy", "multi-lines", line_number);
let raw_stroke_num = *handle_missing(
parameters.get(5),
"number of strokes",
"multi-lines",
line_number,
);
let raw_offset_x = *handle_missing(parameters.get(6), "sx", "multi-lines", line_number);
let raw_offset_y = *handle_missing(parameters.get(7), "sy", "multi-lines", line_number);
let start_position = Point {
x: parse_size(raw_pos_x)?,
y: parse_size(raw_pos_y)?,
};
let d_position = Point {
x: parse_size(raw_direction_x)?,
y: parse_size(raw_direction_y)?,
};
let stroke_number: u32 = raw_stroke_num.parse::<u32>()?;
let s_position = Point {
x: parse_size(raw_offset_x)?,
y: parse_size(raw_offset_y)?,
};
Ok(MultiLines {
start_position,
direction: d_position,
stroke_number,
position_offset: s_position,
})
}
fn parse_ymbox(parameters: &[&str], line_number: usize) -> Result<YMBox> {
let raw_title = *handle_missing(parameters.get(1), "title", "ym box", line_number);
let raw_height = *handle_missing(parameters.get(2), "height", "ym box", line_number);
let raw_num = *handle_missing(parameters.get(3), "number", "ym box", line_number);
let raw_value = *handle_missing(parameters.get(4), "value", "ym box", line_number);
Ok(YMBox {
title: raw_title.to_owned(),
height: parse_size(raw_height)?,
num: raw_num.parse::<u32>()?,
value: raw_value.to_owned(),
font_options: parse_font_options(parameters)?,
})
}
fn parse_miscbox(parameters: &[&str], line_number: usize) -> Result<MiscBox> {
let raw_title = *handle_missing(parameters.get(1), "title", "misc box", line_number);
let raw_y = *handle_missing(parameters.get(2), "y", "misc box", line_number);
let raw_height = *handle_missing(parameters.get(3), "height", "misc box", line_number);
let raw_value = *handle_missing(parameters.get(4), "value", "misc box", line_number);
Ok(MiscBox {
title: raw_title.to_owned(),
y: parse_size(raw_y)?,
height: parse_size(raw_height)?,
value: raw_value.to_owned(),
font_options: parse_font_options(parameters)?,
})
}
fn parse_history(parameters: &[&str], line_number: usize) -> Result<History> {
let raw_y = *handle_missing(parameters.get(1), "y", "history", line_number);
let raw_year_x = *handle_missing(parameters.get(2), "year x", "history", line_number);
let raw_month_x = *handle_missing(parameters.get(3), "month x", "history", line_number);
let raw_value_x = *handle_missing(parameters.get(4), "value x", "history", line_number);
let raw_padding = *handle_missing(parameters.get(5), "dy", "history", line_number);
let raw_value = *handle_missing(parameters.get(6), "value", "history", line_number);
Ok(History {
positions: HistoryPosition {
y: parse_size(raw_y)?,
year_x: parse_size(raw_year_x)?,
month_x: parse_size(raw_month_x)?,
value_x: parse_size(raw_value_x)?,
padding: parse_size(raw_padding)?,
},
value: raw_value.to_owned(),
font_options: parse_font_options(parameters)?,
})
}
fn parse_education_experience(
parameters: &[&str],
line_number: usize,
) -> Result<EducationExperience> {
let raw_y = *handle_missing(parameters.get(1), "y", "history", line_number);
let raw_year_x = *handle_missing(parameters.get(2), "year x", "history", line_number);
let raw_month_x = *handle_missing(parameters.get(3), "month x", "history", line_number);
let raw_value_x = *handle_missing(parameters.get(4), "value x", "history", line_number);
let raw_padding = *handle_missing(parameters.get(5), "dy", "history", line_number);
let raw_caption_x = *handle_missing(parameters.get(6), "caption x", "history", line_number);
let raw_ijo_x = *handle_missing(parameters.get(7), "ijo x", "history", line_number);
Ok(EducationExperience {
positions: HistoryPosition {
y: parse_size(raw_y)?,
year_x: parse_size(raw_year_x)?,
month_x: parse_size(raw_month_x)?,
value_x: parse_size(raw_value_x)?,
padding: parse_size(raw_padding)?,
},
caption_x: parse_size(raw_caption_x)?,
ijo_x: parse_size(raw_ijo_x)?,
font_options: parse_font_options(parameters)?,
})
}
fn parse_lines(parameters: &[&str], line_number: usize) -> Result<Lines> {
let raw_stroke_number = *handle_missing(parameters.get(1), "num", "misc box", line_number);
let mut positions: Vec<Point> = Vec::new();
let mut i = 2;
while let [Some(raw_x), Some(raw_y)] = [parameters.get(i), parameters.get(i + 1)] {
if raw_x.contains('=') || raw_y.contains('=') {
break;
}
positions.push(Point {
x: parse_size(raw_x)?,
y: parse_size(raw_y)?,
});
i += 2;
}
let mut close: Option<bool> = None;
if let Some(raw_option) = parameters.last() {
close = Some(parse_option("close", raw_option)?);
}
Ok(Lines {
stroke_number: raw_stroke_number.parse::<u32>()?,
positions,
line_options: parse_line_options(parameters)?,
close,
})
}
pub(crate) enum Command {
Text(Text),
Line(Line),
Box(command::Box),
Photo(Photo),
NewPage,
TextBox(TextBox),
MultiLines(MultiLines),
YMBox(YMBox),
MiscBox(MiscBox),
History(History),
EducationExperience(EducationExperience),
Lines(Lines),
}
type LineIterator = Enumerate<std::io::Lines<BufReader<File>>>;
fn get_lines(path: &PathBuf) -> std::io::Result<LineIterator> {
let style_file = File::open(path)?;
let reader = BufReader::new(style_file);
Ok(reader.lines().enumerate())
}
pub(crate) fn read(path: &PathBuf) -> Result<Vec<Command>> {
let mut items: Vec<Command> = Vec::new();
for (index, line) in get_lines(path)? {
let line = line?;
// Handle comments
if line.starts_with('#') {
continue;
}
// Skip blank lines
if line.is_empty() {
continue;
}
let split_line: Vec<&str> = line.split(',').collect();
let command_name = split_line.first();
match command_name {
Some(&"string") => {
let string = parse_string(&split_line, index)?;
items.push(Command::Text(string));
}
Some(&"line") => {
let line_command = parse_line(&split_line, index)?;
items.push(Command::Line(line_command));
}
Some(&"box") => {
let box_command = parse_box(&split_line, index)?;
items.push(Command::Box(box_command));
}
Some(&"photo") => {
let photo = parse_photo(&split_line, index)?;
items.push(Command::Photo(photo));
}
Some(&"new_page") => {
items.push(Command::NewPage);
}
Some(&"textbox") => {
let textbox = parse_textbox(&split_line, index)?;
items.push(Command::TextBox(textbox));
}
Some(&"multi_lines") => {
let multi_lines = parse_multilines(&split_line, index)?;
items.push(Command::MultiLines(multi_lines));
}
Some(&"ymbox") => {
let ymbox = parse_ymbox(&split_line, index)?;
items.push(Command::YMBox(ymbox));
}
Some(&"miscbox") => {
let miscbox = parse_miscbox(&split_line, index)?;
items.push(Command::MiscBox(miscbox));
}
Some(&"history") => {
let history = parse_history(&split_line, index)?;
items.push(Command::History(history));
}
Some(&"education_experience") => {
let education_experience = parse_education_experience(&split_line, index)?;
items.push(Command::EducationExperience(education_experience));
}
Some(&"lines") => {
let lines = parse_lines(&split_line, index)?;
items.push(Command::Lines(lines));
}
_ => {
return Err(anyhow!(
"Unsupported command: {}!",
command_name.unwrap_or(&"")
))
}
}
}
Ok(items)
}