Rust解析TOML,结构体序列化和反序列化
在Rust中,可以使用标准库中的toml
模块来解析和生成TOML格式的数据;serde
与toml
模块集成,可以将TOML字符串解析为任意Rust结构体,或将Rust结构体序列化为TOML格式的字符串。
TOML是一种简单、符合人体工程学且可读的配置格式;Rust的包管理器cargo
就使用TOML格式;
以下为Cargo.toml
文件的一个例子:
[package]
name = "a1"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
toml = "0.7.3"
添加依赖
要使用toml
和serde
,需添加依赖,编辑Cargo.toml
文件,添加以下内容:
[dependencies]
serde = { version = "1.0", features = ["derive"] }
toml = "0.7.3"
使用toml模块
toml模块提供一个代表TOML表的结构体toml::Table
,可以使用它来访问和操作TOML格式的数据;
pub type Table = Map<String, Value>;
其中,Value
表示toml::Value
,是一个代表TOML值的枚举:
pub enum Value {
String(String),
Integer(i64),
Float(f64),
Boolean(bool),
Datetime(Datetime),
Array(Array),
Table(Table),
}
解析TOML文档
解析TOML文档最简单的方法是通过Table
类型:
use toml::Table;
fn main(){
let toml_text = r#"
a = "perfcode.com"
b = true
[others]
c = 3
"#;
let value = toml_text.parse::<Table>().unwrap();
println!("{}\n{}\n{}",
value["a"].as_str().unwrap(),
value["b"].as_bool().unwrap(),
value["others"]["c"].as_integer().unwrap(),
);
}
程序运行结果
perfcode.com true 3
要获得toml::Table
的详细用法,请访问 Rust toml::Table详细用法
使用toml::from_str函数解析
toml模块提供一个toml::from_str
函数可将TOML文档转换成toml::Table
类型;修改前一个例子:
use toml::Table;
fn main(){
let toml_text = r#"
a = "perfcode.com"
b = true
[others]
c = 3
"#;
let value:Table = toml::from_str(&toml_text).unwrap();
println!("{}\n{}\n{}",
value["a"].as_str().unwrap(),
value["b"].as_bool().unwrap(),
value["others"]["c"].as_integer().unwrap(),
);
}
使用toml::to_string函数转换成字符串
可以使用toml::to_string
函数,将toml::Table
类型转换成TOML字符串:
use toml::Table;
fn main(){
let toml_text = r#"
a = "perfcode.com"
"#;
let mut value:Table = toml::from_str(&toml_text).unwrap();
value.insert("b".to_string(),toml::Value::String("www.perfcode.com".to_string()));
let new_toml = toml::to_string(&value).unwrap();
println!("{}",new_toml);
}
程序运行结果
a = "perfcode.com" b = "www.perfcode.com"
对结构体序列化和反序列化
一个TOML反序列化的例子:
use serde::{Deserialize,Serialize};
#[derive(Deserialize,Serialize)]
struct Config {
ip: String,
port: u16,
others: Others
}
#[derive(Deserialize,Serialize)]
struct Others {
salt: String,
use_whitelist: bool,
}
fn main(){
let tom_text = r#"
ip = "127.0.0.1"
port = 80
[others]
salt = "xxxxxx"
use_whitelist = false
"#;
let config:Config = toml::from_str(tom_text).unwrap();
println!("ip: {}\nport: {}\nsalt: {}\nuse_whitelist: {}",
config.ip,
config.port,
config.others.salt,
config.others.use_whitelist
);
}
程序运行结果
ip: 127.0.0.1 port: 80 salt: xxxxxx use_whitelist: false
一个TOML序列化的例子:
use serde::{Deserialize,Serialize};
#[derive(Deserialize,Serialize)]
struct Config {
ip: String,
port: u16,
others: Others
}
#[derive(Deserialize,Serialize)]
struct Others {
salt: String,
use_whitelist: bool,
}
fn main(){
// 序列化
let new_config = Config {
ip: "0.0.0.0".to_string(),
port: 80,
others: Others {
salt: "ssssssssss".to_string(),
use_whitelist: true
}
};
let new_toml = toml::to_string(&new_config).unwrap();
println!("{}",new_toml);
}
程序运行结果
ip = "0.0.0.0" port = 80 [others] salt = "ssssssssss" use_whitelist = true