interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi / src / runtime / time / integration_common.rs

176 lines · 5320 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Provides common helpers for the integration and conversion with
4//! different time crates from the ecosystem.
5
6use crate::runtime::TimeError;
7use core::error::Error;
8use core::fmt;
9use core::fmt::{Display, Formatter};
10
11/// Opaque error type indicating a UEFI [`Time`] could not be converted.
12///
13/// [`Time`]: super::Time
14#[derive(Debug)]
15pub struct TimeConversionError(pub(super) ConversionErrorInner);
16
17impl Display for TimeConversionError {
18 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
19 write!(f, "Time conversion error: {}", self.0)
20 }
21}
22
23impl Error for TimeConversionError {}
24
25#[derive(Debug)]
26pub(super) enum ConversionErrorInner {
27 /// Invalid component.
28 InvalidComponent,
29 /// Invalid UEFI time: [`Time::is_valid`] reported an error.
30 ///
31 /// [`Time::is_valid`]: super::Time::is_valid
32 InvalidUefiTime(TimeError),
33 /// A timezone was required for the conversion, but the UEFI time indicates
34 /// [`Time::UNSPECIFIED_TIMEZONE`].
35 ///
36 /// [`Time::UNSPECIFIED_TIMEZONE`]: super::Time::UNSPECIFIED_TIMEZONE
37 UnspecifiedTimezone,
38 /// Errors raised in the [`time`] crate.
39 #[cfg(feature = "time03")]
40 TimeCrateError(time::Error),
41 /// Errors raised in the [`jiff`] crate.
42 #[cfg(feature = "jiff02")]
43 JiffCrateError(jiff::Error),
44}
45
46impl Display for ConversionErrorInner {
47 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
48 match self {
49 Self::InvalidComponent => write!(f, "Invalid component"),
50 Self::InvalidUefiTime(e) => write!(f, "Invalid UEFI time: {e}"),
51 Self::UnspecifiedTimezone => write!(f, "Unspecified timezone"),
52 #[cfg(feature = "time03")]
53 Self::TimeCrateError(e) => write!(f, "Time crate error: {}", e),
54 #[cfg(feature = "jiff02")]
55 Self::JiffCrateError(e) => write!(f, "Jiff crate error: {}", e),
56 }
57 }
58}
59
60impl Error for ConversionErrorInner {
61 fn source(&self) -> Option<&(dyn Error + 'static)> {
62 match self {
63 Self::InvalidComponent => None,
64 Self::InvalidUefiTime(e) => Some(e),
65 Self::UnspecifiedTimezone => None,
66 #[cfg(feature = "time03")]
67 Self::TimeCrateError(e) => Some(e),
68 // None: Missing Error trait
69 #[cfg(feature = "jiff02")]
70 Self::JiffCrateError(_e) => None,
71 }
72 }
73}
74
75#[cfg(test)]
76pub(super) mod test_helpers {
77 use super::*;
78 use crate::runtime::TimeParams;
79 use uefi::runtime::Time;
80 use uefi_raw::time::Daylight;
81
82 pub fn sample_time(tz: i16) -> Time {
83 let params = TimeParams {
84 year: 2024,
85 month: 3,
86 day: 14,
87 hour: 12,
88 minute: 34,
89 second: 56,
90 nanosecond: 123_456_789,
91 time_zone: Some(tz),
92 daylight: Daylight::default(),
93 };
94 Time::new(params).unwrap()
95 }
96
97 fn invalid_date() -> Time {
98 let mut t = sample_time(0);
99 t.0.month = 2;
100 t.0.day = 31;
101 t
102 }
103
104 pub fn primitive_roundtrip<T>()
105 where
106 T: TryFrom<Time, Error = TimeConversionError> + TryInto<Time, Error = TimeConversionError>,
107 {
108 let t = sample_time(Time::UNSPECIFIED_TIMEZONE);
109
110 let dt: T = t.try_into().unwrap();
111 let back: Time = dt.try_into().unwrap();
112
113 assert_eq!(back.0.year, t.0.year);
114 assert_eq!(back.0.month, t.0.month);
115 assert_eq!(back.0.day, t.0.day);
116 assert_eq!(back.0.hour, t.0.hour);
117 assert_eq!(back.0.minute, t.0.minute);
118 assert_eq!(back.0.second, t.0.second);
119 assert_eq!(back.0.nanosecond, t.0.nanosecond);
120 }
121
122 pub fn zoned_roundtrip<T>()
123 where
124 T: TryFrom<Time, Error = TimeConversionError> + TryInto<Time, Error = TimeConversionError>,
125 {
126 let t = sample_time(120);
127
128 let dt: T = t.try_into().unwrap();
129 let back: Time = dt.try_into().unwrap();
130
131 assert_eq!(back.0.time_zone, 120);
132 assert_eq!(back.0.hour, t.0.hour);
133 assert_eq!(back.0.minute, t.0.minute);
134 }
135
136 pub fn negative_offset_roundtrip<T>()
137 where
138 T: TryFrom<Time, Error = TimeConversionError> + TryInto<Time, Error = TimeConversionError>,
139 {
140 let t = sample_time(-330);
141
142 let dt: T = t.try_into().unwrap();
143 let back: Time = dt.try_into().unwrap();
144
145 assert_eq!(back.0.time_zone, -330);
146 }
147
148 pub fn preserves_nanoseconds<T>()
149 where
150 T: TryFrom<Time, Error = TimeConversionError> + TryInto<Time, Error = TimeConversionError>,
151 {
152 let t = sample_time(60);
153
154 let dt: T = t.try_into().unwrap();
155 let back: Time = dt.try_into().unwrap();
156
157 assert_eq!(back.0.nanosecond, 123_456_789);
158 }
159
160 pub fn unspecified_timezone_fails<T>()
161 where
162 T: TryFrom<Time, Error = TimeConversionError>,
163 {
164 let t = sample_time(Time::UNSPECIFIED_TIMEZONE);
165 let result: Result<T, _> = t.try_into();
166 assert!(result.is_err());
167 }
168
169 pub fn invalid_calendar_date_fails<T>()
170 where
171 T: TryFrom<Time, Error = TimeConversionError>,
172 {
173 let result: Result<T, _> = invalid_date().try_into();
174 assert!(result.is_err());
175 }
176}