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
// require enable the toml feature such as: // cargo run --example pathread -F toml use json_ops::ValuePath; fn main() { let str_toml = include_str!("./sample.toml"); let v: toml::Value = str_toml.parse().unwrap(); println!("original toml content:"); println!("{str_toml}"); println!("read by path:"); let root = v.path(); let ip = root / "ip" | ""; println!("/ip = {ip}"); let host = root / "host"; let ip = host / "ip" | ""; println!("/host/ip = {ip}"); let port = host / "port" | 0; println!("/host/port = {port}"); let name = v.path() / "service" / 0 / "name" | ""; println!("/service/0/name = {name}"); let desc = v.path() / "service" / 0 / "desc" | ""; println!("/service/0/desc = {desc}"); let name = v.pathto("service/1/name") | ""; println!("/service/1/name = {name}"); let desc = v.pathto("service.1.desc") | ""; println!("/service/1/desc = {desc}"); let int = root / "misc" / "int" | 0; let float = root / "misc" / "float" | 0.0; let tf = root / "misc" / "bool" | false; println!("/misc/int = {int}"); println!("/misc/float = {float}"); println!("/misc/bool = {tf}"); }