-
Notifications
You must be signed in to change notification settings - Fork 9
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
Hide all error internals #44
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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,5 +1,9 @@ | ||
// SPDX-License-Identifier: CC0-1.0 | ||
|
||
use core::fmt; | ||
|
||
use crate::write_err; | ||
|
||
/// Formats error. | ||
/// | ||
/// If `std` feature is OFF appends error source (delimited by `: `). We do this because | ||
|
@@ -21,3 +25,145 @@ macro_rules! write_err { | |
} | ||
} | ||
} | ||
|
||
/// Hex decoding error. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum HexToBytesError { | ||
/// Non-hexadecimal character. | ||
InvalidChar(InvalidCharError), | ||
/// Purported hex string had odd length. | ||
OddLengthString(OddLengthStringError), | ||
} | ||
|
||
impl fmt::Display for HexToBytesError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
use HexToBytesError::*; | ||
|
||
match *self { | ||
InvalidChar(ref e) => write_err!(f, "invalid char, failed to create bytes from hex"; e), | ||
OddLengthString(ref e) => | ||
write_err!(f, "odd length, failed to create bytes from hex"; e), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for HexToBytesError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
use HexToBytesError::*; | ||
|
||
match *self { | ||
InvalidChar(ref e) => Some(e), | ||
OddLengthString(ref e) => Some(e), | ||
} | ||
} | ||
} | ||
|
||
impl From<InvalidCharError> for HexToBytesError { | ||
#[inline] | ||
fn from(e: InvalidCharError) -> Self { Self::InvalidChar(e) } | ||
} | ||
|
||
impl From<OddLengthStringError> for HexToBytesError { | ||
#[inline] | ||
fn from(e: OddLengthStringError) -> Self { Self::OddLengthString(e) } | ||
} | ||
|
||
/// Invalid hex character. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
#[non_exhaustive] | ||
pub struct InvalidCharError { | ||
pub(crate) invalid: u8, | ||
} | ||
|
||
impl fmt::Display for InvalidCharError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "invalid hex char {}", self.invalid) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for InvalidCharError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } | ||
} | ||
|
||
/// Purported hex string had odd length. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
#[non_exhaustive] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also not needed |
||
pub struct OddLengthStringError { | ||
pub(crate) len: usize, | ||
} | ||
|
||
impl fmt::Display for OddLengthStringError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "odd hex string length {}", self.len) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for OddLengthStringError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } | ||
} | ||
|
||
/// Hex decoding error. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum HexToArrayError { | ||
/// Conversion error while parsing hex string. | ||
Conversion(HexToBytesError), | ||
/// Tried to parse fixed-length hash from a string with the wrong length (got, want). | ||
InvalidLength(InvalidLengthError), | ||
} | ||
|
||
impl fmt::Display for HexToArrayError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
use HexToArrayError::*; | ||
|
||
match *self { | ||
Conversion(ref e) => | ||
crate::write_err!(f, "conversion error, failed to create array from hex"; e), | ||
InvalidLength(ref e) => | ||
write_err!(f, "invalid length, failed to create array from hex"; e), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for HexToArrayError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
use HexToArrayError::*; | ||
|
||
match *self { | ||
Conversion(ref e) => Some(e), | ||
InvalidLength(ref e) => Some(e), | ||
} | ||
} | ||
} | ||
|
||
impl From<HexToBytesError> for HexToArrayError { | ||
#[inline] | ||
fn from(e: HexToBytesError) -> Self { Self::Conversion(e) } | ||
} | ||
|
||
impl From<InvalidLengthError> for HexToArrayError { | ||
#[inline] | ||
fn from(e: InvalidLengthError) -> Self { Self::InvalidLength(e) } | ||
} | ||
|
||
/// Tried to parse fixed-length hash from a string with the wrong length. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
#[non_exhaustive] | ||
pub struct InvalidLengthError { | ||
pub(crate) expected: usize, | ||
pub(crate) got: usize, | ||
} | ||
|
||
impl fmt::Display for InvalidLengthError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "bad hex string length {} (expected {})", self.got, self.expected) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for InvalidLengthError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } | ||
} |
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed when at least one field is not
pub
but I could understand if someone wants to avoid us forgetting to add it in the future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inclined to put it on every struct regardless because it makes scanning the error types quicker. However, this code is plain old sloppy, I've been not adding
Copy
when usingnon_exhaustive
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think naming error type
*Error
is much better. There are other types that need to benon_exhaustive
and are not error types.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't follow your comment, all errors are suffixed with
Error
. I'll remove thenon_exhaustive
from all struct errors that exist solely to hide internals. On further thought today it is strange to have unneedednon_exhaustive
and there will always be private fields since that is the whole point of these types.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they are and should continue to be. That's my point
rg 'struct [A-Za-z0-9]*Error' is better than
rg non_exhaustive`