Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
Jonathan Lamothe | af4066d3db | ||
Jonathan Lamothe | 3bc1be9eb9 | ||
Jonathan Lamothe | d99862b3f4 | ||
Jonathan Lamothe | 22b6942900 | ||
Jonathan Lamothe | c1e9fb7b8e |
|
@ -1,3 +1,5 @@
|
|||
# Changelog for csv-sip
|
||||
|
||||
## Unreleased changes
|
||||
- changed the types of encodeCSV and encodeRawCSV to make them more generic
|
||||
- slight re-structuring of documentation
|
||||
|
|
|
@ -6,8 +6,8 @@ cabal-version: 2.2
|
|||
|
||||
name: csv-sip
|
||||
version: 0.1.0
|
||||
synopsis: extracts data from a CSV file
|
||||
description: extracts data from a CSV file - see README.md for more details
|
||||
synopsis: CSV streaming library
|
||||
description: CSV streaming library - see README.md for more details
|
||||
category: Data
|
||||
homepage: https://codeberg.org/jlamothe/csv-sip
|
||||
bug-reports: https://codeberg.org/jlamothe/csv-sip/issues
|
||||
|
|
|
@ -10,13 +10,13 @@ extra-source-files:
|
|||
- ChangeLog.md
|
||||
|
||||
# Metadata used when publishing your package
|
||||
synopsis: extracts data from a CSV file
|
||||
synopsis: CSV streaming library
|
||||
category: Data
|
||||
|
||||
# To avoid duplicated efforts in documentation and dealing with the
|
||||
# complications of embedding Haddock markup inside cabal files, it is
|
||||
# common to point users to the README.md file.
|
||||
description: extracts data from a CSV file - see README.md for more details
|
||||
description: CSV streaming library - see README.md for more details
|
||||
homepage: https://codeberg.org/jlamothe/csv-sip
|
||||
bug-reports: https://codeberg.org/jlamothe/csv-sip/issues
|
||||
|
||||
|
|
|
@ -52,8 +52,9 @@ module Data.CSV.Sip (
|
|||
labelFields,
|
||||
decodeRows,
|
||||
decodeRawRows,
|
||||
decodeUTF8,
|
||||
toBytes,
|
||||
-- * Helper Functions
|
||||
decodeUTF8,
|
||||
) where
|
||||
|
||||
import Conduit
|
||||
|
@ -153,7 +154,7 @@ encodeCSV
|
|||
:: Monad m
|
||||
=> [[T.Text]]
|
||||
-- ^ the data being encoded, organized into rows and fields
|
||||
-> ConduitT () BS.ByteString m ()
|
||||
-> ConduitT o BS.ByteString m ()
|
||||
encodeCSV csv = sourceList csv .| encodeRows
|
||||
|
||||
-- | encode an entire CSV file
|
||||
|
@ -161,7 +162,7 @@ encodeRawCSV
|
|||
:: Monad m
|
||||
=> [[BS.ByteString]]
|
||||
-- ^ the data being encoded, organized into rows and fields
|
||||
-> ConduitT () BS.ByteString m ()
|
||||
-> ConduitT o BS.ByteString m ()
|
||||
encodeRawCSV csv = sourceList csv .| encodeRawRows
|
||||
|
||||
-- | Writes a stream of Text-based rows to a CSV file
|
||||
|
@ -211,13 +212,7 @@ 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 string of bytes
|
||||
-- | convert a stream to ByteStrings to a stream of bytes
|
||||
toBytes :: Monad m => ConduitT BS.ByteString Word8 m ()
|
||||
toBytes = await >>= \case
|
||||
Just bs -> do
|
||||
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user