Type sinonim for ParsecError

This commit is contained in:
Luca Molteni 2018-06-09 12:06:14 +02:00
parent 8e15b253c1
commit ae07f04313

View File

@ -82,6 +82,8 @@ type Record = [Field]
type Field = String type Field = String
type CSVError = Parsec.ParseError
reader :: Reader reader :: Reader
reader = Reader reader = Reader
{rFormat = "csv" {rFormat = "csv"
@ -183,17 +185,13 @@ readJournalFromCsv mrulesfile csvfile csvdata =
return $ Right nulljournal{jtxns=txns''} return $ Right nulljournal{jtxns=txns''}
parseCsv :: FilePath -> String -> IO (Either Parsec.ParseError CSV) parseCsv :: FilePath -> String -> IO (Either CSVError CSV)
parseCsv path csvdata = parseCsv path csvdata =
case path of case path of
"-" -> liftM (parseCassava "(stdin)") getContents "-" -> liftM (parseCassava "(stdin)") getContents
_ -> return $ parseCassava path csvdata _ -> return $ parseCassava path csvdata
parseCassava :: FilePath -> String -> Either CSVError CSV
parseCassava :: FilePath -> String -> Either String [Record]
parseCassava path content = fmap fromCassavaToCSV $ DCSV.decode DCSV.NoHeader (C.pack content)
parseCassava :: FilePath -> String -> Either Parsec.ParseError CSV
parseCassava path content = parseCassava path content =
case parseResult of case parseResult of
Left msg -> Left $ toErrorMessage msg Left msg -> Left $ toErrorMessage msg
@ -204,7 +202,7 @@ fromCassavaToCSV :: (Vector (Vector C.ByteString)) -> CSV
fromCassavaToCSV records = toList (toCSVRecord <$> records) fromCassavaToCSV records = toList (toCSVRecord <$> records)
where toCSVRecord fields = toList (C.unpack <$> fields) where toCSVRecord fields = toList (C.unpack <$> fields)
toErrorMessage :: String -> Parsec.ParseError toErrorMessage :: String -> CSVError
toErrorMessage msg = Parsec.newErrorMessage (Parsec.Message msg) (Parsec.newPos "" 0 0) toErrorMessage msg = Parsec.newErrorMessage (Parsec.Message msg) (Parsec.newPos "" 0 0)
@ -219,7 +217,7 @@ printCSV records = unlines (printRecord `map` records)
-- | Return the cleaned up and validated CSV data (can be empty), or an error. -- | Return the cleaned up and validated CSV data (can be empty), or an error.
validateCsv :: Int -> Either Parsec.ParseError CSV -> Either String [CsvRecord] validateCsv :: Int -> Either CSVError CSV -> Either String [CsvRecord]
validateCsv _ (Left e) = Left $ show e validateCsv _ (Left e) = Left $ show e
validateCsv numhdrlines (Right rs) = validate $ drop numhdrlines $ filternulls rs validateCsv numhdrlines (Right rs) = validate $ drop numhdrlines $ filternulls rs
where where