Skip to content

Commit

Permalink
Issue #5 - Support GPX 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hfiguiere committed Jan 22, 2018
1 parent 8301136 commit 73db18f
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 106 deletions.
10 changes: 7 additions & 3 deletions src/parser/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use xml::reader::Events;
use xml::reader::XmlEvent;
use geo::Bbox;

use GpxVersion;

/// consume consumes an element as a nothing.
pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<Bbox<f64>> {
pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>, _version: GpxVersion) -> Result<Bbox<f64>> {
let mut element: Option<String> = None;
let mut bounds: Bbox<f64> = Bbox{ xmin: 0., xmax: 0., ymin: 0., ymax: 0. };
for event in reader {
Expand Down Expand Up @@ -80,14 +82,16 @@ mod tests {
use std::io::BufReader;
use xml::reader::EventReader;

use GpxVersion;
use super::consume;

#[test]
fn consume_bounds() {
let bounds = consume!(
"
<bounds minlat=\"45.487064362\" minlon=\"-74.031837463\" maxlat=\"45.701225281\" maxlon=\"-73.586273193\"/>
"
",
GpxVersion::Gpx11
);

assert!(bounds.is_ok());
Expand All @@ -101,7 +105,7 @@ mod tests {

#[test]
fn consume_bad_bounds() {
let bounds = consume!("<bounds minlat=\"32.4\" minlon=\"notanumber\"></wpt>");
let bounds = consume!("<bounds minlat=\"32.4\" minlon=\"notanumber\"></wpt>", GpxVersion::Gpx11);

assert!(bounds.is_err());
}
Expand Down
21 changes: 14 additions & 7 deletions src/parser/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use std::iter::Peekable;
use xml::reader::Events;
use xml::reader::XmlEvent;

use GpxVersion;

/// consume consumes a GPX email from the `reader` until it ends.
/// When it returns, the reader will be at the element after the end GPX email
/// tag.
pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<String> {
pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>, _version: GpxVersion) -> Result<String> {
let mut email: Option<String> = None;

while let Some(event) = reader.next() {
Expand Down Expand Up @@ -60,11 +62,13 @@ mod tests {
use std::io::BufReader;
use xml::reader::EventReader;

use GpxVersion;
use super::consume;

#[test]
fn consume_simple_email() {
let email = consume!("<email id=\"me\" domain=\"example.com\" />");
let email = consume!(
"<email id=\"me\" domain=\"example.com\" />", GpxVersion::Gpx11);

assert!(email.is_ok());

Expand All @@ -75,7 +79,8 @@ mod tests {

#[test]
fn consume_attrs_reversed() {
let email = consume!("<email domain=\"example.com\" id=\"me\" />");
let email = consume!(
"<email domain=\"example.com\" id=\"me\" />", GpxVersion::Gpx11);

assert!(email.is_ok());

Expand All @@ -86,7 +91,8 @@ mod tests {

#[test]
fn consume_err_no_id() {
let err = consume!("<email domain='example.com'/>").unwrap_err();
let err = consume!("<email domain='example.com'/>",
GpxVersion::Gpx11).unwrap_err();

assert_eq!(
err.description(),
Expand All @@ -100,7 +106,8 @@ mod tests {

#[test]
fn consume_err_no_domain() {
let err = consume!("<email id=\"gpx\" />").unwrap_err();
let err = consume!("<email id=\"gpx\" />",
GpxVersion::Gpx11).unwrap_err();

assert_eq!(
err.description(),
Expand All @@ -114,15 +121,15 @@ mod tests {

#[test]
fn consume_err_invalid_child_element() {
let err = consume!("<email id=\"id\" domain=\"domain\"><child /></email>").unwrap_err();
let err = consume!("<email id=\"id\" domain=\"domain\"><child /></email>", GpxVersion::Gpx11).unwrap_err();

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

#[test]
fn consume_err_no_ending_tag() {
let err = consume!("<email id=\"id\" domain=\"domain\">").unwrap_err();
let err = consume!("<email id=\"id\" domain=\"domain\">", GpxVersion::Gpx11).unwrap_err();

assert_eq!(err.description(), "error while parsing XML");
}
Expand Down
7 changes: 5 additions & 2 deletions src/parser/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use std::io::Read;
use xml::reader::Events;
use xml::reader::XmlEvent;

use GpxVersion;

/// consume consumes a single string as tag content.
pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<()> {
pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>, _version: GpxVersion) -> Result<()> {
let mut started = false;

for event in reader {
Expand Down Expand Up @@ -41,6 +43,7 @@ mod tests {
use std::io::BufReader;
use xml::reader::EventReader;

use GpxVersion;
use super::consume;

#[test]
Expand All @@ -50,7 +53,7 @@ mod tests {
hello world
<a><b cond=\"no\"><c>derp</c></b></a>
<tag>yadda yadda we dont care</tag>
</extensions>"
</extensions>", GpxVersion::Gpx11
);

assert!(result.is_ok());
Expand Down
22 changes: 13 additions & 9 deletions src/parser/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use xml::reader::Events;
use parser::string;
use types::Fix;

use GpxVersion;

/// consume consumes an element as a fix.
pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>) -> Result<Fix> {
let fix_string = string::consume(reader)?;
pub fn consume<R: Read>(reader: &mut Peekable<Events<R>>, version: GpxVersion) -> Result<Fix> {
let fix_string = string::consume(reader, version)?;

let fix = match fix_string.as_ref() {
"none" => Fix::None,
Expand All @@ -30,30 +32,32 @@ mod tests {
use xml::reader::EventReader;

use super::consume;

use GpxVersion;
use Fix;

#[test]
fn consume_fix() {
let result = consume!("<fix>dgps</fix>");
let result = consume!("<fix>dgps</fix>", GpxVersion::Gpx11);
assert!(result.is_ok());

let result = consume!("<fix>none</fix>");
let result = consume!("<fix>none</fix>", GpxVersion::Gpx11);
assert_eq!(result.unwrap(), Fix::None);

let result = consume!("<fix>2d</fix>");
let result = consume!("<fix>2d</fix>", GpxVersion::Gpx11);
assert_eq!(result.unwrap(), Fix::TwoDimensional);

let result = consume!("<fix>3d</fix>");
let result = consume!("<fix>3d</fix>", GpxVersion::Gpx11);
assert_eq!(result.unwrap(), Fix::ThreeDimensional);

let result = consume!("<fix>dgps</fix>");
let result = consume!("<fix>dgps</fix>", GpxVersion::Gpx11);
assert_eq!(result.unwrap(), Fix::DGPS);

let result = consume!("<fix>pps</fix>");
let result = consume!("<fix>pps</fix>", GpxVersion::Gpx11);
assert_eq!(result.unwrap(), Fix::PPS);

// Not in the specification
let result = consume!("<fix>KF_4SV_OR_MORE</fix>");
let result = consume!("<fix>KF_4SV_OR_MORE</fix>", GpxVersion::Gpx11);
assert_eq!(result.unwrap(), Fix::Other("KF_4SV_OR_MORE".to_owned()));
}
}
Loading

0 comments on commit 73db18f

Please sign in to comment.