j2s 0.1.0

json字符串转结构体
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use anyhow::Result;
use serde_json::Value;
pub mod macros;
pub mod rust;
pub trait GenCodeByValue {
    fn gen(&self) -> String;
}

// 根据字符串生成结构体
pub fn gen_by_str(name: &str, s: &str) -> Result<String> {
    let v: Value = serde_json::from_str(&s)?;
    let obj = rust::parse_value(name, v);
    let mut content = obj.gen();
    content.insert_str(0, "use serde::{Deserialize, Serialize};\n\n");
    Ok(content)
}