1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
use std::sync::MutexGuard;
use std::sync::Mutex;
use toml::Value;
use serde::ser::Serialize;
/// ## toml_from_str used to convert string to toml data
/// ```ignore
///let cargo_toml_mutex = tsu::toml_from_str(r#"
///[package]
///name = "toml"
///version = "0.4.5"
///authors = ["Alex Crichton <alex@alexcrichton.com>"]
///
///[badges]
///travis-ci = { repository = "alexcrichton/toml-rs" }
///
///[dependencies]
///serde = "1.0"
///
///[dev-dependencies]
///serde_derive = "1.0"
///serde_json = "1.0"
///"#);
///
///let package = cargo_toml_mutex.get("package").unwrap();
///let authors = package.get("authors").unwrap();
///println!("{:#?}", &package);
///println!("{:#?}", &authors);
///
///let badges = cargo_toml_mutex.get("badges").unwrap();
///println!("{:#?}", &badges);
///
///let dependencies = cargo_toml_mutex.get("dependencies").unwrap();
///println!("{:#?}", &dependencies);
///
///let dev_dependencies = cargo_toml_mutex.get("dev-dependencies").unwrap();
///println!("{:#?}", &dev_dependencies);
/// ```
#[allow(warnings)]
pub fn toml_from_str(toml_str:impl ToString)-> MutexGuard<'static, Value> {
let toml_str_m = Box::leak(Box::new(Mutex::new(toml_str.to_string().parse::<Value>().unwrap())));
toml_str_m.lock().unwrap()
}
/// ## to_toml_str used to convert struct to toml string
/// ```ignore
/// 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);
/// println!("{}",toml.as_str());
/// ```
pub fn to_toml_str(toml_str:impl Serialize) -> MutexGuard<'static, String> {
let toml_str_m = Box::leak(Box::new(Mutex::new(toml::to_string(&toml_str).unwrap())));
toml_str_m.lock().unwrap()
}