#json-toml #toml #yaml #json #convert-json #convert-json-to-toml #convert-toml-to-json

bin+lib tsu

Toml 编码和解码工具,解析为 toml,将 json 转换为 toml,将 toml 转换为 json,将 toml 转换为 yaml,将 yaml 转换为 toml

28 个版本 (2 个稳定版)

1.0.1 2022 年 9 月 18 日
1.0.0 2022 年 8 月 29 日
0.1.25 2022 年 8 月 29 日
0.1.19 2022 年 7 月 10 日
0.1.9 2022 年 5 月 31 日

#961 in 编码

Download history 33/week @ 2024-03-14 31/week @ 2024-03-21 40/week @ 2024-03-28 27/week @ 2024-04-04 31/week @ 2024-04-11 35/week @ 2024-04-18 31/week @ 2024-04-25 40/week @ 2024-05-02 27/week @ 2024-05-09 31/week @ 2024-05-16 35/week @ 2024-05-23 64/week @ 2024-05-30 23/week @ 2024-06-06 33/week @ 2024-06-13 44/week @ 2024-06-20 13/week @ 2024-06-27

每月 118 次下载
5 crates 中使用

MIT 许可证

15KB
132

tsu - toml 编码和解码工具,解析为 toml

将字符串解析为 toml 数据

{
   let cargo_toml = tsu::toml_from_str(r#"
   [package]
   name = "useful_macro"
   version = "0.2.6"

   [dependencies]
   serde = "1.0"

   [dev-dependencies]
   serde_derive = "1.0"
   serde_json = "1.0"
   "#);
   
   let package = cargo_toml.get("package").unwrap();
   let name = package.get("name").unwrap();
   println!("{:#?}", &package);
   println!("{:#?}", &name);

   let dependencies = cargo_toml.get("dependencies").unwrap();
   println!("{:#?}", &dependencies);

   let dev_dependencies = cargo_toml.get("dev-dependencies").unwrap();
   println!("{:#?}", &dev_dependencies);
}

将结构体转换为 toml 字符串

[package]
name = "demo"
version = "0.1.0"
edition = "2021"

[dependencies]
tsu = "0.1.8"
serde = "1.0.137"
serde_derive = "1.0.137"
{
   use serde_derive::Serialize;
   #[derive(Serialize)]
   struct Human {
       name: String,
       age: u8,
       country: Country,
   }
   #[derive(Serialize)]
   struct Country {
       name: String,
   }
   let user = Human {
       name: "mike".to_string(),
       age: 18,
       country: Country {
           name: "country_name".to_string(),
       },
   };
   let toml = tsu::to_toml_str(&user);
   std::fs::write("./demo.toml", toml.as_str()).unwrap();
   println!("{}",toml.as_str());
}

读取 toml 文件并将内容解析为 toml 数据

{
   let cargo_toml = tsu::toml_from_path("./Cargo.toml");
   let dependencies = cargo_toml.get("dependencies").unwrap();
   println!("{:#?}", &dependencies);
}

convert_toml_to_json 用于将 toml 转换为 json

{
    let toml_data = r#"
            [package]
            name = "package_name"
            version = "0.3.0"
    
            [dependencies]
            serde = "1.0"
    
            [dev-dependencies]
            serde_derive = "1.0"
            serde_json = "1.0"
    "#;
    let json = convert_toml_to_json(toml_data).unwrap();
    assert_eq!("{\n  \"dependencies\": {\n    \"serde\": \"1.0\"\n  },\n  \"dev-dependencies\": {\n    \"serde_derive\": \"1.0\",\n    \"serde_json\": \"1.0\"\n  },\n  \"package\": {\n    \"name\": \"package_name\",\n    \"version\": \"0.3.0\"\n  }\n}",json);
    
}

convert_json_to_toml 用于将 json 转换为 toml

{
    let json_data=  r#"
    {
        "data":{
            "version":"0.12.0",
            "category":"rust"
        }
    }
    "#;
    let toml = convert_json_to_toml(&json_data).unwrap();
    assert_eq!("[data]\nversion = '0.12.0'\ncategory = 'rust'\n",toml);
}

convert_toml_to_yaml 用于将 toml 转换为 yaml

let toml_data = r#"
[package]
name = "package_name"
version = "0.3.0"

[dependencies]
serde = "1.0"

[dev-dependencies]
serde_derive = "1.0"
serde_json = "1.0"
"#;
let yaml = convert_toml_to_yaml(toml_data).unwrap();
assert_eq!("dependencies:\n  serde: '1.0'\ndev-dependencies:\n  serde_derive: '1.0'\n  serde_json: '1.0'\npackage:\n  name: package_name\n  version: 0.3.0\n",yaml);

convert_yaml_to_toml 用于将 yaml 转换为 toml

let yaml_data = r"dependencies:
  serde: '1.0'
dev-dependencies:
  serde_derive: '1.0'
  serde_json: '1.0'
package:
  name: package_name
  version: 0.3.0";
assert_eq!("[dependencies]\nserde = '1.0'\n\n[dev-dependencies]\nserde_derive = '1.0'\nserde_json = '1.0'\n\n[package]\nname = 'package_name'\nversion = '0.3.0'\n",convert_yaml_to_toml(yaml_data).unwrap());

依赖项

~2.2–3MB
~69K SLoC