-
-
Notifications
You must be signed in to change notification settings - Fork 173
Handle UEFI warnings more cleanly #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b1f87ab
Implement the Completion option up to uefi-test-runner
HadrienG2 4d5ebc9
Clean up semantics of some Completion usage
HadrienG2 9476b32
Remove feature flag for stabilized tool_lints
HadrienG2 2d0577f
Add some derives and support handling warnings as errors
HadrienG2 e67ff7f
Add unwrap() for feature-parity with Result
HadrienG2 3957b56
Do a cargo fmt pass
HadrienG2 971b01c
Port uefi-test-runner to Completion
HadrienG2 d0f4722
Undo changes to prelude
HadrienG2 e32ff66
Add more ergonomic ways to deal with uefi-rs' Results
HadrienG2 15b6231
Interface cleanup
HadrienG2 b89bcfd
Use ResultExt in the test runner
HadrienG2 7e74839
Typo fix
HadrienG2 62aa6ad
Clean up interface & naming
HadrienG2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use super::Status; | ||
use log::warn; | ||
|
||
/// This type is used when an UEFI operation has completed, but some non-fatal | ||
/// problems may have been encountered along the way | ||
#[must_use] | ||
#[derive(Clone, Copy, Debug, PartialEq)] | ||
pub enum Completion<T> { | ||
/// The operation completed without problems | ||
Success(T), | ||
|
||
/// The operation completed, but some non-fatal issues were encountered | ||
Warning(T, Status), | ||
} | ||
|
||
impl<T> Completion<T> { | ||
/// Split the completion into a (status, value) pair | ||
pub fn split(self) -> (T, Status) { | ||
match self { | ||
Completion::Success(res) => (res, Status::SUCCESS), | ||
Completion::Warning(res, stat) => (res, stat), | ||
} | ||
} | ||
|
||
/// Access the inner value, logging the warning if there is any | ||
pub fn log(self) -> T { | ||
match self { | ||
Completion::Success(res) => res, | ||
Completion::Warning(res, stat) => { | ||
warn!("Encountered UEFI warning: {:?}", stat); | ||
res | ||
} | ||
} | ||
} | ||
|
||
/// Assume that no warning occured, panic if not | ||
pub fn unwrap(self) -> T { | ||
match self { | ||
Completion::Success(res) => res, | ||
Completion::Warning(_, w) => { | ||
unwrap_failed("Called `Completion::unwrap()` on a `Warning` value", w) | ||
} | ||
} | ||
} | ||
|
||
/// Assume that no warning occured, panic with provided message if not | ||
pub fn expect(self, msg: &str) -> T { | ||
match self { | ||
Completion::Success(res) => res, | ||
Completion::Warning(_, w) => unwrap_failed(msg, w), | ||
} | ||
} | ||
|
||
/// Transform the inner value without unwrapping the Completion | ||
pub fn map<U>(self, f: impl Fn(T) -> U) -> Completion<U> { | ||
match self { | ||
Completion::Success(res) => Completion::Success(f(res)), | ||
Completion::Warning(res, stat) => Completion::Warning(f(res), stat), | ||
} | ||
} | ||
|
||
/// Merge this completion with a success or warning status | ||
/// | ||
/// Since this type only has storage for one warning, if two warnings must | ||
/// be stored, one of them will be spilled into the logs. | ||
/// | ||
pub fn with_warning(self, extra_stat: Status) -> Self { | ||
match self { | ||
Completion::Success(res) => { | ||
if extra_stat.is_success() { | ||
Completion::Success(res) | ||
} else { | ||
Completion::Warning(res, extra_stat) | ||
} | ||
} | ||
Completion::Warning(res, stat) => { | ||
if extra_stat.is_success() { | ||
Completion::Warning(res, stat) | ||
} else { | ||
warn!("Encountered UEFI warning {:?}", stat); | ||
Completion::Warning(res, extra_stat) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<T> From<T> for Completion<T> { | ||
fn from(res: T) -> Self { | ||
Completion::Success(res) | ||
} | ||
} | ||
|
||
// This is a separate function to reduce the code size of the methods | ||
#[inline(never)] | ||
#[cold] | ||
fn unwrap_failed(msg: &str, warning: Status) -> ! { | ||
panic!("{}: {:?}", msg, warning) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,55 @@ | ||
use core::result; | ||
|
||
/// Definition of UEFI's standard status codes | ||
mod status; | ||
pub use self::status::Status; | ||
|
||
/// Completions are used to model operations which have completed, but may have | ||
/// encountered non-fatal errors ("warnings") along the way | ||
mod completion; | ||
pub use self::completion::Completion; | ||
|
||
/// Return type of many UEFI functions. | ||
pub type Result<T> = result::Result<T, Status>; | ||
pub type Result<T> = core::result::Result<Completion<T>, Status>; | ||
|
||
/// Extension trait for Result which helps dealing with UEFI's warnings | ||
pub trait ResultExt<T> { | ||
/// Treat warnings as errors | ||
fn warn_err(self) -> core::result::Result<T, Status>; | ||
|
||
/// Ignore warnings, keeping a trace of them in the logs | ||
fn warn_log(self) -> core::result::Result<T, Status>; | ||
|
||
/// Expect success without warnings, panic otherwise | ||
fn warn_unwrap(self) -> T; | ||
|
||
/// Expect success without warnings, panic with provided message otherwise | ||
fn warn_expect(self, msg: &str) -> T; | ||
|
||
/// Transform the inner output, if any | ||
fn warn_map<U>(self, f: impl Fn(T) -> U) -> Result<U>; | ||
} | ||
|
||
impl<T> ResultExt<T> for Result<T> { | ||
fn warn_err(self) -> core::result::Result<T, Status> { | ||
match self { | ||
Ok(Completion::Success(v)) => Ok(v), | ||
Ok(Completion::Warning(_, s)) => Err(s), | ||
Err(s) => Err(s), | ||
} | ||
} | ||
|
||
fn warn_log(self) -> core::result::Result<T, Status> { | ||
self.map(|completion| completion.log()) | ||
} | ||
|
||
fn warn_unwrap(self) -> T { | ||
self.unwrap().unwrap() | ||
} | ||
|
||
fn warn_expect(self, msg: &str) -> T { | ||
self.expect(msg).expect(msg) | ||
} | ||
|
||
fn warn_map<U>(self, f: impl Fn(T) -> U) -> Result<U> { | ||
self.map(|completion| completion.map(f)) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.