Skip to main content

Module parse

Module parse 

Source
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

  1. Strings must not have leading or trailing whitespace. Error::InvalidWhitespace
  2. Whitespace must not appear between the sign and the degrees value. Error::InvalidWhitespace
  3. Degree, minute, and seconds values have a maximum number of integer digits, 3, 2, and 2 respectively. Error::InvalidNumericFormat
  4. The degrees symbol must be ‘°’, (Unicode U+00B0 DEGREE SIGN). Error::InvalidCharacter
  5. The minutes symbol must be ‘′’ (Unicode U+2032 PRIME). Error::InvalidCharacter
  6. The seconds symbol must be ‘″’ (Unicode U+2033 DOUBLE PRIME). Error::InvalidCharacter
  7. Whitespace must not appear between the degrees, minutes, and seconds values and their corresponding symbols. Error::InvalidWhitespace
  8. 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
  9. Bare format must start with the sign character [+|-] at the beginning of the string. Error::InvalidNumericFormat
  10. The latitude and longitude values of a coordinate may be specified in different formats.
  11. The latitude and longitude values of a coordinate must separated by a single comma , (separator).
  12. If either latitude and longitude are specified in a non-bare format, the separator may have whitespace before and after: \s*,\s*.
  13. If both latitude and longitude are specified in bare format, the separator must not have leading or trailing whitespace. Error::InvalidNumericFormat

§Parsed Examples

Input StringMatchResult
48.858222DecimalOk(Value(Unknown))
+48.858222DecimalOk(Value(Unknown))
48.858DecimalOk(Value(Unknown))
48.9DecimalOk(Value(Unknown))
48DecimalError(InvalidNumericFormat)
048.9DecimalOk(Value(Unknown))
0048.9DecimalError(InvalidNumericFormat)
-48.858222DecimalOk(Value(Unknown))
“ 48.858222“DecimalError(InvalidWhitespace)
“48.858222 “DecimalError(InvalidWhitespace)
- 48.858222DecimalError(InvalidCharacter)
48° 51′ 29.600000″Signed DMSOk(Value(Unknown))
-48° 51′ 29.600000″Signed DMSOk(Value(Unknown))
48° 51′ 29.600000″Signed DMSOk(Value(Unknown))
48° 51’ 29.600000″ NLabeled DMSError(InvalidCharacter)
48° 51′ 29.600000″ SLabeled DMSOk(Value(Latitude))
48° 51′ 29.600000″ ELabeled DMSOk(Value(Longitude))
48° 51′ 29.600000″ WLabeled DMSOk(Value(Longitude))
48° 51′ 29.600000″ wLabeled DMSError(InvalidCharacter)
+048:51:29.600000Bare DMSOk(Value(Unknown))
-048:51:29.600000Bare DMSOk(Value(Unknown))
91, 0, 0.0Signed DMSError(InvalidDegrees)
90, 61, 0.0Signed DMSError(InvalidMinutes)
90, 0, 61.0Signed DMSError(InvalidSeconds)
180, 1, 0.0Signed DMSError(InvalidAngle)
48° 51′ 29.600000″, 73° 59′ 8.400000″Signed+SignedOk(Coordinate)
48° 51′ 29.600000″ N, 73° 59′ 8.400000″ ELabeled+LabeledOk(Coordinate)
48° 51′ 29.600000″ W, 73° 59′ 8.400000″ NLabeled+LabeledError(InvalidLatitude)
48° 51′ 29.600000″ X, 73° 59′ 8.400000″ YLabeled+LabeledError(InvalidCharacter)
48.858222, -73.985667Decimal+DecimalOk(Coordinate)
+048:51:29.600000, 73° 59′ 8.400000″Bare+SignedOk(Coordinate)
48° 51′ 29.600000″, 73.985667Signed+DecimalOk(Coordinate)
48.858222, 73° 59′ 8.400000″Decimal+SignedOk(Coordinate)
48° 51′ 29.600000″, -73.985667Signed+DecimalOk(Coordinate)
-48.858222, 73° 59′ 8.400000″Decimal+SignedOk(Coordinate)
+048:51:29.600000,-073:59:08.400000Bare+BareOk(Coordinate)
+048:51:29.600000, -073:59:08.400000Bare+BareError(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());

Enums§

Parsed
The result of a successful parse_str call.
Value

Functions§

parse_str
Parse a string into a Parsed enum.