I have a json:
{
"sync": {
"storage_type": "",
"local_disk": {
"data_dir_path": ""
},
"interval_seconds": 6
}
}
if I just parse this json string,it works OK:
let r= r##"
{
"sync": {
"storage_type": "",
"local_disk": {
"data_dir_path": ""
},
"interval_seconds": 6
}
}
"##;
match toml::to_string(&r) {
Ok(toml) => {
print!(">>> toml: {:?} \n", toml)
}
Err(e) => {
print!(">>> toml error: {:?} \n", e)
}
}
but,if I parse this json string into a struct,then convert the struct into toml string,it maybe cause an ValueAfterTable error.
Before convert into toml,print the struct converted from json string:
Conf { sync: Sync { storage_type: "", local_disk: SyncLocalDisk { data_dir_path: "" } } }
It as same as the json string.
The struct is:
#[derive(Serialize, Deserialize, Debug)]
struct SyncLocalDisk {
data_dir_path: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct Sync {
storage_type: String,
local_disk: SyncLocalDisk, // After comment this line or the flowlling line, there will have no error
interval_seconds: u32,
}
#[derive(Serialize, Deserialize, Debug)]
struct Encryption {
master_password: String,
entry_file_name: String,
file_ext: String,
enable_file_compress: bool,
}
#[derive(Serialize, Deserialize, Debug)]
struct Conf {
sync: Sync,
}
Or we can create a new Conf,then convert it, the result is the same :
let local_disk = SyncLocalDisk {
data_dir_path: "path".to_string(),
};
let sync = Sync {
storage_type: "storage".to_string(),
local_disk: local_disk,
interval_seconds: 8,
};
let ccc = Conf { sync: sync };
match toml::to_string(&ccc) {
Ok(toml) => {
print!(">>> toml: {:?} \n", toml)
}
Err(e) => {
print!(">>> toml error: {:?} \n", e)
}
}
I have test with 0.5.9 and 0.5.10.
What's the reason? And how to solve it?
Thankyou