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
use crate::parse::MsfParser;
use crate::run::Run;
use crate::config::Config;
use std::fs::File;
use std::io::{BufReader, Error};
use tinyfiledialogs::{
message_box_ok, message_box_yes_no, open_file_dialog, MessageBoxIcon, YesNo, save_file_dialog_with_filter
};
pub fn save_check() -> bool {
match message_box_yes_no(
"Save run?",
"Your split file has been updated, do you want to save it?",
MessageBoxIcon::Question,
YesNo::Yes,
) {
YesNo::Yes => true,
YesNo::No => false,
}
}
pub fn get_file(title: &str, filter: &str) -> Option<String> {
open_file_dialog(title, "", Some((&[filter], "")))
}
pub fn get_save_as() -> Option<String> {
save_file_dialog_with_filter("Save as:", "", &["*.msf"], "mist split files")
}
fn try_again() -> bool {
match message_box_yes_no(
"File parse failed",
"File parse failed. Do you want to try another?",
MessageBoxIcon::Question,
YesNo::Yes,
) {
YesNo::Yes => true,
YesNo::No => false,
}
}
pub fn open_run() -> Result<Option<(Run, String)>, Error> {
loop {
match get_file("Open split file", "*.msf") {
Some(ref p) => {
let f = File::open(p)?;
let reader = BufReader::new(f);
let parser = MsfParser::new();
match parser.parse(reader) {
Ok(r) => {
return Ok(Some((r, p.to_owned())));
}
Err(_) => {
if !try_again() {
return Ok(None);
}
}
}
}
None => return Ok(None),
}
}
}
pub fn open_config() -> Result<Option<Config>, String> {
loop {
match get_file("Open a config file", "*.cfg") {
Some(ref p) => {
let f = File::open(p).map_err(|e| e.to_string())?;
let config: Result<Config, String> = ron::de::from_reader(f).map_err(|e| e.to_string());
match config {
Ok(c) => {
return Ok(Some(c));
}
Err(_) => {
if !try_again() {
return Ok(None);
}
}
}
}
None => return Ok(None)
}
}
}
pub fn error(err: &str) -> ! {
message_box_ok("Error", err, MessageBoxIcon::Error);
std::process::exit(1)
}