Coder Social home page Coder Social logo

Error accessing index about yaml-rust HOT 9 CLOSED

chyh1990 avatar chyh1990 commented on September 26, 2024
Error accessing index

from yaml-rust.

Comments (9)

Drogglbecher avatar Drogglbecher commented on September 26, 2024 1

Hi, I had a similar problem but I think I kinda find out how it works:

  • First of all you have to index your key as string, in your case gut_config["0"] but as I found out this thing would not work for numbers as keys at all, so when you use config0 or something this should be fine
  • Second thing: When you put your values in line after the key like important: true then you should access it with <your_yml_var>.as_str(). In case you take the notation with a empty line and - like
important:
  - true

you have to index the parsed yml with the number of "subvalues", so e.g. <your_yml_var>[0].as_str()

I hope this helps ;)

from yaml-rust.

dtolnay avatar dtolnay commented on September 26, 2024

Can you share the full code including the part where the YAML is loaded into config? I was not able to reproduce this.

from yaml-rust.

ThraaxSession avatar ThraaxSession commented on September 26, 2024

Sorry, I updated my post :)

from yaml-rust.

dtolnay avatar dtolnay commented on September 26, 2024

I still cannot reproduce this. Here is what I ran.

#!/bin/bash

cargo new --bin repro
cat >> repro/Cargo.toml <<-'EOF'
    yaml-rust = "=0.3.5"
EOF

cat > repro/src/main.rs <<-'EOF'
    extern crate yaml_rust;

    use std::error::Error;
    use std::fs::File;
    use std::io::Read;
    use std::path::Path;
    use yaml_rust::YamlLoader;
    use yaml_rust::yaml::Array;

    fn main() {
        static CONFIG_STRING: &'static str = "etc/GUS.yml";
        let cfg = get_yaml_config(&CONFIG_STRING);
        RTM { config: &cfg }.run();    
    }

    fn get_yaml_config(config_file: &str) -> Array {
        let path_to_file = Path::new(&config_file);
        let display = path_to_file.display();
        let mut fd = match File::open(&path_to_file) {
            Err(why) => panic!("couldn't open {}: {}", display, why.description()),
            Ok(file) => file
        };

        let mut content = String::new();
        match fd.read_to_string(&mut content) {
            Err(why) => panic!("couldn't read {}: {}", display, why.description()),
            Ok(_) => println!(""),
        };
        YamlLoader::load_from_str(&content).unwrap()
    }

    #[derive(Debug)]
    struct RTM<'a> {
        config: &'a Array
    }

    impl<'a> RTM<'a> {
        fn run(&self) {
            let gus_config = &self.config[0];
            println!("{:?}", gus_config[0]);
        }
    }
EOF

mkdir repro/etc
cat > repro/etc/GUS.yml <<-'EOF'
0:
    important: true

1:
    important: false
EOF

cd repro
cargo run

from yaml-rust.

ThraaxSession avatar ThraaxSession commented on September 26, 2024

Do you see any issue? :(
I get a BadValue on my code and on the bash script what you wrote. Why do I get a BadValue?

gino@gino-xps13:/tmp/test$ bash test.sh
     Created binary (application) `repro` project
    Updating registry `https://github.com/rust-lang/crates.io-index`
   Compiling yaml-rust v0.3.5
   Compiling repro v0.1.0 (file:///tmp/test/repro)
    Finished debug [unoptimized + debuginfo] target(s) in 3.63 secs
     Running `target/debug/repro`

BadValue

if I would change the 0 in yaml to a string like MyPackage it would work... I expect I could access to integers too.

from yaml-rust.

ThraaxSession avatar ThraaxSession commented on September 26, 2024

Ping still got this issue... now I tried following, iterating through a list of integer, casting to string and use this string as index for the yaml array. But it didn't work :( what type is valid for the array index?

from yaml-rust.

ThraaxSession avatar ThraaxSession commented on September 26, 2024

:( something new? Still on this issue.

from yaml-rust.

ThraaxSession avatar ThraaxSession commented on September 26, 2024

Wtf? There is no issue closed. It's still up.

from yaml-rust.

ThraaxSession avatar ThraaxSession commented on September 26, 2024

@Drogglbecher vielen Dank Sebastian! Ich probiere das heute Abend aus! (I'll try later!) 👍

from yaml-rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.