j2s 0.1.0

json字符串转结构体
Documentation
use j2s::rust;
use j2s::GenCodeByValue;
use std::{fs::read_to_string, io::Write};

use anyhow::Result;
use caisin::files;
use clap::Parser;
use serde_json::Value;
/// json2struct
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
    /// 输入目录
    #[clap(short, long)]
    input: String,
    /// 输出目录
    #[clap(short, long, value_parser, default_value = "out")]
    out_path: String,
    /// 类名
    #[clap(short, long, value_parser, default_value = "root")]
    name: String,
    /// 生成类型
    #[clap(short, long, value_parser, default_value = "rust")]
    typ: String,
}

#[tokio::main]
async fn main() -> Result<()> {
    let args = Args::parse();
    let file_str;
    if files::is_file(&args.input) {
        file_str = read_to_string(&args.input)?;
    } else {
        file_str = args.input;
    }
    let mut content = String::new();
    match &args.typ as &str {
        "rust" => {
            let v: Value = serde_json::from_str(&file_str)?;
            let obj = rust::parse_value(&args.name, v);
            content.push_str("use serde::{Deserialize, Serialize};\n\n");
            content.push_str(&obj.gen());
        }
        _ => {}
    }
    if !content.is_empty() {
        let file = format!("{}/{}.rs", args.out_path, args.name);
        files::create_file(&file)?.write_all(content.as_bytes())?;
    }
    Ok(())
}