Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upDerive encodable for Url and subtypes. #27
Conversation
|
@SimonSapin ping |
|
Assuming this is to to serialize to JSON or something similar, do you really want to use the default implementation based on internal data structures rather than just serialize the URL as a string? (Sorry for the delay, I was on vacation.) |
|
Should |
|
I think it would probably be best to encode/decode from a url string, but for now deriving would be good just to allow people to serialize and de-serialize data which contains Url. |
|
How about this? impl<E, S: serialize::Encoder<E>> serialize::Encodable<S, E> for Url {
fn encode(&self, encoder: &mut S) -> Result<(), E> {
encoder.emit_str(self.to_string().as_slice())
}
}
impl<E, D: serialize::Decoder<E>> serialize::Decodable<D, E> for Url {
fn decode(decoder: &mut D) -> Result<Url, E> {
Url::parse(try!(decoder.read_str()).as_slice()).map_err(|error| {
decoder.error(format!("URL parsing error: {}", error).as_slice())
})
}
} |
|
That looks good. I must confess that I don't really know much about Encodable/Decodable. How would I actually use that? I know there is a decoder for Json, is there a string-decoder? Also, it would still be beneficial to derive encodable and decodable for all the sub-structures, I know we use Host in Iron, for instance, and I would still like to be able to encode/decode it. |
|
http://valve.github.io/blog/2014/08/25/json-serialization-in-rust-part-1/ and http://valve.github.io/blog/2014/08/26/json-serialization-in-rust-part-2/ explain these traits. But it sounds like you’re not actually using them. Why the PR in the first place? |
|
Some downstream users of Iron have tried to store Urls in structs they would be to derive encodable/decodable and have had to work around it. I tried to derive encodable/decodable for iron::Url, but since it uses url::Host, it doesn't work. |
|
Alright. I’d take a PR that adds impls similar to #27 (comment) |
|
Pushed the one I had for |
|
Cool. I think I will actually end up just writing a similar impl for iron::Url instead. Thanks for talking this through. |
reem commentedSep 6, 2014
Title says it all. Nice for downstream users.