I'm trying to parse a series of Json
objects with potential failures that cancel the whole function.
Ideally, I'd do something like:
fn .... -> Result<Vec<Video>, YoutubeParseError> {
...
let videos = try!(doc.find("items").
and_then(Json::as_array).
ok_or(YoutubeParseError));
Ok(videos.into_iter().
map(|item| try!(json_to_video(item))).
collect())
}
But of course try doesn't escape the map()
on error and instead of Result<Vec<Video>,_>
, I get Vec<Result<Video,_>>
. I could rewrite this as manual iteration adding elements into a new vec, but I feel like I'm missing some simpler way of handling this.
Is there some existing function that would get me from Iter<Result<T>>
to Result<Vec<T>,_>
easily?