Comments (9)
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 useconfig0
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.
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.
Sorry, I updated my post :)
from yaml-rust.
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.
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.
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.
:( something new? Still on this issue.
from yaml-rust.
Wtf? There is no issue closed. It's still up.
from yaml-rust.
@Drogglbecher vielen Dank Sebastian! Ich probiere das heute Abend aus! (I'll try later!) 👍
from yaml-rust.
Related Issues (20)
- Add support for YAML 1.1 HOT 1
- Support alternate line endings
- Bug in anchor handling? HOT 2
- Panic uninitialised linked hash map HOT 2
- can not part ref HOT 1
- Doesn't seem possible to iteratate over results HOT 3
- misleading? HOT 1
- is it wasm compatible? HOT 1
- Dupliacte keys are not detected as invalid YAML
- Add API to emit YAML strings (i.e. `need_quotes` and `escape_str`)
- tabs are not allowed as the first character of a block scalar
- `Parser` parses empty scalar as '~' HOT 1
- Support no-std with alloc crate
- Integration with google oss-fuzz fuzzing service
- Maintainers HOT 8
- unsafe-libyaml looks... unsafe? HOT 1
- Dynamically choosing a YAML document path? HOT 1
- `True` and `False` boolean literals seem to be parsed as strings HOT 2
- Is this crate maintained? HOT 4
- Read This First ~ Switch to the actively maintained yaml-rust2 fork
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yaml-rust.