georust / gpx

Rust read/write support for GPS Exchange Format (GPX)
https://crates.io/crates/gpx
MIT License
98 stars 44 forks source link

Relax behavior for empty `<text>` tags #92

Closed Bktero closed 1 year ago

Bktero commented 1 year ago

Hello

A friend sent me a .gpx file for a trip that we are planning. I tried to load the file with the following simple code:

use gpx;
use std::fs::File;
use std::io::BufReader;

fn main() {
    let file = File::open("traces/randonnee-37642110-3-beaulolais.gpx").unwrap();
    let reader = BufReader::new(file);
    let gpx = gpx::read(reader).unwrap();
    println!("{:?}", gpx);
}

I got this error:

thread 'main' panicked at 'called Result::unwrap() on an Err value: NoStringContent', src\main.rs:8:33

The metadata section of the .gpx file is as follow:

    <metadata>
        <name>3 BEAULOLAIS</name>
        <link href="">
            <text></text>
        </link>
    </metadata>

The application works properly if I remove <text></text> or add some text between the tags. This is coherent with the error, as the tag is empty (it has NoStringContent XD).

However, both Komoot and Garmin Connect load the file happily.

I believe the gpx library should have a more relaxed behavior when encountering an empty <text> tag and just ignore the tag, without raising any error.

w-flo commented 1 year ago

Looking at the GPX schema (that's all I can find for documentation), there appears to be nothing that forbids an empty string as the link text. That empty xsd:anyURI href would probably refer to the current document, so it should be okay, too.

I think it might be best to treat <text></text> as link.text = Some(""), and when the <text> element is missing as link.text = None, as it represents what is in the actual gpx file and allows to re-write the gpx file without changing that part of the file. Or should it be None in both cases to maybe make life easier for application developers?

There is also an optional <type> child element of <link>, which currently has the same issue.

w-flo commented 1 year ago

Github didn't notice the PR automatically for some reason. This should be fixed by #93