Skip to content

Implement Error and Display traits for FromStrError #610

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
merged 1 commit into from
Dec 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions uefi/src/data_types/owned_strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ pub enum FromStrError {
InteriorNul,
}

impl fmt::Display for FromStrError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"UCS-2 Conversion Error: {}",
match self {
Self::InvalidChar => "Invalid character",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicholasbishop I think we should find a solution to guarantee consistency. Either we use derive_more::Display for errors or the display impl should fall back to the Debug impl. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having Debug and Display be different is fine, with the general convention that Debug should be a bare-bones representation of the type, and Display should be human readable.

For example, in Rust's std, Utf8Error follows this pattern. It has derive(Debug) and an impl Derive that manually provides a more human-readable string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Self::InteriorNul => "Interior null terminator",
}
)
}
}

#[cfg(feature = "unstable")]
impl core::error::Error for FromStrError {}

/// An owned UCS-2 null-terminated string.
///
/// For convenience, a [CString16] is comparable with `&str` and `String` from the standard library
Expand Down