module Data.Sv.Parse (
parseSv
, parseSv'
, parseSvFromFile
, parseSvFromFile'
, separatedValues
, module Data.Sv.Parse.Options
, SvParser (..)
, trifecta
, attoparsecByteString
, attoparsecText
) where
import Control.Lens (view)
import Control.Monad.IO.Class (MonadIO, liftIO)
import qualified Data.Attoparsec.ByteString as BS (parseOnly)
import qualified Data.Attoparsec.Text as Text (parseOnly)
import Data.Bifunctor (first)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Text (Text)
import qualified Data.Text.IO as Text
import qualified Text.Trifecta as Tri
import Data.Sv.Syntax.Sv (Sv)
import Data.Sv.Decode.Error (trifectaResultToEither)
import Data.Sv.Parse.Internal (separatedValues)
import Data.Sv.Parse.Options
parseSv :: ParseOptions ByteString -> ByteString -> Either ByteString (Sv ByteString)
parseSv = parseSv' trifecta
parseSv' :: SvParser s -> ParseOptions s -> s -> Either s (Sv s)
parseSv' svp opts s =
let enc = view encodeString opts
in first enc $ runSvParser svp opts s
parseSvFromFile :: MonadIO m => ParseOptions ByteString -> FilePath -> m (Either ByteString (Sv ByteString))
parseSvFromFile = parseSvFromFile' trifecta
parseSvFromFile' :: MonadIO m => SvParser s -> ParseOptions s -> FilePath -> m (Either s (Sv s))
parseSvFromFile' svp opts fp =
let enc = view encodeString opts
in liftIO (first enc <$> runSvParserFromFile svp opts fp)
data SvParser s = SvParser
{ runSvParser :: ParseOptions s -> s -> Either String (Sv s)
, runSvParserFromFile :: ParseOptions s -> FilePath -> IO (Either String (Sv s))
}
trifecta :: SvParser ByteString
trifecta = SvParser
{ runSvParser = \opts bs -> trifectaResultToEither $ Tri.parseByteString (separatedValues opts) mempty bs
, runSvParserFromFile = \opts fp -> trifectaResultToEither <$> Tri.parseFromFileEx (separatedValues opts) fp
}
attoparsecByteString :: SvParser ByteString
attoparsecByteString = SvParser
{ runSvParser = \opts bs -> BS.parseOnly (separatedValues opts) bs
, runSvParserFromFile = \opts fp -> BS.parseOnly (separatedValues opts) <$> BS.readFile fp
}
attoparsecText :: SvParser Text
attoparsecText = SvParser
{ runSvParser = \opts t -> Text.parseOnly (separatedValues opts) t
, runSvParserFromFile = \opts fp -> Text.parseOnly (separatedValues opts) <$> Text.readFile fp
}