From 3bc1be9eb9092a67b69997d426fd44ae24f14036 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Mon, 25 Apr 2022 18:46:57 -0400 Subject: [PATCH] moved decodeUTF8 into helpers section --- ChangeLog.md | 1 + src/Data/CSV/Sip.hs | 15 ++++++++------- test/Data/CSV/SipSpec.hs | 18 +++++++++--------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 74d02fb..27c9426 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -2,3 +2,4 @@ ## Unreleased changes - changed the types of encodeCSV and encodeRawCSV to make them more generic +- slight re-structuring of documentation diff --git a/src/Data/CSV/Sip.hs b/src/Data/CSV/Sip.hs index f73db20..4f6bdc6 100644 --- a/src/Data/CSV/Sip.hs +++ b/src/Data/CSV/Sip.hs @@ -52,8 +52,9 @@ module Data.CSV.Sip ( labelFields, decodeRows, decodeRawRows, - decodeUTF8, toBytes, + -- * Helper Functions + decodeUTF8, ) where import Conduit @@ -211,12 +212,6 @@ decodeRows = decodeRawRows .| mapC (map $ fromMaybe "" . decodeUTF8) decodeRawRows :: Monad m => ConduitT BS.ByteString [BS.ByteString] m () decodeRawRows = toBytes .| evalStateT decodeLoop newDecodeState --- | decode a raw ByteString into Text (if possible) -decodeUTF8 :: BS.ByteString -> Maybe T.Text -decodeUTF8 bs = case decodeUtf8' bs of - Left _ -> Nothing - Right txt -> Just txt - -- | convert a stream to ByteStrings to a stream of bytes toBytes :: Monad m => ConduitT BS.ByteString Word8 m () toBytes = await >>= \case @@ -226,6 +221,12 @@ toBytes = await >>= \case toBytes Nothing -> return () +-- | decode a raw ByteString into Text (if possible) +decodeUTF8 :: BS.ByteString -> Maybe T.Text +decodeUTF8 bs = case decodeUtf8' bs of + Left _ -> Nothing + Right txt -> Just txt + -- Internal data DecodeState = DecodeState diff --git a/test/Data/CSV/SipSpec.hs b/test/Data/CSV/SipSpec.hs index ad396e7..993f0e7 100644 --- a/test/Data/CSV/SipSpec.hs +++ b/test/Data/CSV/SipSpec.hs @@ -40,8 +40,8 @@ spec = describe "Data.CSV.Sip" $ do labelFieldsSpec decodeRowsSpec decodeRawRowsSpec - decodeUTF8Spec toBytesSpec + decodeUTF8Spec encodeCSVSpec :: Spec encodeCSVSpec = describe "encodeCSV" $ do @@ -327,6 +327,14 @@ decodeRawRowsSpec = describe "decodeRawRows" $ mapM_ , ["baz", "quux"] ] +toBytesSpec :: Spec +toBytesSpec = describe "toBytes" $ let + input = ["ab", "cd"] + expected = map (fromIntegral . ord) "abcd" + in it ("should be " ++ show expected) $ do + result <- runConduit $ sourceList input .| toBytes .| consume + result `shouldBe` expected + decodeUTF8Spec :: Spec decodeUTF8Spec = describe "decodeUTF8" $ mapM_ ( \(label, input, expected) -> context label $ @@ -341,12 +349,4 @@ decodeUTF8Spec = describe "decodeUTF8" $ mapM_ , ( "blank", "", Just "" ) ] -toBytesSpec :: Spec -toBytesSpec = describe "toBytes" $ let - input = ["ab", "cd"] - expected = map (fromIntegral . ord) "abcd" - in it ("should be " ++ show expected) $ do - result <- runConduit $ sourceList input .| toBytes .| consume - result `shouldBe` expected - --jl