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    /// An improper numeric value.
29    InvalidNumericValue(f64),
30    /// Unable to create a new Coordinate value.
31    InvalidCoordinate,
32    /// The URI scheme was not `geo:` as required by RFC 5870.
33    #[cfg(feature = "urn")]
34    InvalidUrnScheme,
35}
36
37// ---------------------------------------------------------------------------
38// Implementations
39// ---------------------------------------------------------------------------
40
41impl Display for Error {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match self {
44            Error::InvalidAngle(v, limit) => write!(
45                f,
46                "Invalid value `{v}` for angle; expecting `-{limit}..={limit}`."
47            ),
48            Error::InvalidLatitudeDegrees(v) => write!(
49                f,
50                "Invalid value `{v}` for latitude degrees; expecting `-90..=90`."
51            ),
52            Error::InvalidLongitudeDegrees(v) => write!(
53                f,
54                "Invalid value `{v}` for longitude degrees; expecting `-180..=180`."
55            ),
56            Error::InvalidMinutes(v) => {
57                write!(f, "Invalid value `{v}` for minutes; expecting `0..60`.")
58            }
59            Error::InvalidSeconds(v) => {
60                write!(f, "Invalid value `{v}` for seconds; expecting `0.0..60.0`.")
61            }
62            Error::InvalidCharacter(c, s) => {
63                write!(f, "Invalid character `{c}` parsing value `\"{s}\"`.")
64            }
65            Error::InvalidWhitespace(s) => {
66                write!(f, "Invalid whitespace parsing value `\"{s}\"`.")
67            }
68            Error::InvalidNumericFormat(s) => {
69                write!(f, "Invalid numeric format parsing value `\"{s}\"`.")
70            }
71            Error::InvalidCoordinate => {
72                write!(
73                    f,
74                    "Invalid `Latitude` and `Longitude` pair constructing `Coordinate`."
75                )
76            }
77            #[cfg(feature = "url")]
78            Error::InvalidUrnScheme => write!(f, "URI scheme must be `geo:`."),
79            Error::InvalidNumericValue(v) => write!(f, "Invalid floating point value, `{v}`"),
80        }
81    }
82}
83
84impl std::error::Error for Error {}