Skip to content

Commit 6681ed3

Browse files
committed
uefi: time: add integration_common module
This module provides the foundational plumbing for future time-related crate integrations. It introduces common error types, including a unified opaque ConversionError to standardize conversion failures across crates. While integration with crates like time and jiff is planned in subsequent commits, this module lays the groundwork, ensuring a consistent and minimal API for handling errors once those integrations are implemented.
1 parent 22afe4f commit 6681ed3

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
6+
use crate::runtime::TimeError;
7+
use core::error::Error;
8+
use core::fmt;
9+
use core::fmt::{Display, Formatter};
10+
11+
/// An opaque error type indicating a UEFI [`Time`] could not be converted.
12+
///
13+
/// [`Time`]: super::Time
14+
#[derive(Debug)]
15+
pub struct ConversionError(pub(super) ConversionErrorInner);
16+
17+
impl Display for ConversionError {
18+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
19+
write!(f, "Time conversion error: {}", self.0)
20+
}
21+
}
22+
23+
impl Error for ConversionError {
24+
fn source(&self) -> Option<&(dyn Error + 'static)> {
25+
// Don't expose the inner error, it is not useful to the user.
26+
None
27+
}
28+
}
29+
30+
#[derive(Debug)]
31+
pub(super) enum ConversionErrorInner {
32+
/// Invalid component.
33+
InvalidComponent,
34+
/// Invalid UEFI time: [`Time::is_valid`] reported an error.
35+
InvalidUefiTime(TimeError),
36+
/// A timezone was required for the conversion, but the UEFI time indicates
37+
/// [`Time::UNSPECIFIED_TIMEZONE`].
38+
///
39+
/// [`Time::UNSPECIFIED_TIMEZONE`]: super::Time::UNSPECIFIED_TIMEZONE
40+
UnspecifiedTimezone,
41+
}
42+
43+
impl Display for ConversionErrorInner {
44+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
45+
match self {
46+
Self::InvalidComponent => write!(f, "Invalid component"),
47+
Self::InvalidUefiTime(e) => write!(f, "Invalid UEFI time: {e}"),
48+
Self::UnspecifiedTimezone => write!(f, "Unspecified timezone"),
49+
}
50+
}
51+
}
52+
53+
impl Error for ConversionErrorInner {
54+
fn source(&self) -> Option<&(dyn Error + 'static)> {
55+
match self {
56+
Self::InvalidComponent => None,
57+
Self::InvalidUefiTime(e) => Some(e),
58+
Self::UnspecifiedTimezone => None,
59+
}
60+
}
61+
}

uefi/src/runtime/time_defs/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use core::fmt;
77
use core::fmt::{Debug, Display, Formatter};
88
use uefi_raw::time::Daylight;
99

10+
#[allow(unused)]
11+
mod integration_common;
12+
1013
/// Date and time representation.
1114
#[derive(Copy, Clone, Eq, PartialEq)]
1215
#[repr(transparent)]

0 commit comments

Comments
 (0)