1use crate::error::{check_azimuth, check_pressure, check_temperature, check_zenith_angle};
4use crate::{Error, Result};
5
6#[derive(Debug, Clone, Copy, PartialEq)]
10pub enum Horizon {
11 SunriseSunset,
13 CivilTwilight,
15 NauticalTwilight,
17 AstronomicalTwilight,
19 Custom(f64),
21}
22
23impl Horizon {
24 #[must_use]
28 pub const fn elevation_angle(&self) -> f64 {
29 match self {
30 Self::SunriseSunset => -0.83337, Self::CivilTwilight => -6.0,
32 Self::NauticalTwilight => -12.0,
33 Self::AstronomicalTwilight => -18.0,
34 Self::Custom(angle) => *angle,
35 }
36 }
37
38 pub fn custom(elevation_degrees: f64) -> Result<Self> {
43 if !(-90.0..=90.0).contains(&elevation_degrees) {
44 return Err(Error::invalid_elevation_angle(elevation_degrees));
45 }
46 Ok(Self::Custom(elevation_degrees))
47 }
48}
49
50impl Eq for Horizon {}
51
52impl core::hash::Hash for Horizon {
53 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
54 match self {
55 Self::SunriseSunset => 0.hash(state),
56 Self::CivilTwilight => 1.hash(state),
57 Self::NauticalTwilight => 2.hash(state),
58 Self::AstronomicalTwilight => 3.hash(state),
59 Self::Custom(angle) => {
60 4.hash(state);
61 angle.to_bits().hash(state);
63 }
64 }
65 }
66}
67
68#[derive(Debug, Clone, Copy, PartialEq)]
87pub struct RefractionCorrection {
88 pressure: f64,
90 temperature: f64,
92}
93
94impl RefractionCorrection {
95 pub fn new(pressure: f64, temperature: f64) -> Result<Self> {
108 check_pressure(pressure)?;
109 check_temperature(temperature)?;
110 Ok(Self {
111 pressure,
112 temperature,
113 })
114 }
115
116 #[must_use]
130 pub const fn standard() -> Self {
131 Self {
132 pressure: 1013.25,
133 temperature: 15.0,
134 }
135 }
136
137 #[must_use]
139 pub const fn pressure(&self) -> f64 {
140 self.pressure
141 }
142
143 #[must_use]
145 pub const fn temperature(&self) -> f64 {
146 self.temperature
147 }
148}
149
150#[derive(Debug, Clone, Copy, PartialEq)]
158pub struct SolarPosition {
159 azimuth: f64,
161 zenith_angle: f64,
163}
164
165impl SolarPosition {
166 pub fn new(azimuth: f64, zenith_angle: f64) -> Result<Self> {
180 let normalized_azimuth = check_azimuth(azimuth)?;
181 let validated_zenith = check_zenith_angle(zenith_angle)?;
182
183 Ok(Self {
184 azimuth: normalized_azimuth,
185 zenith_angle: validated_zenith,
186 })
187 }
188
189 #[must_use]
191 pub const fn azimuth(&self) -> f64 {
192 self.azimuth
193 }
194
195 #[must_use]
197 pub const fn zenith_angle(&self) -> f64 {
198 self.zenith_angle
199 }
200
201 #[must_use]
205 pub fn elevation_angle(&self) -> f64 {
206 90.0 - self.zenith_angle
207 }
208
209 #[must_use]
211 pub fn is_sun_up(&self) -> bool {
212 self.elevation_angle() > 0.0
213 }
214
215 #[must_use]
217 pub fn is_sun_down(&self) -> bool {
218 self.elevation_angle() <= 0.0
219 }
220}
221
222#[derive(Debug, Clone, PartialEq, Eq)]
227pub enum SunriseResult<T = chrono::DateTime<chrono::Utc>> {
228 RegularDay {
230 sunrise: T,
232 transit: T,
234 sunset: T,
236 },
237 AllDay {
239 transit: T,
241 },
242 AllNight {
244 transit: T,
246 },
247}
248
249impl<T> SunriseResult<T> {
250 pub const fn transit(&self) -> &T {
252 match self {
253 Self::RegularDay { transit, .. }
254 | Self::AllDay { transit }
255 | Self::AllNight { transit } => transit,
256 }
257 }
258
259 pub const fn is_regular_day(&self) -> bool {
261 matches!(self, Self::RegularDay { .. })
262 }
263
264 pub const fn is_polar_day(&self) -> bool {
266 matches!(self, Self::AllDay { .. })
267 }
268
269 pub const fn is_polar_night(&self) -> bool {
271 matches!(self, Self::AllNight { .. })
272 }
273
274 pub const fn sunrise(&self) -> Option<&T> {
276 if let Self::RegularDay { sunrise, .. } = self {
277 Some(sunrise)
278 } else {
279 None
280 }
281 }
282
283 pub const fn sunset(&self) -> Option<&T> {
285 if let Self::RegularDay { sunset, .. } = self {
286 Some(sunset)
287 } else {
288 None
289 }
290 }
291}
292
293#[cfg(test)]
294mod tests {
295 use super::*;
296
297 #[test]
298 fn test_horizon_elevation_angles() {
299 assert_eq!(Horizon::SunriseSunset.elevation_angle(), -0.83337);
300 assert_eq!(Horizon::CivilTwilight.elevation_angle(), -6.0);
301 assert_eq!(Horizon::NauticalTwilight.elevation_angle(), -12.0);
302 assert_eq!(Horizon::AstronomicalTwilight.elevation_angle(), -18.0);
303
304 let custom = Horizon::custom(-3.0).unwrap();
305 assert_eq!(custom.elevation_angle(), -3.0);
306
307 assert!(Horizon::custom(-95.0).is_err());
308 assert!(Horizon::custom(95.0).is_err());
309 }
310
311 #[test]
312 fn test_solar_position_creation() {
313 let pos = SolarPosition::new(180.0, 45.0).unwrap();
314 assert_eq!(pos.azimuth(), 180.0);
315 assert_eq!(pos.zenith_angle(), 45.0);
316 assert_eq!(pos.elevation_angle(), 45.0);
317 assert!(pos.is_sun_up());
318 assert!(!pos.is_sun_down());
319
320 let pos = SolarPosition::new(-90.0, 90.0).unwrap();
322 assert_eq!(pos.azimuth(), 270.0);
323 assert_eq!(pos.elevation_angle(), 0.0);
324
325 assert!(SolarPosition::new(0.0, -1.0).is_err());
327 assert!(SolarPosition::new(0.0, 181.0).is_err());
328 }
329
330 #[test]
331 fn test_solar_position_sun_state() {
332 let above_horizon = SolarPosition::new(180.0, 30.0).unwrap();
333 assert!(above_horizon.is_sun_up());
334 assert!(!above_horizon.is_sun_down());
335
336 let on_horizon = SolarPosition::new(180.0, 90.0).unwrap();
337 assert!(!on_horizon.is_sun_up());
338 assert!(on_horizon.is_sun_down());
339
340 let below_horizon = SolarPosition::new(180.0, 120.0).unwrap();
341 assert!(!below_horizon.is_sun_up());
342 assert!(below_horizon.is_sun_down());
343 }
344
345 #[test]
346 fn test_sunrise_result_regular_day() {
347 use chrono::{DateTime, Utc};
348
349 let sunrise = "2023-06-21T05:30:00Z".parse::<DateTime<Utc>>().unwrap();
350 let transit = "2023-06-21T12:00:00Z".parse::<DateTime<Utc>>().unwrap();
351 let sunset = "2023-06-21T18:30:00Z".parse::<DateTime<Utc>>().unwrap();
352
353 let result = SunriseResult::RegularDay {
354 sunrise,
355 transit,
356 sunset,
357 };
358
359 assert!(result.is_regular_day());
360 assert!(!result.is_polar_day());
361 assert!(!result.is_polar_night());
362 assert_eq!(result.transit(), &transit);
363 assert_eq!(result.sunrise(), Some(&sunrise));
364 assert_eq!(result.sunset(), Some(&sunset));
365 }
366
367 #[test]
368 fn test_sunrise_result_polar_day() {
369 use chrono::{DateTime, Utc};
370
371 let transit = "2023-06-21T12:00:00Z".parse::<DateTime<Utc>>().unwrap();
372 let result = SunriseResult::AllDay { transit };
373
374 assert!(!result.is_regular_day());
375 assert!(result.is_polar_day());
376 assert!(!result.is_polar_night());
377 assert_eq!(result.transit(), &transit);
378 assert_eq!(result.sunrise(), None);
379 assert_eq!(result.sunset(), None);
380 }
381
382 #[test]
383 fn test_sunrise_result_polar_night() {
384 use chrono::{DateTime, Utc};
385
386 let transit = "2023-12-21T12:00:00Z".parse::<DateTime<Utc>>().unwrap();
387 let result = SunriseResult::AllNight { transit };
388
389 assert!(!result.is_regular_day());
390 assert!(!result.is_polar_day());
391 assert!(result.is_polar_night());
392 assert_eq!(result.transit(), &transit);
393 assert_eq!(result.sunrise(), None);
394 assert_eq!(result.sunset(), None);
395 }
396
397 #[test]
398 fn test_refraction_correction() {
399 let standard = RefractionCorrection::standard();
401 assert_eq!(standard.pressure(), 1013.25);
402 assert_eq!(standard.temperature(), 15.0);
403
404 let custom = RefractionCorrection::new(1000.0, 20.0).unwrap();
406 assert_eq!(custom.pressure(), 1000.0);
407 assert_eq!(custom.temperature(), 20.0);
408
409 assert!(RefractionCorrection::new(-1.0, 15.0).is_err()); assert!(RefractionCorrection::new(1013.25, -300.0).is_err()); assert!(RefractionCorrection::new(3000.0, 15.0).is_err()); assert!(RefractionCorrection::new(1013.25, 150.0).is_err()); }
415}