A Rust crate for parsing and generating uniform resource identifiers for geographic locations (geo URIs) according to RFC 5870.
Go to file
Paul van Tilburg 8201b4d17b
Bump the version to 0.1.1
2022-09-30 20:52:08 +02:00
src Include examples from RFC 5870 as tests 2022-09-29 22:24:00 +02:00
.gitignore Initial import into Git 2022-09-27 17:10:45 +02:00
CHANGELOG.md Add a changelog 2022-09-30 20:50:57 +02:00
Cargo.toml Bump the version to 0.1.1 2022-09-30 20:52:08 +02:00
LICENSE Add README.md and LICENSE file 2022-09-27 18:00:03 +02:00
README.md Update the examples in README.md; tweak text 2022-09-30 08:08:03 +02:00

README.md

geo-uri-rs

A Rust crate for uniform resource identifiers for geographic locations (geo URIs) according to IEEE RFC 5870. This crate supports parsing and generating geo URIs in the correct format. Its parser is currently somewhat more liberal than the proposed standard.

It supports geolocations specified by latitude and longitude, but also optionally altitude and an uncertainty radius. The currently only supported coordinate reference system is WGS-84.

Usage

Just run the following to add this library to your project:

$ cargo add geo-uri
    Updating crates.io index
      Adding thiserror vX.Y.Z to dependencies.

Parsing

Use either the FromStr or TryFrom traits to parse a geo URI string:

use geo_uri::GeoUri;

let geo_uri = GeoUri::try_from("geo:52.107,5.134,3.6;u=1000").expect("valid geo URI");
assert_eq!(geo_uri.latitude(), 52.107);
assert_eq!(geo_uri.longitude(), 5.134);
assert_eq!(geo_uri.altitude(), Some(3.6));
assert_eq!(geo_uri.uncertainty(), Some(1000.0));

use std::str::FromStr;
let geo_uri = GeoUri::from_str("geo:52.107,5.134;u=2000.0").expect("valid geo URI");
assert_eq!(geo_uri.latitude(), 52.107);
assert_eq!(geo_uri.longitude(), 5.134);
assert_eq!(geo_uri.altitude(), None);
assert_eq!(geo_uri.uncertainty(), Some(2000.0));

It is also possible to call the parse function directly:

use geo_uri::GeoUri;

let geo_uri = GeoUri::parse("geo:52.107,5.134,3.6").expect("valid geo URI");
assert_eq!(geo_uri.latitude(), 52.107);
assert_eq!(geo_uri.longitude(), 5.134);
assert_eq!(geo_uri.altitude(), Some(3.6));
assert_eq!(geo_uri.uncertainty(), None);

Generating

Use the GeoUriBuilder to construct a GeoUri struct. Then, use either the ToString or Display trait to generate an geo URI string:

use geo_uri::GeoUri;

let geo_uri = GeoUri::builder()
    .latitude(52.107)
    .longitude(5.134)
    .uncertainty(1_000.0)
    .build()
    .unwrap();

assert_eq!(
    geo_uri.to_string(),
    String::from("geo:52.107,5.134;u=1000")
);
assert_eq!(
    format!("{geo_uri}"),
    String::from("geo:52.107,5.134;u=1000")
);

License

geo-uri-rs is licensed under the MIT license (see the LICENSE file or http://opensource.org/licenses/MIT).