我正在使用 cratetoml = "0.5.8"
来解析 TOML 文件。我的预期输出应该存储在这个结构中:
#[derive(Deserialize, Debug)]
pub struct Config {
pub watching: Vec<Stocks>,
}
#[derive(Deserialize, Debug)]
pub struct Stocks {
pub symbol: String,
}
这是我解析 TOML 文件的函数:
pub fn parse_toml_file(path: String) -> Config {
let content = fs::read_to_string(path).expect("Failed to access file.");
let toml_config: Config = toml::from_str(&content[..]).expect("Failed to parse file.");
toml_config
}
这就是我所说的:
let home: String = format!("{:?}", home_dir().unwrap());
let path: String = format!(
"{}/.config/stonks.toml",
home[1..home.len() - 1].to_string()
);
let cli = Cli::new(parse_toml_file(path));
这是content
变量的值:
"[watching]\nsymbol = [\"AAPL\", \"TSLA\"]\n"
这是我在创建toml_config
变量时遇到的错误:
thread 'main' panicked at 'Failed to parse file.: Error { inner: ErrorInner { kind: Custom, line: Some(0), col: 0, at: Some(0), message: "invalid type: map, expected a sequence", key: ["watching"] } }', src/parse.rs:19:60
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
如何修复此错误?内容按预期打印出来。