Skip to content
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 2 commits into from
Nov 7, 2023
Merged

Conversation

tcharding
Copy link
Member

Before we do v1.0 we want to hide all the error internals so that we can modify/improve them without breaking the public API.

Please pay particular attention to the Display strings because we they are considered part of the public API so we don't want to change them willy-nilly. Of note, because the enums have inner structs it was difficult to not duplicate the error message.

Resolve: #42

In preparation for adding a bunch more error types move the current ones
out of `parse` into `error`.
@tcharding
Copy link
Member Author

tcharding commented Nov 6, 2023

Man, I forget if we decided struct errors are supposed to have #[non_exhaustive]? The hashes::FromSliceError has it so I think we decided yes, if so this PR is incomplete.

EDIT: Found it, they are supposed to be: rust-bitcoin/rust-secp256k1#635 (comment)

Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK a05458f

Just a few things that would be nice to clean up.

src/iter.rs Outdated
let loh = (lo as char).to_digit(16).ok_or(HexToBytesError::InvalidChar(lo))?;
let hih = (hi as char)
.to_digit(16)
.ok_or(HexToBytesError::InvalidChar(InvalidCharError { invalid: hi }))?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can keep it short by writing ok_or(InvalidCharError { invalid: hi })?; thanks to the conversion made by ?.

Copy link
Member Author

Choose a reason for hiding this comment

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

I back and forth'ed on this a bunch of times (literally added and remove code). It works sometimes and in some places it didn't so I went with uniform and wrote it out fully everywhere. I"ll have another go though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I misspoke, you suggestion is clearly better. I was referring to the into case below.

src/parse.rs Outdated Show resolved Hide resolved
src/parse.rs Outdated Show resolved Hide resolved
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
Copy link
Collaborator

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.

Copy link
Member Author

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 using non_exhaustive.

Copy link
Collaborator

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 be non_exhaustive and are not error types.

Copy link
Member Author

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 the non_exhaustive from all struct errors that exist solely to hide internals. On further thought today it is strange to have unneeded non_exhaustive and there will always be private fields since that is the whole point of these types.

Copy link
Collaborator

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`


/// Purported hex string had odd length.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also not needed

Hide the internals of all errors by wrapping enum variant internals in
structs.
@tcharding
Copy link
Member Author

All suggestions implemented @Kixunil except removing non_exhaustive, ok with that?

Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK af04208

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK af04208

@tcharding
Copy link
Member Author

Please don't merge yet, I want to remove the unnecessary non_exhaustive.

@apoelstra apoelstra merged commit 78df275 into rust-bitcoin:master Nov 7, 2023
9 checks passed
@apoelstra
Copy link
Member

lol @tcharding you were just a bit too late, sorry

@tcharding
Copy link
Member Author

Ha! I thought you might have started the merge process soon as you acked. I'll fix as a follow up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Totally conceal error internals
3 participants