解析YAML文件

发布于 2025-01-28 02:54:48 字数 1693 浏览 2 评论 0原文

在过去的三天中,我试图弄清楚如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯到世界奔溃 2025-02-04 02:54:48

您的模式和YAML都错了。主要原因:

  • 您应该具有嵌套结构,而不是vec
  • 您的YAML类型不准确,例如true是字符串,true是Bool。 8000不是字符串“ 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();
}

mode=debug&; eDition = 2021&amp;gist = 011c713ab300fbdbd869cd55c6b0c6b0deda26b0deda26c”要使用VEC作为原始模式的一部分,您需要一些更改:

  • 可能colorscruct应该是枚举,但是如果不是,您只需要将其作为其余示例。
  • 您的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"
    }
]"#;

...

Both your schema and your yaml were wrong. Main reasons:

  • You should have nested structs, not Vec.
  • Your yaml types were not accurate, for example True is string, true is bool. 8000 is not String, "8000" is.
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();
}

Playground

If you really want to use Vec as part of your original schema, you would need some changes:

  • Probably ColorStruct should be an enum, but if not you just need to keep as the remaining examples.
  • Your yaml need to provide the data correcly too to match those types.

#[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"
    }
]"#;

...

Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文