This repository was archived by the owner on May 27, 2019. It is now read-only.
ref/haskell: refactor error handling for bech32Decode#1
Merged
manifoldhiker merged 4 commits intomasterfrom Mar 24, 2018
Merged
ref/haskell: refactor error handling for bech32Decode#1manifoldhiker merged 4 commits intomasterfrom
manifoldhiker merged 4 commits intomasterfrom
Conversation
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.
Since you use this pattern a lot I would recomment implementing local function like:
verify :: Bool -> a -> Either a bIt'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) |
| 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.
If you implemented locally rightToMaybe you could also implement maybeToRight
Author
|
@kirelagin please, review this. I am not quite sure about that, especially about naming |
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
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