Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb237f5b

Browse files
committed
uefi: Make TimeError more descriptive
When returning a TimeError, it'd be helpful to specify to the user which fieldis invalid so that it can be handled accordingly, or at least communicated. Thischange does this by adding bool fields to the TimeError struct, representing thevalidity of each field of a Time struct.
1 parentcfd63fb commitb237f5b

File tree

2 files changed

+89
-16
lines changed

2 files changed

+89
-16
lines changed

‎uefi/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Added`TryFrom<&[u8]>` for`DevicePathHeader`,`DevicePathNode` and`DevicePath`.
1515
- Added`ByteConversionError`.
1616
- Re-exported`CapsuleFlags`.
17+
- One can now specify in`TimeError` what fields of`Time` are outside its valid range.`Time::is_valid` has been updated accordingly.
1718

1819
##Changed
1920
-`SystemTable::exit_boot_services` is now`unsafe`. See that method's

‎uefi/src/table/runtime.rs

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -371,19 +371,66 @@ pub struct TimeParams {
371371
pubdaylight:Daylight,
372372
}
373373

374-
/// Error returned by [`Time`] methods if the input is outside the valid range.
375-
#[derive(Copy,Clone,Debug,Default,Eq,PartialEq)]
376-
pubstructTimeError;
374+
/// Error returned by [`Time`] methods. A bool value of `true` means
375+
/// the specified field is outside of its valid range.
376+
#[allow(missing_docs)]
377+
#[derive(Debug,Default,PartialEq,Eq)]
378+
pubstructTimeError{
379+
pubyear:bool,
380+
pubmonth:bool,
381+
pubday:bool,
382+
pubhour:bool,
383+
pubminute:bool,
384+
pubsecond:bool,
385+
pubnanosecond:bool,
386+
pubtimezone:bool,
387+
pubdaylight:bool,
388+
}
389+
390+
#[cfg(feature ="unstable")]
391+
impl core::error::ErrorforTimeError{}
377392

378393
implDisplayforTimeError{
379394
fnfmt(&self,f:&mutFormatter<'_>) -> fmt::Result{
380-
write!(f,"{self:?}")
395+
if*self ==TimeError::default(){
396+
write!(f,"No erroneous fields found in EFI Time.")?;
397+
}else{
398+
write!(f,"Erroneous fields found in EFI Time:")?;
399+
ifself.year{
400+
write!(f,"\nyear not within `1900..=9999`")?;
401+
}
402+
ifself.month{
403+
write!(f,"\nmonth not within `1..=12")?;
404+
}
405+
ifself.day{
406+
write!(f,"\nday not within `1..=31`")?;
407+
}
408+
ifself.hour{
409+
write!(f,"\nhour not within `0..=23`")?;
410+
}
411+
ifself.minute{
412+
write!(f,"\nminute not within `0..=59`")?;
413+
}
414+
ifself.second{
415+
write!(f,"\nsecond not within `0..=59`")?;
416+
}
417+
ifself.nanosecond{
418+
write!(f,"\nnanosecond not within `0..=999_999_999`")?;
419+
}
420+
ifself.timezone{
421+
write!(
422+
f,
423+
"\ntime_zone not `Time::UNSPECIFIED_TIMEZONE` nor within `-1440..=1440`"
424+
)?;
425+
}
426+
ifself.daylight{
427+
write!(f,"\nunknown bits set for daylight")?;
428+
}
429+
}
430+
Ok(())
381431
}
382432
}
383433

384-
#[cfg(feature ="unstable")]
385-
impl core::error::ErrorforTimeError{}
386-
387434
implTime{
388435
/// Unspecified Timezone/local time.
389436
constUNSPECIFIED_TIMEZONE:i16 = uefi_raw::time::Time::UNSPECIFIED_TIMEZONE;
@@ -404,11 +451,8 @@ impl Time {
404451
daylight: params.daylight,
405452
pad2:0,
406453
});
407-
if time.is_valid(){
408-
Ok(time)
409-
}else{
410-
Err(TimeError)
411-
}
454+
455+
time.is_valid().map(|_| time)
412456
}
413457

414458
/// Create an invalid `Time` with all fields set to zero. This can
@@ -422,10 +466,38 @@ impl Time {
422466
Self(uefi_raw::time::Time::invalid())
423467
}
424468

425-
/// True if all fields are within valid ranges, false otherwise.
426-
#[must_use]
427-
pubfnis_valid(&self) ->bool{
428-
self.0.is_valid()
469+
/// `Ok()` if all fields are within valid ranges, `Err(TimeError)` otherwise.
470+
pubfnis_valid(&self) -> core::result::Result<(),TimeError>{
471+
letmut err =TimeError::default();
472+
if !(1900..=9999).contains(&self.year()){
473+
err.year =true;
474+
}
475+
if !(1..=12).contains(&self.month()){
476+
err.month =true;
477+
}
478+
if !(1..=31).contains(&self.day()){
479+
err.day =true;
480+
}
481+
ifself.hour() >23{
482+
err.hour =true;
483+
}
484+
ifself.minute() >59{
485+
err.minute =true;
486+
}
487+
ifself.second() >59{
488+
err.second =true;
489+
}
490+
ifself.nanosecond() >999_999_999{
491+
err.nanosecond =true;
492+
}
493+
ifself.time_zone().is_some() && !((-1440..=1440).contains(&self.time_zone().unwrap())){
494+
err.timezone =true;
495+
}
496+
if err ==TimeError::default(){
497+
Ok(())
498+
}else{
499+
Err(err)
500+
}
429501
}
430502

431503
/// Query the year.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp