module Data.Sv.Svfactor (
parseDecode
, parseDecode'
, decodeSvfactor
, ignoreSpaces
, ignoreField
, ignoreSpacedField
, treatAsQuoted
, treatAsQuotedUnspaced
, spacedField
, field
, contents
) where
import Control.Lens (view)
import Data.ByteString (ByteString)
import Data.Profunctor (Profunctor (lmap))
import Data.Svfactor.Parse
import Data.Svfactor.Syntax
import Data.Svfactor.Vector.NonEmpty (toVector)
import Data.Svfactor.Text.Space (spacedValue)
import Data.Svfactor.Text.Quote (Quote (DoubleQuote))
import qualified Data.Sv.Decode.Core as D
import Data.Validation
parseDecode :: D.Decode ByteString (SpacedField ByteString) a -> ParseOptions ByteString -> ByteString -> D.DecodeValidation ByteString [a]
parseDecode =
parseDecode' trifecta
parseDecode' :: SvParser s -> D.Decode s (SpacedField s) a -> ParseOptions s -> s -> D.DecodeValidation s [a]
parseDecode' svp dec opts =
flip bindValidation (decodeSvfactor dec) . D.validateEitherWith D.BadParse . parseSv' svp opts
decodeSvfactor :: D.Decode s (SpacedField s) a -> Sv s -> D.DecodeValidation s [a]
decodeSvfactor dec =
let dec' = D.promote' (view (spacedValue.fieldContents)) dec
in traverse dec' . fmap (toVector . view spacedFields) . recordList
ignoreSpaces :: D.Decode e (Field s) a -> D.Decode e (SpacedField s) a
ignoreSpaces = lmap (view spacedValue)
ignoreField :: D.Decode e s a -> D.Decode e (Field s) a
ignoreField = lmap (view fieldContents)
ignoreSpacedField :: D.Decode e s a -> D.Decode e (SpacedField s) a
ignoreSpacedField = ignoreSpaces . ignoreField
treatAsQuoted :: D.Decode e (Field s) a -> D.Decode e s a
treatAsQuoted = lmap (unescapedField DoubleQuote)
treatAsQuotedUnspaced :: D.Decode e (SpacedField s) a -> D.Decode e s a
treatAsQuotedUnspaced = treatAsQuoted . lmap pure
spacedField :: D.Decode e (SpacedField s) (SpacedField s)
spacedField = D.contents
field :: D.Decode e (SpacedField s) (Field s)
field = fmap (view spacedValue) D.contents
contents :: D.Decode e (SpacedField s) s
contents = fmap (view (spacedValue.fieldContents)) D.contents