Skip to content

Refactor decodeFileOrFail to have decodeGetFileOrFail, fixing #57. #58

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions src/Data/Binary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module Data.Binary (
, encodeFile -- :: Binary a => FilePath -> a -> IO ()
, decodeFile -- :: Binary a => FilePath -> IO a
, decodeFileOrFail
, decodeGetFileOrFail

, module Data.Word -- useful

Expand Down Expand Up @@ -215,9 +216,15 @@ decodeFile f = do
-- in 'Right'. In case of decoder errors, the error message together with
-- the byte offset will be returned.
decodeFileOrFail :: Binary a => FilePath -> IO (Either (ByteOffset, String) a)
decodeFileOrFail f =
decodeFileOrFail f = decodeGetFileOrFail get f

-- | Decode a value from a file using a 'Get' monad. In case of success, the
-- value will be returned in 'Right'. In case of decoder errors, the error
-- message together with the byte offset will be returned.
decodeGetFileOrFail :: Get a -> FilePath -> IO (Either (ByteOffset, String) a)
Copy link
Member

Choose a reason for hiding this comment

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

This function probably belongs in Data.Binary.Get and should probably be called runGetSomething. We're going to split off Data.Binary from Data.Binary.Get, the former being a particular serialization format and the latter being a generic binary data parser.

/cc @dcoutts

decodeGetFileOrFail g f =
withBinaryFile f ReadMode $ \h -> do
feed (runGetIncremental get) h
feed (runGetIncremental g) h
where -- TODO: put in Data.Binary.Get and name pushFromHandle?
feed (Done _ _ x) _ = return (Right x)
feed (Fail _ pos str) _ = return (Left (pos, str))
Expand Down