#toml #parser #json #convert-json-to-toml #convert-toml-to-json

bin+lib tsu

Toml encoding and decoding utilities,parse to toml,convert-json-to-toml,convert-toml-to-json

21 releases

Uses new Rust 2021

new 0.1.20 Aug 14, 2022
0.1.19 Jul 10, 2022
0.1.14 Jun 23, 2022
0.1.9 May 31, 2022

#272 in Encoding

Download history 161/week @ 2022-05-24 128/week @ 2022-05-31 52/week @ 2022-06-07 87/week @ 2022-06-14 52/week @ 2022-06-21 23/week @ 2022-06-28 110/week @ 2022-07-05 130/week @ 2022-07-12 48/week @ 2022-07-19 66/week @ 2022-07-26 49/week @ 2022-08-02 36/week @ 2022-08-09

205 downloads per month

MIT license

11KB
104 lines

tsu - toml encoding and decoding utilities,parse to toml

parse string to toml data

{
   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);
}

convert struct to toml string

[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());
}

read toml file and parse contents to toml data

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

convert_toml_to_json used to convert toml to 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 used to convert json to 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);
}

Dependencies

~0.8–1.6MB
~36K SLoC