Use Text and Lazy Bytestring

This commit is contained in:
Luca Molteni 2018-06-10 13:28:57 +02:00
parent 470c9bcb8d
commit 79f59fd28c
3 changed files with 17 additions and 6 deletions

View File

@ -44,6 +44,7 @@ import Data.Ord
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
import Data.Time.Calendar (Day)
#if MIN_VERSION_time(1,5,0)
@ -60,6 +61,9 @@ import qualified Data.Csv as DSCV
import Test.HUnit hiding (State)
import qualified Data.Csv as Cassava
import qualified Data.Csv.Parser.Megaparsec as CassavaMP
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Char8 as C
import Data.Foldable
import Data.Either
import Text.Megaparsec hiding (parse)
@ -72,6 +76,7 @@ import Hledger.Utils.UTF8IOCompat (getContents)
import Hledger.Utils
import Hledger.Read.Common (Reader(..),InputOpts(..),amountp, statusp, genericSourcePos)
import Data.String.Conversions
type CSV = [Record]
@ -141,7 +146,7 @@ readJournalFromCsv mrulesfile csvfile csvdata =
records <- (either throwerr id .
dbg2 "validateCsv" . validateCsv skip .
dbg2 "parseCsv")
`fmap` parseCsv parsecfilename (T.unpack csvdata)
`fmap` parseCsv parsecfilename csvdata
dbg1IO "first 3 csv records" $ take 3 records
-- identify header lines
@ -183,20 +188,21 @@ readJournalFromCsv mrulesfile csvfile csvdata =
return $ Right nulljournal{jtxns=txns''}
parseCsv :: FilePath -> String -> IO (Either CSVError CSV)
parseCsv :: FilePath -> Text -> IO (Either CSVError CSV)
parseCsv path csvdata =
case path of
"-" -> liftM (parseCassava "(stdin)") getContents
"-" -> liftM (parseCassava "(stdin)") T.getContents
_ -> return $ parseCassava path csvdata
parseCassava :: FilePath -> String -> Either CSVError CSV
parseCassava :: FilePath -> Text -> Either CSVError CSV
parseCassava path content =
case parseResult of
Left msg -> Left $ CSVError msg
Right a -> Right a
where parseResult = fmap parseResultToCsv $ CassavaMP.decode Cassava.NoHeader path (C.pack content)
where parseResult = fmap parseResultToCsv $ CassavaMP.decode Cassava.NoHeader path lazyContent
lazyContent = cs $ T.encodeUtf8 content
parseResultToCsv :: (Foldable t, Functor t) => t (t C.ByteString) -> CSV
parseResultToCsv :: (Foldable t, Functor t) => t (t B.ByteString) -> CSV
parseResultToCsv = toListList . unpackFields
where
toListList = toList . fmap toList

View File

@ -131,6 +131,7 @@ library
, regex-tdfa
, safe >=0.2
, split >=0.1
, string-conversions
, tabular >=0.2
, text >=1.2
, time >=1.5
@ -230,6 +231,7 @@ test-suite doctests
, regex-tdfa
, safe >=0.2
, split >=0.1
, string-conversions
, tabular >=0.2
, text >=1.2
, time >=1.5
@ -329,6 +331,7 @@ test-suite easytests
, regex-tdfa
, safe >=0.2
, split >=0.1
, string-conversions
, tabular >=0.2
, text >=1.2
, time >=1.5
@ -428,6 +431,7 @@ test-suite hunittests
, regex-tdfa
, safe >=0.2
, split >=0.1
, string-conversions
, tabular >=0.2
, test-framework
, test-framework-hunit

View File

@ -74,6 +74,7 @@ dependencies:
- uglymemo
- utf8-string >=0.3.5
- extra
- string-conversions
- Glob >= 0.9
# for ledger-parse:
#- parsers >=0.5