This repository has been archived by the owner on May 27, 2019. It is now read-only.
forked from sipa/bech32
-
Notifications
You must be signed in to change notification settings - Fork 0
ref/haskell: refactor error handling for bech32Decode #1
Merged
Conversation
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
chshersh
reviewed
Mar 19, 2018
bech32Decode bech32 = do | ||
guard $ BS.length bech32 <= 90 | ||
guard $ BSC.map toUpper bech32 == bech32 || BSC.map toLower bech32 == bech32 | ||
when (BS.length bech32 > 90) (Left Bech32StringLengthExceeded) |
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.
Since you use this pattern a lot I would recomment implementing local function like:
verify :: Bool -> a -> Either a b
It's like guard
but for Either
.
guard $ BS.length bech32 <= 90 | ||
guard $ BSC.map toUpper bech32 == bech32 || BSC.map toLower bech32 == bech32 | ||
when (BS.length bech32 > 90) (Left Bech32StringLengthExceeded) | ||
when (not $ validCase bech32) (Left CaseInconsistency) |
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.
when (not x)
is unless x
when (BS.length dat < 6) (Left TooShortDataPart) | ||
hrp' <- maybe (Left InvalidHRP) Right $ BSC.stripSuffix (BSC.pack "1") hrp | ||
when (not $ validHRP hrp') (Left InvalidHRP) | ||
dat' <- maybe (Left InvalidCharsetMap) Right $ mapM charsetMap $ BSC.unpack dat |
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.
If you implemented locally rightToMaybe
you could also implement maybeToRight
@kirelagin please, review this. I am not quite sure about that, especially about naming |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
The main reason to refactor was the Maybe in a signature. It is not clear why the given bech32 string is invalid.
Reopen of sipa#41