解析YAML文件
在过去的三天中,我试图弄清楚如何在Rust中解析我的YAML。 而且我不知道为什么它不起作用。
我的YAML:
default_verbosity: 0
logging:
use_color: True,
log_color:
fatal: Red,
error: Red,
warn: Red,
info: Green,
debug: Blue,
trace: Yellow
log_output: file,
file_location: "example.log"
rocket:
mount_location: "/",
port: 8000
但是我的程序员在UNAWRAP线路上失败:让myyaml:config = serde_yaml :: frof_reader(yamlfile).unwrap();
带有此错误消息:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value:
Scan(ScanError { mark: Marker { index: 284, line: 14, col: 21 }, info: "while parsing
a block mapping, did not find expected key" })', src/main.rs:41:60
我的程序:我的程序:
use std::fs::File;
extern crate serde_yaml;
#[macro_use]
extern crate serde_derive;
#[derive(Debug, Serialize, Deserialize)]
struct ColorStruct {
fatal: String,
error: String,
warn: String,
info: String,
debug: String,
trace: String
}
#[derive(Debug, Serialize, Deserialize)]
struct LoggingStruct {
use_color: bool,
log_color: Vec<ColorStruct>,
log_output: String,
file_location: String
}
#[derive(Debug, Serialize, Deserialize)]
struct RocketStruct {
mount_location: String,
port: String
}
#[derive(Debug, Serialize, Deserialize)]
struct Config {
default_verbosity: i32,
logging: Vec<LoggingStruct>,
rocket: Vec<RocketStruct>
}
fn main(){
let yamlFile = File::open("config.yaml").unwrap();
let myYaml: Config = serde_yaml::from_reader(yamlFile).unwrap();
}
我真的很沮丧。我在做什么错?我的结构中缺少一些东西吗?
For the last 3 days now, I tried to figure out how to parse my yaml in Rust.
And I can't figure out why it doesn't work.
My Yaml:
default_verbosity: 0
logging:
use_color: True,
log_color:
fatal: Red,
error: Red,
warn: Red,
info: Green,
debug: Blue,
trace: Yellow
log_output: file,
file_location: "example.log"
rocket:
mount_location: "/",
port: 8000
But my programm failes at the unwrap line: let myYaml: Config = serde_yaml::from_reader(yamlFile).unwrap();
with this error message:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value:
Scan(ScanError { mark: Marker { index: 284, line: 14, col: 21 }, info: "while parsing
a block mapping, did not find expected key" })', src/main.rs:41:60
My program:
use std::fs::File;
extern crate serde_yaml;
#[macro_use]
extern crate serde_derive;
#[derive(Debug, Serialize, Deserialize)]
struct ColorStruct {
fatal: String,
error: String,
warn: String,
info: String,
debug: String,
trace: String
}
#[derive(Debug, Serialize, Deserialize)]
struct LoggingStruct {
use_color: bool,
log_color: Vec<ColorStruct>,
log_output: String,
file_location: String
}
#[derive(Debug, Serialize, Deserialize)]
struct RocketStruct {
mount_location: String,
port: String
}
#[derive(Debug, Serialize, Deserialize)]
struct Config {
default_verbosity: i32,
logging: Vec<LoggingStruct>,
rocket: Vec<RocketStruct>
}
fn main(){
let yamlFile = File::open("config.yaml").unwrap();
let myYaml: Config = serde_yaml::from_reader(yamlFile).unwrap();
}
I am really frustrated by this. What am I doing wrong? Am I missing something in my structs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的模式和YAML都错了。主要原因:
vec
。true
是字符串,true
是Bool。8000
不是字符串
,“ 8000”
是。mode=debug&; eDition = 2021&amp;gist = 011c713ab300fbdbd869cd55c6b0c6b0deda26b0deda26c”要使用
VEC
作为原始模式的一部分,您需要一些更改:colorscruct
应该是枚举,但是如果不是,您只需要将其作为其余示例。Both your schema and your yaml were wrong. Main reasons:
Vec
.True
is string,true
is bool.8000
is notString
,"8000"
is.Playground
If you really want to use
Vec
as part of your original schema, you would need some changes:ColorStruct
should be an enum, but if not you just need to keep as the remaining examples.Playground