Expand description
This module provides the Parsed enum and parse_str function.
The parse_str function should proceed according to the following grammar:
<coordinate> ::= <bare_pair> | <non_bare_pair>
<bare_pair> ::= <bare_dms> "," <bare_dms>
<non_bare_pair> ::= ( <latitude> <separator> <longitude> )
| ( <bare_dms> <separator> <longitude> )
| ( <latitude> <separator> <bare_dms> )
<latitude> ::= <decimal> | <signed_dms> | <labeled_dms>
<longitude> ::= <decimal> | <signed_dms> | <labeled_dms>
<separator> ::= WHITESPACE* "," WHITESPACE*
<decimal> ::= <sign>? <digits> "." <digits>
<signed_dms> ::= <sign>? <degs> WHITESPACE* <mins> WHITESPACE* <secs>
<labeled_dms> ::= <degs> WHITESPACE* <mins> WHITESPACE* <secs> WHITESPACE* <direction>
<bare_dms> ::= <sign> <bare_degs> ":" <bare_mins> ":" <bare_secs>
<degs> ::= <digits> "°"
<mins> ::= <digits> "′"
<secs> ::= <digits> "." <digits> "″"
<bare_degs> ::= <digit> <digit> <digit>
<bare_mins> ::= <digit> <digit>
<bare_secs> ::= <digit> <digit> "." <digit> <digit> <digit> <digit>+
<direction> ::= "N" | "S" | "E" | "W"
<sign> ::= "+" | "-"
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<digits> ::= <digit>+§Format Notes
- Strings must not have leading or trailing whitespace.
Error::InvalidWhitespace - Whitespace must not appear between the sign and the degrees value.
Error::InvalidWhitespace - Degree, minute, and seconds values have a maximum number of integer digits, 3, 2, and 2 respectively.
Error::InvalidNumericFormat - The degrees symbol must be ‘°’, (Unicode
U+00B0DEGREE SIGN).Error::InvalidCharacter - The minutes symbol must be ‘′’ (Unicode
U+2032PRIME).Error::InvalidCharacter - The seconds symbol must be ‘″’ (Unicode
U+2033DOUBLE PRIME).Error::InvalidCharacter - Whitespace must not appear between the degrees, minutes, and seconds values and their corresponding symbols.
Error::InvalidWhitespace - Labeled format must have a direction character ‘N’, ‘S’, ‘E’, or ‘W’ at the end of the string, this character is case sensitive.
Error::InvalidCharacter - Bare format must start with the sign character [+|-] at the beginning of the string.
Error::InvalidNumericFormat - The latitude and longitude values of a coordinate may be specified in different formats.
- The latitude and longitude values of a coordinate must separated by a single comma
,(separator). - If either latitude and longitude are specified in a non-bare format, the separator may have whitespace before and after:
\s*,\s*. - If both latitude and longitude are specified in bare format, the separator must not have leading or trailing whitespace.
Error::InvalidNumericFormat
§Parsed Examples
| Input String | Match | Result |
|---|---|---|
| 48.858222 | Decimal | Ok(Value(Unknown)) |
| +48.858222 | Decimal | Ok(Value(Unknown)) |
| 48.858 | Decimal | Ok(Value(Unknown)) |
| 48.9 | Decimal | Ok(Value(Unknown)) |
| 48 | Decimal | Error(InvalidNumericFormat) |
| 048.9 | Decimal | Ok(Value(Unknown)) |
| 0048.9 | Decimal | Error(InvalidNumericFormat) |
| -48.858222 | Decimal | Ok(Value(Unknown)) |
| “ 48.858222“ | Decimal | Error(InvalidWhitespace) |
| “48.858222 “ | Decimal | Error(InvalidWhitespace) |
| - 48.858222 | Decimal | Error(InvalidCharacter) |
| 48° 51′ 29.600000″ | Signed DMS | Ok(Value(Unknown)) |
| -48° 51′ 29.600000″ | Signed DMS | Ok(Value(Unknown)) |
| 48° 51′ 29.600000″ | Signed DMS | Ok(Value(Unknown)) |
| 48° 51’ 29.600000″ N | Labeled DMS | Error(InvalidCharacter) |
| 48° 51′ 29.600000″ S | Labeled DMS | Ok(Value(Latitude)) |
| 48° 51′ 29.600000″ E | Labeled DMS | Ok(Value(Longitude)) |
| 48° 51′ 29.600000″ W | Labeled DMS | Ok(Value(Longitude)) |
| 48° 51′ 29.600000″ w | Labeled DMS | Error(InvalidCharacter) |
| +048:51:29.600000 | Bare DMS | Ok(Value(Unknown)) |
| -048:51:29.600000 | Bare DMS | Ok(Value(Unknown)) |
| 91, 0, 0.0 | Signed DMS | Error(InvalidDegrees) |
| 90, 61, 0.0 | Signed DMS | Error(InvalidMinutes) |
| 90, 0, 61.0 | Signed DMS | Error(InvalidSeconds) |
| 180, 1, 0.0 | Signed DMS | Error(InvalidAngle) |
| 48° 51′ 29.600000″, 73° 59′ 8.400000″ | Signed+Signed | Ok(Coordinate) |
| 48° 51′ 29.600000″ N, 73° 59′ 8.400000″ E | Labeled+Labeled | Ok(Coordinate) |
| 48° 51′ 29.600000″ W, 73° 59′ 8.400000″ N | Labeled+Labeled | Error(InvalidLatitude) |
| 48° 51′ 29.600000″ X, 73° 59′ 8.400000″ Y | Labeled+Labeled | Error(InvalidCharacter) |
| 48.858222, -73.985667 | Decimal+Decimal | Ok(Coordinate) |
| +048:51:29.600000, 73° 59′ 8.400000″ | Bare+Signed | Ok(Coordinate) |
| 48° 51′ 29.600000″, 73.985667 | Signed+Decimal | Ok(Coordinate) |
| 48.858222, 73° 59′ 8.400000″ | Decimal+Signed | Ok(Coordinate) |
| 48° 51′ 29.600000″, -73.985667 | Signed+Decimal | Ok(Coordinate) |
| -48.858222, 73° 59′ 8.400000″ | Decimal+Signed | Ok(Coordinate) |
| +048:51:29.600000,-073:59:08.400000 | Bare+Bare | Ok(Coordinate) |
| +048:51:29.600000, -073:59:08.400000 | Bare+Bare | Error(InvalidWhitespace) |
§Code Examples
Parse individual angles:
use lat_long::parse;
assert!(parse::parse_str("48.858222").is_ok());
assert!(parse::parse_str("-73.985667").is_ok());
assert!(parse::parse_str("48° 51′ 29.600000″ N").is_ok());
assert!(parse::parse_str("73° 59′ 8.400000″ W").is_ok());
assert!(parse::parse_str("+048:51:29.600000").is_ok());
assert!(parse::parse_str("-073:59:08.400000").is_ok());Parse coordinates:
use lat_long::parse;
assert!(parse::parse_str("48.858222, -73.985667").is_ok());
assert!(parse::parse_str("48° 51′ 29.600000″ N, 73° 59′ 8.400000″ W").is_ok());
assert!(parse::parse_str("+048:51:29.600000,-073:59:08.400000").is_ok());
assert!(parse::parse_str("+048:51:29.600000, 73° 59′ 8.400000″ W").is_ok());