Skip to content

Instantly share code, notes, and snippets.

@mattyhall
Created August 19, 2014 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattyhall/46fafc61c6e0eb1db35c to your computer and use it in GitHub Desktop.
Save mattyhall/46fafc61c6e0eb1db35c to your computer and use it in GitHub Desktop.
osmxml helper
fn parse_relation(&mut self, attrs: sax::Attributes) -> ParseResult {
let id = try!(self.parse_int_attr("id", attrs));
let mut tags = HashMap::new();
let mut members = Vec::new();
try!(helper(self, "relation", |name, attrs, osm| {
match name.as_slice() {
"member" => members.push(try!(osm.parse_member(attrs))),
"tag" => try!(osm.parse_tag(attrs, &mut tags)),
_ => return Err(ParseErr(format!("Relations can only have members and tags. Got {}",name)))
}
Ok(())
}));
self.elements.insert(id, Relation{id: id, members: members, tags: tags});
Ok(())
}
fn helper(osm: &mut Osm, tag: &'static str, c: |String, sax::Attributes, &mut Osm| -> ParseResult) -> ParseResult {
for event in osm.parser.iter() {
match event {
Ok(sax::StartElement(name, attrs)) => try!(c(name, attrs, osm)),
Ok(sax::EndElement(name)) => {
if name.as_slice() == tag.as_slice() {
break;
}
return Err(ParseErr(format!(
"Expecting tag to end. Instead got a {}", name)));
}
_ => ()
}
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment