These are chat archives for rust-lang/rust

12th
Oct 2018
cubetastic
@cubetastic33
Oct 12 2018 01:50
I am using yaml_rust
After I load the yaml using YamlLoader::load_from_str(), how do I modify the values?
Kelly Thomas Kline
@kellytk
Oct 12 2018 03:23
How do you decide when to use Cell for a struct's fields vs keeping a mutable reference to the struct?
trsh
@trsh
Oct 12 2018 09:32
Any idea how to unescape a serde_json::to_string generated JSON string?
trsh
@trsh
Oct 12 2018 09:41
Hmm, seems like it's not serde it self.. nm
cubetastic
@cubetastic33
Oct 12 2018 12:16
Does nobody here know how I modify the values?
Denis Lisov
@tanriol
Oct 12 2018 12:17
What's the problem with that?
cubetastic
@cubetastic33
Oct 12 2018 12:18
Well, I'm unable to simply assign the value. Let me pull up an example, give me a sec
image.png
Ryan Levick
@rylev
Oct 12 2018 12:22
Seems you need to use the Yaml enum: https://docs.serde.rs/yaml_rust/yaml/enum.Yaml.html
cubetastic
@cubetastic33
Oct 12 2018 12:23
image.png
I also tried that.
I was still modifying the example to paste it here, sorry it took so long
Ryan Levick
@rylev
Oct 12 2018 12:23
What ever is returned from docs[0]["id"] is immutable. Sorry I've never used the crate before...
Where does YamlLoader come from?
cubetastic
@cubetastic33
Oct 12 2018 12:24
Yeah, so I thought there might be a simple way to do this, that's why I asked it here... Weirdly though, making docs mutable doesn't seem to h have an effect
Ryan Levick
@rylev
Oct 12 2018 12:25
marking a variable as mut makes the variable as mutable - the underlying value is still immutable it seems
cubetastic
@cubetastic33
Oct 12 2018 12:25
extern crate yaml_rust;

use yaml_rust::{Yaml, YamlLoader, YamlEmitter};
Denis Lisov
@tanriol
Oct 12 2018 12:27
Try docs[0].as_hash().expect("document should be a hash")[Yaml::from_str("id")] = Yaml::Integer(2);
Yaml seems to be missing IndexMut implementations that would simplify that. You can try adding them to yaml_rust
cubetastic
@cubetastic33
Oct 12 2018 12:27
why should the index be a Yaml?
Ryan Levick
@rylev
Oct 12 2018 12:28
read_from_string returns a Vec<Yaml>
so you're first index is getting a Yaml and then your second index is calling the index function on Yaml which returns a &Yaml
cubetastic
@cubetastic33
Oct 12 2018 12:28
I mean why [Yaml::from_str("id")]
as_hash returns a Hash
Which is an alias for: LinkedHashMap<Yaml, Yaml>
Denis Lisov
@tanriol
Oct 12 2018 12:29
In YAML, a dictionary (map/hash/however you call it) key can be an arbitrary YAML value -- including arrays(!) or even other dictionaries(!!)
Ryan Levick
@rylev
Oct 12 2018 12:34
One last thing: if you need a value of type Yaml again you'll have to wrap the Hash again in Yaml::Hash
cubetastic
@cubetastic33
Oct 12 2018 13:48
I'm sorry, I had gone out
I'm still getting an error...
image.png
Denis Lisov
@tanriol
Oct 12 2018 13:51
...ok, sorry, I was slightly wrong - you'll have to match on it manually instead of the as_hashcall
cubetastic
@cubetastic33
Oct 12 2018 13:51
I don't understand...
Denis Lisov
@tanriol
Oct 12 2018 13:52
Let's actually start with another question: do you work with some "arbitrary" YAML data or with something that has a known and fixed schema?
cubetastic
@cubetastic33
Oct 12 2018 13:53
The structure is kind of known. For example - I know what the keys will be and what the value datatypes should be
Denis Lisov
@tanriol
Oct 12 2018 13:56
But you need to read multiple YAML documents from one stream, correct?
cubetastic
@cubetastic33
Oct 12 2018 13:57
what does "one stream" mean?
Denis Lisov
@tanriol
Oct 12 2018 13:58
One file in your snippet.
cubetastic
@cubetastic33
Oct 12 2018 13:59
well - how can you read multiple YAML documents from one file? Aren't "document" and "file" synonymous?
James McCoy
@jamessan
Oct 12 2018 14:15
No. You can have multiple YAML documents in a single stream/file -- http://yaml.org/spec/1.2/spec.html#YAML
cubetastic
@cubetastic33
Oct 12 2018 14:17
@jamessan, @tanriol I don't think I'll have more than one YAML document
Denis Lisov
@tanriol
Oct 12 2018 14:52
Then I'd suggest deserializing to regular structures with serde_yaml
cubetastic
@cubetastic33
Oct 12 2018 15:09
@tanriol sounds like a good idea. Thanks! ;)
Ash
@ashthespy
Oct 12 2018 15:14
greetings - question about serde. I am trying to deserialise json directly into a struct generated by the protobuf crate. I am doing this with the #[serde(deserialize_with = <my function> now. But is there a simpler way to do this?
Franz Gregor
@fzgregor
Oct 12 2018 15:53
Hi, by now I'm really confused by futures. I try to drive the hyper server manually. I use hyper::server::conn::Http to create a hyper::server::conn::Serve. As serve serves multiple connections it implementsfuture::Stream, I call .poll() on it giving me a hyper::server::conn::Connecting, which implements future::Future for a single connection. I tried to use .and_then and .then to drive the individual connection forward but I'm stuck with Connecting. It should become Connection which should become (). I don't understand this. hyper::server::conn::Connecting implements Future with Connection being its Item. I don't have to use .poll again, do I?
Vladislav
@vmarkushin
Oct 12 2018 16:14
@fzgregor yes, you should use and_then or something to turn Connecting into Connection. I think you should read more about how futures work.