Skip to main content

lat_long/
error.rs

1//! This module provides the [`Error`] type.
2
3use core::fmt::Display;
4
5// ---------------------------------------------------------------------------
6// Public Types
7// ---------------------------------------------------------------------------
8
9/// Errors that can occur when constructing or parsing coordinate values.
10#[derive(Debug, Clone, PartialEq)]
11pub enum Error {
12    /// Generic angle out of range; used internally before conversion to a typed error.
13    InvalidAngle(f64, f64),
14    /// Latitude degrees outside the valid range −90..=90.
15    InvalidLatitudeDegrees(i32),
16    /// Longitude degrees outside the valid range −180..=180.
17    InvalidLongitudeDegrees(i32),
18    /// Minutes value ≥ 60 (valid range is 0..59).
19    InvalidMinutes(u32),
20    /// Seconds value < 0.0 or ≥ 60.0 (valid range is 0.0..60.0).
21    InvalidSeconds(f32),
22    /// An unexpected character was encountered while parsing.
23    InvalidCharacter(char, String),
24    /// Invalid whitespace between DMS components.
25    InvalidWhitespace(String),
26    /// An improperly formatted numeric value.
27    InvalidNumericFormat(String),
28    /// Unable to create a new Coordinate value.
29    InvalidCoordinate,
30    /// The URI scheme was not `geo:` as required by RFC 5870.
31    #[cfg(feature = "urn")]
32    InvalidUrnScheme,
33}
34
35// ---------------------------------------------------------------------------
36// Implementations
37// ---------------------------------------------------------------------------
38
39impl Display for Error {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            Error::InvalidAngle(v, limit) => write!(
43                f,
44                "Invalid value `{v}` for angle; expecting `-{limit}..={limit}`."
45            ),
46            Error::InvalidLatitudeDegrees(v) => write!(
47                f,
48                "Invalid value `{v}` for latitude degrees; expecting `-90..=90`."
49            ),
50            Error::InvalidLongitudeDegrees(v) => write!(
51                f,
52                "Invalid value `{v}` for longitude degrees; expecting `-180..=180`."
53            ),
54            Error::InvalidMinutes(v) => {
55                write!(f, "Invalid value `{v}` for minutes; expecting `0..60`.")
56            }
57            Error::InvalidSeconds(v) => {
58                write!(f, "Invalid value `{v}` for seconds; expecting `0.0..60.0`.")
59            }
60            Error::InvalidCharacter(c, s) => {
61                write!(f, "Invalid character `{c}` parsing value `\"{s}\"`.")
62            }
63            Error::InvalidWhitespace(s) => {
64                write!(f, "Invalid whitespace parsing value `\"{s}\"`.")
65            }
66            Error::InvalidNumericFormat(s) => {
67                write!(f, "Invalid numeric format parsing value `\"{s}\"`.")
68            }
69            Error::InvalidCoordinate => {
70                write!(
71                    f,
72                    "Invalid `Latitude` and `Longitude` pair constructing `Coordinate`."
73                )
74            }
75            #[cfg(feature = "url")]
76            Error::InvalidUrnScheme => write!(f, "URI scheme must be `geo:`."),
77        }
78    }
79}
80
81impl std::error::Error for Error {}