Skip to content

Commit bbf46a5

Browse files
committed
Switch from serde_json to sonic_rs, optimize code in some places, implement the importantest impl IntoRSplit
1 parent 69f6e5a commit bbf46a5

File tree

4 files changed

+333
-356
lines changed

4 files changed

+333
-356
lines changed

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
[package]
22
name = "rvpacker-json-txt"
3-
version = "1.3.0"
3+
version = "1.4.0"
44
edition = "2021"
55

66
[dependencies]
77
rayon = "^1.10.0"
8-
serde = "^1.0.197"
9-
serde_json = "^1.0.116"
108
indexmap = "^2.2.6"
119
sys-locale = "^0.3.1"
1210
clap = { version = "^4.5.7", features = ["wrap_help"] }
1311
rand = "^0.8.5"
1412
color-print = "^0.3.6"
1513
fancy-regex = "^0.13.0"
1614
xxhash-rust = {version = "0.8.11", features = ["xxh3"]}
15+
sonic-rs = "0.3.8"

src/main.rs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
22
use color_print::{cformat, cstr};
3-
use serde_json::{from_str, Value};
3+
use sonic_rs::{from_str, JsonValueTrait, Value};
44
use std::{
55
env::args,
66
fs::{create_dir_all, read_to_string},
@@ -42,6 +42,22 @@ impl PartialEq<ProcessingType> for &ProcessingType {
4242
}
4343
}
4444

45+
trait IntoRSplit {
46+
fn into_rsplit_once(self, delimiter: char) -> Option<(String, String)>;
47+
}
48+
49+
// genius implementation
50+
impl IntoRSplit for String {
51+
fn into_rsplit_once(self, delimiter: char) -> Option<(String, String)> {
52+
if let Some(pos) = self.rfind(delimiter) {
53+
let (left, right) = self.split_at(pos);
54+
Some((left.to_string(), right[delimiter.len_utf8()..].to_string()))
55+
} else {
56+
None
57+
}
58+
}
59+
}
60+
4561
struct ProgramLocalization<'a> {
4662
// General program descriptions
4763
about_msg: &'a str,
@@ -78,10 +94,9 @@ struct ProgramLocalization<'a> {
7894
translation_dirs_missing: &'a str,
7995
write_log_msg: &'a str,
8096
read_log_msg: &'a str,
81-
write_success_msg: &'a str,
82-
read_success_msg: &'a str,
8397
file_already_parsed_msg: &'a str,
8498
file_is_not_parsed_msg: &'a str,
99+
done_in_msg: &'a str,
85100

86101
// Misc
87102
possible_values: &'a str,
@@ -126,12 +141,11 @@ impl<'a> ProgramLocalization<'a> {
126141
original_dir_missing: r#"The "original" or "data" folder in the input directory does not exist."#,
127142
translation_dirs_missing: r#"The "translation/maps" and/or "translation/other" folders in the input directory do not exist."#,
128143
write_log_msg: "Wrote file",
129-
write_success_msg: "All files were written successfully.\nTime spent (in seconds):",
130-
read_success_msg: "The entire game text was successfully parsed.\nTime spent: (in seconds):",
131144
read_log_msg: "Parsed file",
132145
file_already_parsed_msg: "file already exists. If you want to forcefully re-read all files, use --force flag, or --append if you want append new text to already existing files.",
133146
file_is_not_parsed_msg: "Files aren't already parsed. Continuing as if --append flag was omitted.",
134147
disable_custom_parsing_desc: "Disables built-in custom parsing for some games.",
148+
done_in_msg: "Done in:"
135149
}
136150
}
137151

@@ -164,12 +178,11 @@ impl<'a> ProgramLocalization<'a> {
164178
original_dir_missing: r#"Папка "original" или "data" входной директории не существует."#,
165179
translation_dirs_missing: r#"Папки "translation/maps" и/или "translation/other" входной директории не существуют."#,
166180
write_log_msg: "Записан файл",
167-
write_success_msg: "Все файлы были записаны успешно.\nПотрачено (в секундах):",
168-
read_success_msg: "Весь игровой текст был успешно запарсен.\nПотрачено (в секундах):",
169181
read_log_msg: "Распарсен файл",
170182
file_already_parsed_msg: "уже существует. Если вы хотите принудительно перезаписать все файлы, используйте флаг --force, или --append если вы хотите добавить новый текст в файлы.",
171183
file_is_not_parsed_msg: "Файлы ещё не распарсены. Продолжаем в режиме с выключенным флагом --append.",
172184
disable_custom_parsing_desc: "Отключает использование индивидуальных способов парсинга файлов для некоторых игр.",
185+
done_in_msg: "Выполнено за:"
173186
}
174187
}
175188
}
@@ -350,7 +363,7 @@ fn main() {
350363
.hide_possible_values(true);
351364

352365
let matches: ArgMatches = cli.get_matches();
353-
let command: &str = matches.subcommand_name().unwrap();
366+
let (subcommand, subcommand_matches): (&str, &ArgMatches) = matches.subcommand().unwrap();
354367

355368
let (
356369
disable_maps_processing,
@@ -400,7 +413,7 @@ fn main() {
400413
}
401414
}
402415

403-
let (maps_path, other_path) = if output_dir == PathBuf::from("./") {
416+
let (maps_path, other_path) = if output_dir.to_str().unwrap() == "./" {
404417
(
405418
input_dir.join("translation/maps"),
406419
input_dir.join("translation/other"),
@@ -420,11 +433,9 @@ fn main() {
420433
get_game_type(&system_file_path)
421434
};
422435

423-
if command == "read" {
424-
let read_matches: &ArgMatches = matches.subcommand_matches(command).unwrap();
425-
426-
let force: bool = read_matches.get_flag("force");
427-
let append: bool = read_matches.get_flag("append");
436+
if subcommand == "read" {
437+
let force: bool = subcommand_matches.get_flag("force");
438+
let append: bool = subcommand_matches.get_flag("append");
428439

429440
let processing_type: ProcessingType = if force {
430441
ProcessingType::Force
@@ -469,20 +480,14 @@ fn main() {
469480
&processing_type,
470481
);
471482
}
472-
473-
println!(
474-
"{} {}.",
475-
localization.read_success_msg,
476-
start_time.elapsed().as_secs_f64()
477-
);
478483
} else {
479484
if !maps_path.exists() || !other_path.exists() {
480485
panic!("{}", localization.translation_dirs_missing);
481486
}
482487

483488
let plugins_path: PathBuf = input_dir.join("translation/plugins");
484489

485-
let (output_path, plugins_output_path) = if output_dir == PathBuf::from("./") {
490+
let (output_path, plugins_output_path) = if output_dir.to_str().unwrap() == "./" {
486491
(input_dir.join("output/data"), input_dir.join("output/js"))
487492
} else {
488493
(output_dir.join("output/data"), output_dir.join("output/js"))
@@ -491,11 +496,7 @@ fn main() {
491496
create_dir_all(&output_path).unwrap();
492497
create_dir_all(&plugins_output_path).unwrap();
493498

494-
let shuffle_level: u8 = *matches
495-
.subcommand_matches(command)
496-
.unwrap()
497-
.get_one::<u8>("shuffle_level")
498-
.unwrap();
499+
let shuffle_level: u8 = *subcommand_matches.get_one::<u8>("shuffle_level").unwrap();
499500

500501
unsafe { write::LOG_MSG = localization.write_log_msg }
501502

@@ -544,11 +545,11 @@ fn main() {
544545
enable_logging,
545546
);
546547
}
547-
548-
println!(
549-
"{} {}.",
550-
localization.write_success_msg,
551-
start_time.elapsed().as_secs_f64()
552-
);
553548
}
549+
550+
println!(
551+
"{} {}",
552+
localization.done_in_msg,
553+
start_time.elapsed().as_secs_f64()
554+
);
554555
}

0 commit comments

Comments
 (0)