Skip to content

Commit

Permalink
Include the child tag name into 'InvalidChildElement' error.
Browse files Browse the repository at this point in the history
This makes the error much more useful. Including the actual name of
a tag that caused problem makes it possible to figure out what's wrong
with the gpx file without having to go through the library code
finding out what did it expect.

PR: #7
  • Loading branch information
zudov authored and brendanashworth committed Feb 26, 2018
1 parent 58a26fc commit 92dbb56
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ error_chain!{
errors {
/// InvalidChildElement signifies when an element has a child that isn't
/// valid per the GPX spec.
InvalidChildElement(parent: &'static str) {
InvalidChildElement(child: String, parent: &'static str) {
description("invalid child element")
display("invalid child element in {}", String::from(*parent))
display("invalid child element '{}' in {}", child, String::from(*parent))
}

/// InvalidElementLacksAttribute signifies when an element is missing a
Expand Down
7 changes: 5 additions & 2 deletions src/parser/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<String> {

email = Some(format!("{id}@{domain}", id = id, domain = domain));
}
_ => Err(Error::from(ErrorKind::InvalidChildElement("email")))?,
child => Err(Error::from(ErrorKind::InvalidChildElement(
String::from(child),
"email",
)))?,
}
}

Expand Down Expand Up @@ -117,7 +120,7 @@ mod tests {
let err = consume!("<email id=\"id\" domain=\"domain\"><child /></email>").unwrap_err();

assert_eq!(err.description(), "invalid child element");
assert_eq!(err.to_string(), "invalid child element in email");
assert_eq!(err.to_string(), "invalid child element 'child' in email");
}

#[test]
Expand Down
5 changes: 4 additions & 1 deletion src/parser/gpx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<Gpx> {
"trk" => Ok(ParseEvent::StartTrack),
"wpt" => Ok(ParseEvent::StartWaypoint),
"gpx" => Ok(ParseEvent::Ignore),
_ => Err(Error::from(ErrorKind::InvalidChildElement("gpx")))?,
child => Err(Error::from(ErrorKind::InvalidChildElement(
String::from(child),
"gpx",
)))?,
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/parser/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<Link> {

link.href = attr.value;
}
_ => Err(Error::from(ErrorKind::InvalidChildElement("link")))?,
child => Err(Error::from(ErrorKind::InvalidChildElement(
String::from(child),
"link",
)))?,
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/parser/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<Metadata> {
"keywords" => Ok(ParseEvent::StartKeywords),
"time" => Ok(ParseEvent::StartTime),
"link" => Ok(ParseEvent::StartLink),
_ => Err(Error::from(ErrorKind::InvalidChildElement("metadata")))?,
child => Err(Error::from(ErrorKind::InvalidChildElement(
String::from(child),
"metadata",
)))?,
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/parser/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<Person> {
"link" => Ok(ParseEvent::StartLink),
"person" => Ok(ParseEvent::Ignore),
"author" => Ok(ParseEvent::Ignore),
_ => Err(Error::from(ErrorKind::InvalidChildElement("person"))),
child => Err(Error::from(ErrorKind::InvalidChildElement(
String::from(child),
"person",
))),
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/parser/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<Track> {
"src" => track.source = Some(string::consume(reader)?),
"type" => track._type = Some(string::consume(reader)?),
"trkseg" => track.segments.push(tracksegment::consume(reader)?),
_ => Err(Error::from(ErrorKind::InvalidChildElement("track")))?,
child => Err(Error::from(ErrorKind::InvalidChildElement(
String::from(child),
"track",
)))?,
},

XmlEvent::EndElement { .. } => {
Expand Down
5 changes: 4 additions & 1 deletion src/parser/tracksegment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<TrackSegment
match name.local_name.as_ref() {
"trkseg" => Ok(TrackSegmentEvent::StartTrkSeg),
"trkpt" => Ok(TrackSegmentEvent::StartTrkPt),
_ => Err(Error::from(ErrorKind::InvalidChildElement("tracksegment"))),
child => Err(Error::from(ErrorKind::InvalidChildElement(
String::from(child),
"tracksegment",
))),
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/parser/waypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<Waypoint> {

// Finally the GPX 1.1 extensions
"extensions" => extensions::consume(reader)?,
_ => Err(Error::from(ErrorKind::InvalidChildElement("waypoint")))?,
child => Err(Error::from(ErrorKind::InvalidChildElement(
String::from(child),
"waypoint",
)))?,
}
}

Expand Down

0 comments on commit 92dbb56

Please sign in to comment.