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

Make XLSError a sub-type of XlsException #1

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
10 changes: 7 additions & 3 deletions lib/Data/Xls.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ CCALL(xls_cell_hidden, XLSCell -> IO Int8 -- Int8)
data XlsException =
XlsFileNotFound String
| XlsParseError String
| Libxls XLSError

Choose a reason for hiding this comment

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

The two cases of XLSError are already represented in XlsException. We can add the other cases as well, something like this:

data XlsException =
      XlsFileNotFound String
    | XlsParseError String
    | XlsSeekError String
    | XlsReadError String 
    | XlsMallocError String

And just translate XLSError to XlsException, and then do not export XLSError.

deriving (Show, Typeable)

instance Exception XlsException

exceptionLeft :: XlsException -> Either XLSError a
exceptionLeft (XlsFileNotFound _) = Left LIBXLS_ERROR_OPEN
exceptionLeft (XlsParseError _) = Left LIBXLS_ERROR_PARSE
exceptionLeft (Libxls e) = Left e

catchXls :: IO a -> IO (Either XLSError a)
catchXls = flip catch (return.exceptionLeft) . fmap Right
Expand Down Expand Up @@ -179,15 +181,17 @@ decodeXlsByteString content = withSystemTempFile "decodeXlsByteString"
hPut h content
decodeXlsIO filePath

-- | Experimental: This function uses the @xls_open_buffer@ function of libxls.
decodeXlsByteString' :: ByteString -> IO (Either XLSError [[[Cell]]])
-- | Alternative to 'decodeXlsByteString':
-- This function uses the @xls_open_buffer@ function of libxls.

Choose a reason for hiding this comment

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

This is hard to understand for users of the library. We can instead say something like:

"This function does not use a temporary file unlike decodeXlsByteString".

BTW, why don't we just use solely this API and remove the other one? Is this not reliable?

-- Can throw 'XlsException' when decoding fails.
decodeXlsByteString' :: ByteString -> IO [[[Cell]]]
decodeXlsByteString' bs = do
(buf,buflen) <- toCBuffer bs
enc <- newCString "UTF-8"
outError <- malloc
wb <- c_xls_open_buffer buf buflen enc outError
e <- peek outError
case e of
either (throwIO. Libxls) return =<< case e of
LIBXLS_OK -> decodeXLSWorkbook Nothing wb
_ -> return (Left e)

Expand Down