-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
main.rs
287 lines (261 loc) · 8.39 KB
/
main.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
use std::io;
mod section;
mod inifile;
use inifile::IniFile;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
struct Opt {
#[structopt(subcommand)]
cmd: Command,
#[structopt(short, long)]
dry_run: bool,
}
#[derive(StructOpt, Debug)]
enum Command {
CopyMissingLines {
#[structopt(short, long)]
dont_comment_missing: bool,
},
CommentUnknownLines {},
RemoveUnknownLines {},
AddNewKey {
section: String,
key: String,
},
AddNewKeyValue {
section: String,
key: String,
value: String,
},
MoveKey {
old: String,
new: String,
key: String,
},
RenameKey {
section: String,
old: String,
new: String,
},
SortSection {
section: String,
},
RemoveKey {
section: String,
key: String,
},
}
fn copy_missing_lines(
reference_ini: &IniFile,
target_ini: &mut IniFile,
comment_missing: bool,
) -> io::Result<()> {
for reference_section in &reference_ini.sections {
// Insert any missing full sections.
if !target_ini.insert_section_if_missing(reference_section) {
if let Some(target_section) = target_ini.get_section_mut(&reference_section.name) {
for line in &reference_section.lines {
target_section.insert_line_if_missing(line);
}
//target_section.remove_lines_if_not_in(reference_section);
if comment_missing {
target_section.comment_out_lines_if_not_in(reference_section);
}
}
} else {
// Note: insert_section_if_missing will copy the entire section,
// no need to loop over the lines here.
println!("Inserted missing section: {}", reference_section.name);
}
}
Ok(())
}
fn deal_with_unknown_lines(
reference_ini: &IniFile,
target_ini: &mut IniFile,
remove: bool,
) -> io::Result<()> {
for reference_section in &reference_ini.sections {
if let Some(target_section) = target_ini.get_section_mut(&reference_section.name) {
if remove {
target_section.remove_lines_if_not_in(reference_section);
} else {
target_section.comment_out_lines_if_not_in(reference_section);
}
}
}
Ok(())
}
fn move_key(target_ini: &mut IniFile, old: &str, new: &str, key: &str) -> io::Result<()> {
if let Some(old_section) = target_ini.get_section_mut(old) {
if let Some(line) = old_section.remove_line(key) {
if let Some(new_section) = target_ini.get_section_mut(new) {
new_section.insert_line_if_missing(&line);
} else {
println!("No new section {}", new);
}
} else {
println!("No key {} in section {}", key, old);
}
} else {
println!("No old section {}", old);
}
Ok(())
}
fn remove_key(target_ini: &mut IniFile, section: &str, key: &str) -> io::Result<()> {
if let Some(old_section) = target_ini.get_section_mut(section) {
old_section.remove_line(key);
} else {
println!("No section {}", section);
}
Ok(())
}
fn add_new_key(target_ini: &mut IniFile, section: &str, key: &str, value: &str) -> io::Result<()> {
if let Some(section) = target_ini.get_section_mut(section) {
section.insert_line_if_missing(&format!("{} = {}", key, value));
} else {
println!("No section {}", section);
}
Ok(())
}
fn rename_key(target_ini: &mut IniFile, section: &str, old: &str, new: &str) -> io::Result<()> {
if let Some(section) = target_ini.get_section_mut(section) {
section.rename_key(old, new);
} else {
println!("No section {}", section);
}
Ok(())
}
fn sort_section(target_ini: &mut IniFile, section: &str) -> io::Result<()> {
if let Some(section) = target_ini.get_section_mut(section) {
section.sort();
} else {
println!("No section {}", section);
}
Ok(())
}
// TODO: Look into using https://github.com/Byron/google-apis-rs/tree/main/gen/translate2 for initial translations.
fn main() {
let opt = Opt::from_args();
// TODO: Grab extra arguments from opt somehow.
let args: Vec<String> = vec![]; //std::env::args().skip(1).collect();
let mut filenames = args;
let root = "../../assets/lang";
let reference_ini_filename = "en_US.ini";
let mut reference_ini =
IniFile::parse(&format!("{}/{}", root, reference_ini_filename)).unwrap();
if filenames.is_empty() {
// Grab them all.
for path in std::fs::read_dir(root).unwrap() {
let path = path.unwrap();
if path.file_name() == reference_ini_filename {
continue;
}
let filename = path.file_name();
let filename = filename.to_string_lossy();
if !filename.ends_with(".ini") {
continue;
}
filenames.push(path.file_name().to_string_lossy().to_string());
}
}
for filename in filenames {
let reference_ini = &reference_ini;
if filename == "langtool" {
// Get this from cargo run for some reason.
continue;
}
let target_ini_filename = format!("{}/{}", root, filename);
println!("Langtool processing {}", target_ini_filename);
let mut target_ini = IniFile::parse(&target_ini_filename).unwrap();
match opt.cmd {
Command::CopyMissingLines {
dont_comment_missing,
} => {
copy_missing_lines(reference_ini, &mut target_ini, !dont_comment_missing).unwrap();
}
Command::CommentUnknownLines {} => {
deal_with_unknown_lines(reference_ini, &mut target_ini, false).unwrap();
}
Command::RemoveUnknownLines {} => {
deal_with_unknown_lines(reference_ini, &mut target_ini, true).unwrap();
}
Command::SortSection { ref section } => sort_section(&mut target_ini, section).unwrap(),
Command::RenameKey {
ref section,
ref old,
ref new,
} => rename_key(&mut target_ini, section, old, new).unwrap(),
Command::AddNewKey {
ref section,
ref key,
} => add_new_key(&mut target_ini, section, key, key).unwrap(),
Command::AddNewKeyValue {
ref section,
ref key,
ref value,
} => add_new_key(&mut target_ini, section, key, value).unwrap(),
Command::MoveKey {
ref old,
ref new,
ref key,
} => {
move_key(&mut target_ini, old, new, key).unwrap();
}
Command::RemoveKey {
ref section,
ref key,
} => {
remove_key(&mut target_ini, section, key).unwrap();
}
}
if !opt.dry_run {
target_ini.write().unwrap();
}
}
println!("Langtool processing {}", reference_ini_filename);
// Some commands also apply to the reference ini.
match opt.cmd {
Command::AddNewKey {
ref section,
ref key,
} => {
add_new_key(&mut reference_ini, section, key, key).unwrap();
}
Command::AddNewKeyValue {
ref section,
ref key,
ref value,
} => {
add_new_key(&mut reference_ini, section, key, value).unwrap();
}
Command::SortSection { ref section } => sort_section(&mut reference_ini, section).unwrap(),
Command::RenameKey {
ref section,
ref old,
ref new,
} => {
if old == new {
println!("WARNING: old == new");
}
rename_key(&mut reference_ini, section, old, new).unwrap();
}
Command::MoveKey {
ref old,
ref new,
ref key,
} => {
move_key(&mut reference_ini, old, new, key).unwrap();
}
Command::RemoveKey {
ref section,
ref key,
} => {
remove_key(&mut reference_ini, section, key).unwrap();
}
_ => {}
}
if !opt.dry_run {
reference_ini.write().unwrap();
}
}