parse_json/
lib.rs

1use serde::{Deserialize, Serialize};
2use std::fs;
3pub mod fusion;
4use clap::{Parser, Subcommand};
5
6#[derive(Debug, Deserialize, Serialize)]
7struct IconInfo {
8    title: String,
9    #[serde(rename = "categoryTitle")]
10    category_title: String,
11    path: String,
12    category: String,
13    #[serde(rename = "titleKey")]
14    title_key: String,
15    #[serde(rename = "categoryKey")]
16    category_key: String,
17}
18
19pub fn process(path: &str, output: &str) -> Result<(), Box<dyn std::error::Error>> {
20    let reg = regex::Regex::new(r"\{.*\}")?;
21    let json_str = fs::read_to_string(path)?;
22    let res = reg
23        .find_iter(&json_str)
24        .map(|i| serde_json::from_str::<IconInfo>(i.as_str()).unwrap())
25        .collect::<Vec<_>>();
26    fs::write(output, serde_json::to_vec_pretty(&res).unwrap())?;
27    Ok(())
28}
29
30/// 处理json文件,将json转换成正常的格式
31#[derive(Debug, Parser)]
32#[command(author, version, about, long_about = None)]
33pub struct ProcessJson {
34    #[command(subcommand)]
35    pub command: SubCommand,
36}
37
38#[derive(Debug, Subcommand)]
39pub enum SubCommand {
40    /// 处理异常格式的json文件
41    Process {
42        /// json 文件路径
43        #[arg(short, long)]
44        path: String,
45        /// 输出文件路径
46        #[arg(short, long)]
47        output: String,
48    },
49    /// 融合两个json文件,并将英文title放入一个json中
50    Fusion,
51}
52
53impl SubCommand {
54    pub fn run(&self) -> Result<(), Box<dyn std::error::Error>> {
55        match self {
56            SubCommand::Process { path, output } => {
57                process(path, output)?;
58            }
59            SubCommand::Fusion => {
60                fusion::fusion_run()?;
61            }
62        }
63        Ok(())
64    }
65}