在過去的 3 天里,我試圖弄清楚如何在 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
但是我的程式在展開行失敗:let myYaml: Config = serde_yaml::from_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();
}
我對此感到非常沮喪。我究竟做錯了什么?我的結構中是否缺少某些內容?
uj5u.com熱心網友回復:
您的架構和 yaml 都是錯誤的。主要原因:
- 你應該有嵌套結構,而不是
Vec. - 您的 yaml 型別不準確,例如
True是字串,true是布林值。8000不是String,"8000"是。
use std::fs::File;
use serde_yaml; // 0.8.23
use serde::{Serialize, Deserialize};
#[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: 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: LoggingStruct,
rocket: RocketStruct
}
fn main(){
let yamlFile = r#"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""#;
let myYaml: Config = serde_yaml::from_str(yamlFile).unwrap();
}
操場
如果您真的想將其Vec用作原始架構的一部分,則需要進行一些更改:
- 可能
ColorStruct應該是一個列舉,但如果不是,您只需保留其余示例。 - 您的 yaml 也需要正確地提供資料以匹配這些型別。
#[derive(Debug, Serialize, Deserialize)]
enum ColorStruct {
fatal(String),
error(String),
warn(String),
info(String),
debug(String),
trace(String),
}
...
let yamlFile = r#"default_verbosity: 0
logging: [
{
log_output: "file",
file_location: "example.log",
use_color: true,
log_color: [
{ fatal: "Red" },
{ error: "Red" },
{ warn: "Red" },
{ info: "Green" },
{ debug: "Blue" },
{ trace: "Yellow" }
]
}
]
rocket: [
{
mount_location: "/",
port: "8000"
}
]"#;
...
操場
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/473564.html
