1use dialogue_macro::Asker;
2use serde_json::{json, Value};
3use std::{borrow::BorrowMut, ffi::OsStr, fs};
4
5use crate::IconInfo;
6
7#[derive(Debug, Asker)]
8struct Fusion {
9 #[select(prompt = "请选择中文json")]
10 json_a: String,
11 #[select(prompt = "请选择英文json")]
12 json_b: String,
13 #[input(prompt = "请输入输出文件路径", default = "fusion.json")]
14 output: String,
15}
16
17fn read_json_files() -> Result<Vec<String>, Box<dyn std::error::Error>> {
18 let current_dir = std::env::current_dir()?;
19 let entries = fs::read_dir(current_dir)?;
20 let mut res = vec![];
21 for i in entries {
22 let entry = i?;
23 let path = entry.path();
24 if path.is_file() && path.extension() == Some(OsStr::new("json")) {
25 let str = path.to_string_lossy().to_string();
26 res.push(str);
27 }
28 }
29 Ok(res)
30}
31
32pub fn fusion_run() -> Result<(), Box<dyn std::error::Error>> {
33 let json_files = read_json_files()?;
34
35 let fusion = Fusion::asker()
36 .json_a(&json_files)
37 .json_b(&json_files)
38 .output()
39 .finish();
40
41 let reg = regex::Regex::new(r"\{.*\}")?;
42 let json_str1 = fs::read_to_string(&fusion.json_a)?;
43 let mut json1 = reg
44 .find_iter(&json_str1)
45 .map(|i| serde_json::from_str::<IconInfo>(i.as_str()).unwrap())
46 .collect::<Vec<_>>();
47 let json_str2 = fs::read_to_string(&fusion.json_b)?;
48 let json2 = reg
49 .find_iter(&json_str2)
50 .map(|i| serde_json::from_str::<IconInfo>(i.as_str()).unwrap())
51 .collect::<Vec<_>>();
52
53 let mut res: Vec<Value> = vec![];
54 for i in 0..json1.len() {
55 let value = json1[i].borrow_mut();
56 let keys = value.category_key.split("_").collect::<Vec<_>>();
57 value.category_key = keys.last().unwrap().to_string();
58
59 let keys = value.path.split("/").collect::<Vec<_>>();
60 value.title_key = keys.last().unwrap().to_string().replace(".svg", "");
61
62 let mut icon = json!(value);
63 icon["enTitle"] = Value::String(json2[i].title.clone());
64 icon["enCategoryTitle"] = Value::String(json2[i].category_title.clone());
65 res.push(icon);
66 }
67 fs::write(fusion.output, serde_json::to_vec_pretty(&res).unwrap())?;
68 Ok(())
69}