1use crate::{
4 Angle, Error,
5 fmt::{FormatOptions, Formatter, formatter_impl},
6 inner,
7 parse::{self, Parsed, Value},
8};
9use core::{
10 fmt::{Debug, Display, Write},
11 str::FromStr,
12};
13use ordered_float::OrderedFloat;
14
15#[cfg(feature = "serde")]
16use serde::{Deserialize, Serialize};
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
36#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
37pub struct Longitude(OrderedFloat<f64>);
38
39pub const INTERNATIONAL_REFERENCE_MERIDIAN: Longitude = Longitude(inner::ZERO);
45
46pub const ANTI_MERIDIAN: Longitude = Longitude(OrderedFloat(LONGITUDE_LIMIT));
48
49#[macro_export]
54macro_rules! long {
55 (E $degrees:expr, $minutes:expr, $seconds:expr) => {
56 long!($degrees.abs(), $minutes, $seconds).unwrap()
57 };
58 (W $degrees:expr, $minutes:expr, $seconds:expr) => {
59 long!(-$degrees.abs(), $minutes, $seconds).unwrap()
60 };
61 ($degrees:expr, $minutes:expr, $seconds:expr) => {
62 Longitude::new($degrees, $minutes, $seconds).unwrap()
63 };
64 (E $degrees:expr, $minutes:expr) => {
65 long!($degrees.abs(), $minutes).unwrap()
66 };
67 (W $degrees:expr, $minutes:expr) => {
68 long!(-$degrees.abs(), $minutes).unwrap()
69 };
70 ($degrees:expr, $minutes:expr) => {
71 long!($degrees, $minutes, 0.0).unwrap()
72 };
73 (E $degrees:expr) => {
74 long!($degrees.abs()).unwrap()
75 };
76 (W $degrees:expr) => {
77 long!(-$degrees.abs()).unwrap()
78 };
79 ($degrees:expr) => {
80 long!($degrees, 0, 0.0).unwrap()
81 };
82}
83
84const LONGITUDE_LIMIT: f64 = 180.0;
89
90impl Default for Longitude {
91 fn default() -> Self {
92 INTERNATIONAL_REFERENCE_MERIDIAN
93 }
94}
95
96impl TryFrom<f64> for Longitude {
97 type Error = Error;
98
99 fn try_from(value: f64) -> Result<Self, Self::Error> {
100 Self::try_from(OrderedFloat(value))
101 }
102}
103
104impl TryFrom<OrderedFloat<f64>> for Longitude {
105 type Error = Error;
106
107 fn try_from(value: OrderedFloat<f64>) -> Result<Self, Self::Error> {
108 if value.0 < -LONGITUDE_LIMIT || value.0 > LONGITUDE_LIMIT {
109 return Err(Error::InvalidAngle(value.into_inner(), LONGITUDE_LIMIT));
110 }
111 Ok(Self(value))
112 }
113}
114
115impl From<Longitude> for OrderedFloat<f64> {
116 fn from(value: Longitude) -> Self {
117 value.0.into()
118 }
119}
120
121impl From<Longitude> for f64 {
122 fn from(value: Longitude) -> Self {
123 value.0.into()
124 }
125}
126
127impl FromStr for Longitude {
128 type Err = Error;
129
130 fn from_str(s: &str) -> Result<Self, Self::Err> {
131 match parse::parse_str(s)? {
132 Parsed::Angle(Value::Unknown(decimal)) => Self::try_from(decimal),
133 Parsed::Angle(Value::Longitude(lon)) => Ok(lon),
134 _ => Err(Error::InvalidAngle(0.0, 0.0)),
135 }
136 }
137}
138
139impl Display for Longitude {
140 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
143 if f.alternate() {
144 let mut buf = String::new();
145 self.format(&mut buf, &FormatOptions::dms_signed())?;
146 f.write_str(&buf)
147 } else {
148 Display::fmt(&(self.0), f)
149 }
150 }
151}
152
153impl Formatter for Longitude {
154 fn format<W: Write>(&self, f: &mut W, fmt: &FormatOptions) -> std::fmt::Result {
155 let fmt = (*fmt).with_labels(('E', 'W'));
156 formatter_impl(self.0, f, &fmt)
157 }
158}
159
160impl Angle for Longitude {
161 const MIN: Self = Self(OrderedFloat(-LONGITUDE_LIMIT));
162 const MAX: Self = Self(OrderedFloat(LONGITUDE_LIMIT));
163
164 fn new(degrees: i32, minutes: u32, seconds: f32) -> Result<Self, Error> {
165 if degrees < Self::MIN.as_float().0 as i32 || degrees > Self::MAX.as_float().0 as i32 {
166 return Err(Error::InvalidLongitudeDegrees(degrees));
167 }
168 let float = inner::from_degrees_minutes_seconds(degrees, minutes, seconds)?;
169 Self::try_from(float).map_err(|_| Error::InvalidLongitudeDegrees(degrees))
170 }
171
172 fn as_float(&self) -> OrderedFloat<f64> {
173 self.0
174 }
175}
176
177impl Longitude {
178 #[must_use]
180 pub fn is_on_international_reference_meridian(&self) -> bool {
181 self.is_zero()
182 }
183
184 #[must_use]
186 pub fn is_western(&self) -> bool {
187 self.is_nonzero_negative()
188 }
189
190 #[must_use]
192 pub fn is_eastern(&self) -> bool {
193 self.is_nonzero_positive()
194 }
195}